trie icon indicating copy to clipboard operation
trie copied to clipboard

Simplified trie implementation

Open kostaz opened this issue 8 years ago • 0 comments

First of all, @fvbock, thanks a bunch for the trie example! But I'm getting a hard time understanding it. Can you please implement more simple trie example? Something like the below:

package main

type Node struct {
	end  bool
	nmap map[byte]*Node
}

func (n *Node) Add(entry []byte) {
	if len(entry) == 0 {
		n.end = true
		return
	}

	b := entry[0] // current byte
	if n.nmap[b] == nil {
		n.nmap[b] = &Node{end: false, nmap: make(map[byte]*Node)}
	}
	n.nmap[b].Add(entry[1:])
}

func main() {
	n := &Node{end: false, nmap: make(map[byte]*Node)}
	n.Add([]byte("a"))
	n.Add([]byte("b"))
	n.Add([]byte("aa"))
	n.Add([]byte("abc"))
}

I've put this very simple example here: https://github.com/kostaz/playground-go-trie/blob/master/trie.go It will really help myself and other guys...

kostaz avatar Nov 18 '17 10:11 kostaz