Learning GoLang: Implement Trie (Prefix Tree)

in #programming5 years ago

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.

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.

/** 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:

Reposted to Blog

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^

NEW! Following my Trail (Upvote or/and Downvote)

Follow me for topics of Algorithms, Blockchain and Cloud.
I am @justyy - a Steem Witness
https://steemyy.com

My contributions

Delegation Service

  1. Voting Algorithm Updated to Favor those High Delegations!
  • Delegate 1000 to justyy: Link
  • Delegate 5000 to justyy: Link
  • Delegate 10000 to justyy: Link

Support me

If you like my work, please:

  1. Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
  2. Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
  3. Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&action=proxy
    Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses

Sort:  

Thank You for sharing Your insights...

😇🥰😍

[WhereIn Android] (http://www.wherein.io)

Coin Marketplace

STEEM 0.05
TRX 0.33
JST 0.080
BTC 63386.83
ETH 1683.18
USDT 1.00
SBD 0.41