← Back

Trie (Prefix Tree)

Store words by shared prefixes for fast insert and lookup.

1 / 1
// Each node has one child slot per lowercase letter, plus a flag marking
// whether a complete word ends here. Shared prefixes share the same path.
type Trie struct {
    children [26]*Trie
    end      bool
}
// Each node has one child slot per lowercase letter, plus a flag marking
// whether a complete word ends here. Shared prefixes share the same path.
type Trie struct {
    children [26]*Trie
    end      bool
}

    // Start at the root and walk down one letter at a time.
        // Map the letter to a slot 0..25.
// Each node has one child slot per lowercase letter, plus a flag marking
// whether a complete word ends here. Shared prefixes share the same path.
type Trie struct {
    children [26]*Trie
    end      bool
}

func (t *Trie) Insert(word string) {
    // Start at the root and walk down one letter at a time.
    node := t
    for _, ch := range word {
        // Map the letter to a slot 0..25.
        i := ch - 'a'
    }
}
// Each node has one child slot per lowercase letter, plus a flag marking
// whether a complete word ends here. Shared prefixes share the same path.
type Trie struct {
    children [26]*Trie
    end      bool
}

func (t *Trie) Insert(word string) {
    // Start at the root and walk down one letter at a time.
    node := t
    for _, ch := range word {
        // Map the letter to a slot 0..25.
        i := ch - 'a'

        // If this letter has never been seen from here, grow a new branch.

        // Descend into that child and continue with the next letter.
    }
}
// Each node has one child slot per lowercase letter, plus a flag marking
// whether a complete word ends here. Shared prefixes share the same path.
type Trie struct {
    children [26]*Trie
    end      bool
}

func (t *Trie) Insert(word string) {
    // Start at the root and walk down one letter at a time.
    node := t
    for _, ch := range word {
        // Map the letter to a slot 0..25.
        i := ch - 'a'

        // If this letter has never been seen from here, grow a new branch.
        if node.children[i] == nil {
            node.children[i] = &Trie{}
        }

        // Descend into that child and continue with the next letter.
        node = node.children[i]
    }
}
// Each node has one child slot per lowercase letter, plus a flag marking
// whether a complete word ends here. Shared prefixes share the same path.
type Trie struct {
    children [26]*Trie
    end      bool
}

func (t *Trie) Insert(word string) {
    // Start at the root and walk down one letter at a time.
    node := t
    for _, ch := range word {
        // Map the letter to a slot 0..25.
        i := ch - 'a'

        // If this letter has never been seen from here, grow a new branch.
        if node.children[i] == nil {
            node.children[i] = &Trie{}
        }

        // Descend into that child and continue with the next letter.
        node = node.children[i]
    }

    // We've placed every letter; mark this node as the end of a real word.
}
// Each node has one child slot per lowercase letter, plus a flag marking
// whether a complete word ends here. Shared prefixes share the same path.
type Trie struct {
    children [26]*Trie
    end      bool
}

func (t *Trie) Insert(word string) {
    // Start at the root and walk down one letter at a time.
    node := t
    for _, ch := range word {
        // Map the letter to a slot 0..25.
        i := ch - 'a'

        // If this letter has never been seen from here, grow a new branch.
        if node.children[i] == nil {
            node.children[i] = &Trie{}
        }

        // Descend into that child and continue with the next letter.
        node = node.children[i]
    }

    // We've placed every letter; mark this node as the end of a real word.
    node.end = true
}
// Each node has one child slot per lowercase letter, plus a flag marking
// whether a complete word ends here. Shared prefixes share the same path.
type Trie struct {
    children [26]*Trie
    end      bool
}

func (t *Trie) Insert(word string) {
    // Start at the root and walk down one letter at a time.
    node := t
    for _, ch := range word {
        // Map the letter to a slot 0..25.
        i := ch - 'a'

        // If this letter has never been seen from here, grow a new branch.
        if node.children[i] == nil {
            node.children[i] = &Trie{}
        }

        // Descend into that child and continue with the next letter.
        node = node.children[i]
    }

    // We've placed every letter; mark this node as the end of a real word.
    node.end = true
}

    // Follow the same path the letters describe.

        // A missing branch means the word was never inserted.

    // The path exists — but it's only a match if a word actually ended here
    // (otherwise "app" would wrongly match when only "apple" was inserted).
// Each node has one child slot per lowercase letter, plus a flag marking
// whether a complete word ends here. Shared prefixes share the same path.
type Trie struct {
    children [26]*Trie
    end      bool
}

func (t *Trie) Insert(word string) {
    // Start at the root and walk down one letter at a time.
    node := t
    for _, ch := range word {
        // Map the letter to a slot 0..25.
        i := ch - 'a'

        // If this letter has never been seen from here, grow a new branch.
        if node.children[i] == nil {
            node.children[i] = &Trie{}
        }

        // Descend into that child and continue with the next letter.
        node = node.children[i]
    }

    // We've placed every letter; mark this node as the end of a real word.
    node.end = true
}

func (t *Trie) Search(word string) bool {
    // Follow the same path the letters describe.
    node := t
    for _, ch := range word {
        i := ch - 'a'

        // A missing branch means the word was never inserted.
        if node.children[i] == nil {
            return false
        }
        node = node.children[i]
    }

    // The path exists — but it's only a match if a word actually ended here
    // (otherwise "app" would wrongly match when only "apple" was inserted).
    return node.end
}

Tap the left half to go back, the right half to go forward.

Arrow keys or space to navigate · Esc to exit.