-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtree.go
171 lines (156 loc) · 2.81 KB
/
tree.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package trie
import "container/list"
// Tree implemnets ternary trie-tree.
type Tree struct {
// Root is root of the tree. Only Child is valid.
Root Node
// nc means node counts
nc int
}
// New creates a Tree.
func New() *Tree {
return new(Tree)
}
// Get retrieves a value for key.
func (tr *Tree) Get(key string) *Node {
n := &tr.Root
for _, r := range key {
n = n.Get(r)
if n == nil {
return nil
}
}
return n
}
// Put stores a pair of key and value.
func (tr *Tree) Put(key string, value interface{}) *Node {
n := &tr.Root
for _, r := range key {
var f bool
n, f = n.Dig(r)
if f {
tr.nc++
}
}
n.Value = value
return n
}
// Each processes all nodes in width first.
func (tr *Tree) Each(proc NodeProc) {
q := list.New()
q.PushBack(&tr.Root)
for q.Len() != 0 {
f := q.Front()
q.Remove(f)
curr := f.Value.(*Node)
if !proc(curr) {
break
}
curr.Child.Each(func(n *Node) bool {
q.PushBack(n)
return true
})
}
}
// Node implemnets node of ternary trie-tree.
type Node struct {
Label rune
Value interface{}
Low *Node
High *Node
Child *Node
cc int // count of children.
}
// Get finds a child node which Label matches r.
func (n *Node) Get(r rune) *Node {
n = n.Child
for n != nil {
switch {
case r == n.Label:
return n
case r < n.Label:
n = n.Low
default:
n = n.High
}
}
return nil
}
// Dig finds a child node which Label matches r. Or create a new one when there
// are no nodes.
func (n *Node) Dig(r rune) (node *Node, isNew bool) {
if n.Child == nil {
n.Child = &Node{Label: r}
n.cc = 1
return n.Child, true
}
m := n
n = n.Child
for {
switch {
case r == n.Label:
return n, false
case r < n.Label:
if n.Low == nil {
n.Low = &Node{Label: r}
m.cc++
return n.Low, true
}
n = n.Low
default:
if n.High == nil {
n.High = &Node{Label: r}
m.cc++
return n.High, true
}
n = n.High
}
}
}
// Balance balances children nodes.
func (n *Node) Balance() {
if n.Child == nil {
return
}
nodes := make([]*Node, 0, n.cc)
n.Child.Each(func(m *Node) bool {
nodes = append(nodes, m)
return true
})
n.Child = balanceNodes(nodes, 0, len(nodes))
}
// Each processes all sibiling nodes with proc.
func (n *Node) Each(proc NodeProc) bool {
if n == nil {
return true
}
if !n.Low.Each(proc) || !proc(n) || !n.High.Each(proc) {
return false
}
return true
}
func balanceNodes(nodes []*Node, s, e int) *Node {
c := e - s
switch {
case c <= 0:
return nil
case c == 1:
n := nodes[s]
n.Low = nil
n.High = nil
return n
case c == 2:
n := nodes[s]
n.High = nodes[s+1]
n.Low = nil
return n
default:
m := (s + e) / 2
n := nodes[m]
n.Low = balanceNodes(nodes, s, m)
n.High = balanceNodes(nodes, m+1, e)
return n
}
}
// NodeProc provides procedure for nodes.
type NodeProc func(*Node) bool