Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bit trie that supports variable-length keys #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (trie *Trie) Size() int {
}

func (trie *Trie) SizeAtDepth(depth int) int {
if trie.Branch[0] == nil && trie.Branch[1] == nil {
if trie.IsLeaf() {
if trie.IsEmpty() {
return 0
} else {
Expand Down
46 changes: 46 additions & 0 deletions trie2/add2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package trie2

func (trie *Trie) Add(q Key) (insertedDepth int, insertedOK bool) {
return trie.AddAtDepth(0, q)
}

func (trie *Trie) AddAtDepth(depth int, q Key) (insertedDepth int, insertedOK bool) {
if q.Len() == depth {
if trie.Key == nil {
trie.Key = q
return depth, true
} else {
if q.Equal(trie.Key) {
return depth, false
} else {
// trie.Key.Len() > depth
trie.Key, q = q, trie.Key
}
}
}
// q.Len() > depth
switch {
case trie.IsEmptyLeaf():
trie.Key = q
return depth, true
case trie.IsNonEmptyLeaf():
if q.Equal(trie.Key) {
return depth, false
} else {
if trie.Key.Len() == depth {
// both branches are nil
trie.Branch[0], trie.Branch[1] = &Trie{}, &Trie{}
return trie.Branch[q.BitAt(depth)].AddAtDepth(depth+1, q)
} else { // trie.Key.Len() > depth
p := trie.Key
trie.Key = nil
// both branches are nil
trie.Branch[0], trie.Branch[1] = &Trie{}, &Trie{}
trie.Branch[p.BitAt(depth)].Key = p
return trie.Branch[q.BitAt(depth)].AddAtDepth(depth+1, q)
}
}
default:
return trie.Branch[q.BitAt(depth)].AddAtDepth(depth+1, q)
}
}
41 changes: 41 additions & 0 deletions trie2/find2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package trie2

// FindSubKeys finds all keys in the trie that are prefixes of the query key.
func (trie *Trie) FindSubKeys(q Key) (reachedDepth int, found []Key) {
return trie.FindSubKeysAtDepth(0, q)
}

func (trie *Trie) FindSubKeysAtDepth(depth int, q Key) (reachedDepth int, found []Key) {
if trie.IsLeaf() {
if trie.IsEmpty() {
return depth, nil
} else {
return depth, []Key{trie.Key}
}
} else {
reachedDepth, found = trie.Branch[q.BitAt(depth)].FindSubKeysAtDepth(depth+1, q)
if trie.IsEmpty() {
return reachedDepth, found
} else {
return reachedDepth, append(found, trie.Key)
}
}
}

// FindSuperKeys finds all keys in the trie that are prefixed by the query key.
func (trie *Trie) FindSuperKeys(q Key) (reachedDepth int, found []Key) {
return trie.FindSuperKeysAtDepth(0, q)
}

func (trie *Trie) FindSuperKeysAtDepth(depth int, q Key) (reachedDepth int, found []Key) {
d, p := trie.Walk(depth, q)
return d, p.List()
}

func (trie *Trie) Walk(depth int, q Key) (reachedDepth int, arrivedAt *Trie) {
if trie.IsLeaf() {
return depth, trie
} else {
return trie.Branch[q.BitAt(depth)].Walk(depth+1, q)
}
}
19 changes: 19 additions & 0 deletions trie2/list2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package trie2

// List returns a list of all keys in the trie.
func (trie *Trie) List() []Key {
switch {
case trie.IsEmptyLeaf():
return nil
case trie.IsNonEmptyLeaf():
return []Key{trie.Key}
case trie.IsEmpty():
return append(trie.Branch[0].List(), trie.Branch[1].List()...)
case !trie.IsEmpty():
return append(
[]Key{trie.Key},
append(trie.Branch[0].List(), trie.Branch[1].List()...)...,
)
}
panic("unreachable")
}
50 changes: 50 additions & 0 deletions trie2/trie2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package trie2

type Key interface {
Equal(Key) bool
BitAt(int) byte
Len() int
}

type Trie struct {
Branch [2]*Trie
Key Key
}

func (trie *Trie) IsEmpty() bool {
return trie.Key == nil
}

func (trie *Trie) IsLeaf() bool {
return trie.Branch[0] == nil && trie.Branch[1] == nil
}

func (trie *Trie) IsEmptyLeaf() bool {
return trie.IsEmpty() && trie.IsLeaf()
}

func (trie *Trie) IsNonEmptyLeaf() bool {
return !trie.IsEmpty() && trie.IsLeaf()
}

func (trie *Trie) Size() int {
return trie.SizeAtDepth(0)
}

func (trie *Trie) SizeAtDepth(depth int) int {
if trie.IsLeaf() {
if trie.IsEmpty() {
return 0
} else {
return 1
}
} else {
var here int
if trie.IsEmpty() {
here = 0
} else {
here = 1
}
return here + trie.Branch[0].SizeAtDepth(depth+1) + trie.Branch[1].SizeAtDepth(depth+1)
}
}