GoLang: Implement Trie (Prefix Tree)


Trie aka Prefix Tree is a data structure helps to search a word or prefix in a list of strings. The following declars the struct (class) type of Trie Node in GoLang.

1
2
3
4
5
6
7
8
9
10
11
type Trie struct {
    children[26] *Trie
    isLeaf bool
}
 
/** Initialize your data structure here. */
func Constructor() Trie {
    var obj = Trie{}
    obj.isLeaf = false
    return obj
}
type Trie struct {
    children[26] *Trie
    isLeaf bool
}

/** Initialize your data structure here. */
func Constructor() Trie {
    var obj = Trie{}
    obj.isLeaf = false
    return obj
}

Given the input is lowercase or upper characters, we can use a static array of size 26 to store the references (pointers) to children Trie Node. Howerver, we can also use hash map if there are any other characters.

For Trie Node, we usually need to implement the Search, Prefix, and Insert function. They all require walking down the path of Trie.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/** Inserts a word into the trie. */
func (this *Trie) Insert(word string)  {
    var cur = this
    for _, ch := range word {
        var idx = ch - 'a'
        if cur.children[idx] == nil {
            cur.children[idx] = &Trie{}
        }
        cur = cur.children[idx]
    }
    cur.isLeaf = true
}
 
/** Returns if the word is in the trie. */
func (this *Trie) Search(word string) bool {
    var cur = this
    for _, ch := range word {
        var idx = ch - 'a'
        if cur.children[idx] == nil {
            return false
        }
        cur = cur.children[idx]
    }
    return cur.isLeaf
}
 
/** Returns if there is any word in the trie that starts with the given prefix. */
func (this *Trie) StartsWith(prefix string) bool {
    var cur = this
    for _, ch := range prefix {
        var idx = ch - 'a'
        if cur.children[idx] == nil {
            return false
        }
        cur = cur.children[idx]
    }
    return true
}
 
/**
 * Your Trie object will be instantiated and called as such:
 * obj := Constructor();
 * obj.Insert(word);
 * param_2 := obj.Search(word);
 * param_3 := obj.StartsWith(prefix);
 */
/** Inserts a word into the trie. */
func (this *Trie) Insert(word string)  {
    var cur = this
    for _, ch := range word {
        var idx = ch - 'a'
        if cur.children[idx] == nil {
            cur.children[idx] = &Trie{}
        }
        cur = cur.children[idx]
    }
    cur.isLeaf = true
}

/** Returns if the word is in the trie. */
func (this *Trie) Search(word string) bool {
    var cur = this
    for _, ch := range word {
        var idx = ch - 'a'
        if cur.children[idx] == nil {
            return false
        }
        cur = cur.children[idx]
    }
    return cur.isLeaf
}

/** Returns if there is any word in the trie that starts with the given prefix. */
func (this *Trie) StartsWith(prefix string) bool {
    var cur = this
    for _, ch := range prefix {
        var idx = ch - 'a'
        if cur.children[idx] == nil {
            return false
        }
        cur = cur.children[idx]
    }
    return true
}

/**
 * Your Trie object will be instantiated and called as such:
 * obj := Constructor();
 * obj.Insert(word);
 * param_2 := obj.Search(word);
 * param_3 := obj.StartsWith(prefix);
 */

Trie Algorithms Implementations:

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
473 words
Last Post: Teaching Kids Programming - Binary Tree Inorder Traversal via Recursion or Iteration
Next Post: Teaching Kids Programming - Back Tracking Algorithm to Generate Parentheses

The Permanent URL is: GoLang: Implement Trie (Prefix Tree)

Leave a Reply