-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
181 lines (144 loc) · 3.89 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
172
173
174
175
176
177
178
179
180
181
package avl
import (
"golang.org/x/exp/constraints"
)
// Comparator is to compare keys
type Comparator[Key any] func(a, b Key) int
// Tree : AVL tree
type Tree[Key any, Value any] struct {
root *Node[Key, Value]
comparator Comparator[Key]
}
// NewTree creates a new AVL tree
func NewTree[Key any, Value any](comp Comparator[Key]) *Tree[Key, Value] {
return &Tree[Key, Value]{nil, comp}
}
// NewTree creates a new AVL tree for comparable key types
func NewTreeOrdered[Key constraints.Ordered, Value any]() *Tree[Key, Value] {
return &Tree[Key, Value]{nil, func(a, b Key) int {
if a < b {
return 1
} else if a > b {
return -1
}
return 0
}}
}
func (t *Tree[Key, Value]) get(n *Node[Key, Value], key Key) (*Node[Key, Value], bool) {
if n == nil {
return nil, false
}
g := t.comparator(n.Key, key)
if g == 0 {
return n, true
} else if g < 0 {
return t.get(n.child[0], key)
} else {
return t.get(n.child[1], key)
}
}
func (t *Tree[Key, Value]) index(n *Node[Key, Value], rank int) (*Node[Key, Value], bool) {
if n == nil {
return nil, false
}
m := nodeSizeOr(n.child[0])
if rank < m {
return t.index(n.child[0], rank)
} else if rank == m {
return n, true
} else {
return t.index(n.child[1], rank-m-1)
}
}
func (t *Tree[Key, Value]) rank(n *Node[Key, Value], key Key, prev int) int {
if n == nil {
return -1
}
g := t.comparator(n.Key, key)
if g == 0 {
return prev + nodeSizeOr(n.child[0])
} else if g < 0 {
return t.rank(n.child[0], key, prev)
} else {
return t.rank(n.child[1], key, prev+nodeSizeOr(n.child[0])+1)
}
}
func (t *Tree[Key, Value]) set(n, p *Node[Key, Value], force bool) (*Node[Key, Value], bool) {
if n == nil {
return p, true
}
g := t.comparator(n.Key, p.Key)
isChanged := false
if g == 0 {
if force {
n.Val = p.Val
}
return n, false
} else if g < 0 {
n.child[0], isChanged = t.set(n.child[0], p, force)
} else {
n.child[1], isChanged = t.set(n.child[1], p, force)
}
if isChanged {
n.size++
return balance(n), true
}
return n, false
}
func (t *Tree[Key, Value]) erase(n *Node[Key, Value], key Key) (*Node[Key, Value], bool) {
if n == nil {
return nil, false
}
g := t.comparator(n.Key, key)
isChanged := false
if g == 0 {
return moveDown(n.child[0], n.child[1]), true
} else if g < 0 {
n.child[0], isChanged = t.erase(n.child[0], key)
} else {
n.child[1], isChanged = t.erase(n.child[1], key)
}
if isChanged {
n.size--
return balance(n), true
}
return n, false
}
// Get returns the node whose key is 'key' and true.
// If the key is not found, it returns nil and false.
func (t *Tree[Key, Value]) Get(key Key) (*Node[Key, Value], bool) {
return t.get(t.root, key)
}
// Index returns the node whose key is 'rank' th and true.
// If the key is not found, it returns nil and false.
func (t *Tree[Key, Value]) Index(rank int) (*Node[Key, Value], bool) {
return t.index(t.root, rank)
}
// Rank returns the index of the node whose key is 'key'.
// If the key is not found, it returns -1.
func (t *Tree[Key, Value]) Rank(key Key) int {
return t.rank(t.root, key, 0)
}
// Set adds a new node with 'key' and 'val' if a node with the key isn't found.
// Set updates the node with 'key' and 'val' if a node with the key is found.
func (t *Tree[Key, Value]) Set(key Key, val Value) {
t.root, _ = t.set(t.root, newNode(key, val), true)
}
// Insert adds a new node with 'key' and 'val' if a node with the key isn't found.
// Insert do nothing if a node with the key is found.
func (t *Tree[Key, Value]) Insert(key Key, val Value) bool {
var res bool
t.root, res = t.set(t.root, newNode(key, val), false)
return res
}
// Erase erases the node whose key is 'key'
// If the key is not found, it returns false.
func (t *Tree[Key, Value]) Erase(key Key) bool {
var res bool
t.root, res = t.erase(t.root, key)
return res
}
// Size returns the size of the tree
func (t *Tree[Key, Value]) Size() int {
return nodeSizeOr(t.root)
}