Skip to content

Simplified trie implementation #5

@kostaz

Description

@kostaz

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...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions