Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Trie (Prefix Tree) #150

Closed
Tracked by #101
fkdl0048 opened this issue Oct 20, 2024 · 0 comments
Closed
Tracked by #101

Implement Trie (Prefix Tree) #150

fkdl0048 opened this issue Oct 20, 2024 · 0 comments
Assignees
Labels
Milestone

Comments

@fkdl0048
Copy link
Owner

fkdl0048 commented Oct 20, 2024

class TrieNode {
public:
    // 자식 노드들
    TrieNode* children[26];
    // 단어의 끝 여부
    bool isEnd;

    TrieNode() {
        // 모든 자식 노드를 nullptr로 초기화
        memset(children, 0, sizeof(children));
        isEnd = false;
    }
};

class Trie {
private:
    TrieNode* root;

public:
    /** 생성자 */
    Trie() {
        root = new TrieNode();
    }
    
    /** 단어 삽입 */
    void insert(string word) {
        TrieNode* node = root;
        for (char ch : word) {
            int index = ch - 'a';
            // 해당 문자의 노드가 없으면 생성
            if (node->children[index] == nullptr) {
                node->children[index] = new TrieNode();
            }
            // 다음 노드로 이동
            node = node->children[index];
        }
        // 단어의 끝 표시
        node->isEnd = true;
    }
    
    /** 단어 검색 */
    bool search(string word) {
        TrieNode* node = root;
        for (char ch : word) {
            int index = ch - 'a';
            // 해당 문자가 없으면 false
            if (node->children[index] == nullptr) {
                return false;
            }
            node = node->children[index];
        }
        // 단어의 끝까지 도달했고, isEnd가 true인지 확인
        return node->isEnd;
    }
    
    /** 접두사 검색 */
    bool startsWith(string prefix) {
        TrieNode* node = root;
        for (char ch : prefix) {
            int index = ch - 'a';
            // 해당 문자가 없으면 false
            if (node->children[index] == nullptr) {
                return false;
            }
            node = node->children[index];
        }
        // 접두사의 마지막 문자까지 도달하면 true
        return true;
    }
};
@fkdl0048 fkdl0048 mentioned this issue Oct 20, 2024
75 tasks
@fkdl0048 fkdl0048 self-assigned this Oct 20, 2024
@fkdl0048 fkdl0048 added this to Todo Oct 20, 2024
@github-project-automation github-project-automation bot moved this to Todo in Todo Oct 20, 2024
@fkdl0048 fkdl0048 added this to the LeetCode milestone Oct 20, 2024
@fkdl0048 fkdl0048 moved this from Todo to In Progress in Todo Oct 20, 2024
@fkdl0048 fkdl0048 moved this from Two-Week Plan to In Progress in Todo Oct 30, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in Todo Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

No branches or pull requests

1 participant