-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathavl.go
203 lines (176 loc) · 4.08 KB
/
avl.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Package avl provides an implementation of an AVL tree. An AVL tree is a
// self-balancing binary search tree. It stores key-value pairs that are sorted
// based on the key, and maintains that the tree is always balanced, ensuring
// logarithmic-time for all operations.
package avl
import (
g "github.com/zyedidia/generic"
)
// Tree implements an AVL tree.
type Tree[K, V any] struct {
root *node[K, V]
less g.LessFn[K]
}
// New returns an empty AVL tree.
func New[K, V any](less g.LessFn[K]) *Tree[K, V] {
return &Tree[K, V]{
less: less,
}
}
// Put associates 'key' with 'value'.
func (t *Tree[K, V]) Put(key K, value V) {
t.root = t.root.add(key, value, t.less)
}
// Remove removes the value associated with 'key'.
func (t *Tree[K, V]) Remove(key K) {
t.root = t.root.remove(key, t.less)
}
// Get returns the value associated with 'key'.
func (t *Tree[K, V]) Get(key K) (V, bool) {
n := t.root.search(key, t.less)
if n == nil {
var v V
return v, false
}
return n.value, true
}
// Each calls 'fn' on every node in the tree in order
func (t *Tree[K, V]) Each(fn func(key K, val V)) {
t.root.each(fn)
}
// Height returns the height of the tree.
func (t *Tree[K, V]) Height() int {
return t.root.getHeight()
}
// Size returns the number of elements in the tree.
func (t *Tree[K, V]) Size() int {
return t.root.size()
}
type node[K, V any] struct {
key K
value V
height int
left *node[K, V]
right *node[K, V]
}
func (n *node[K, V]) add(key K, value V, less g.LessFn[K]) *node[K, V] {
if n == nil {
return &node[K, V]{
key: key,
value: value,
height: 1,
left: nil,
right: nil,
}
}
if g.Compare(key, n.key, less) < 0 {
n.left = n.left.add(key, value, less)
} else if g.Compare(key, n.key, less) > 0 {
n.right = n.right.add(key, value, less)
} else {
n.value = value
}
return n.rebalanceTree()
}
func (n *node[K, V]) remove(key K, less g.LessFn[K]) *node[K, V] {
if n == nil {
return nil
}
if g.Compare(key, n.key, less) < 0 {
n.left = n.left.remove(key, less)
} else if g.Compare(key, n.key, less) > 0 {
n.right = n.right.remove(key, less)
} else {
if n.left != nil && n.right != nil {
rightMinNode := n.right.findSmallest()
n.key = rightMinNode.key
n.value = rightMinNode.value
n.right = n.right.remove(rightMinNode.key, less)
} else if n.left != nil {
n = n.left
} else if n.right != nil {
n = n.right
} else {
n = nil
return n
}
}
return n.rebalanceTree()
}
func (n *node[K, V]) search(key K, less g.LessFn[K]) *node[K, V] {
if n == nil {
return nil
}
if g.Compare(key, n.key, less) < 0 {
return n.left.search(key, less)
} else if g.Compare(key, n.key, less) > 0 {
return n.right.search(key, less)
} else {
return n
}
}
func (n *node[K, V]) each(fn func(key K, val V)) {
if n == nil {
return
}
n.left.each(fn)
fn(n.key, n.value)
n.right.each(fn)
}
func (n *node[K, V]) getHeight() int {
if n == nil {
return 0
}
return n.height
}
func (n *node[K, V]) recalculateHeight() {
n.height = 1 + g.Max(n.left.getHeight(), n.right.getHeight())
}
func (n *node[K, V]) rebalanceTree() *node[K, V] {
if n == nil {
return n
}
n.recalculateHeight()
balanceFactor := n.left.getHeight() - n.right.getHeight()
if balanceFactor <= -2 {
if n.right.left.getHeight() > n.right.right.getHeight() {
n.right = n.right.rotateRight()
}
return n.rotateLeft()
} else if balanceFactor >= 2 {
if n.left.right.getHeight() > n.left.left.getHeight() {
n.left = n.left.rotateLeft()
}
return n.rotateRight()
}
return n
}
func (n *node[K, V]) rotateLeft() *node[K, V] {
newRoot := n.right
n.right = newRoot.left
newRoot.left = n
n.recalculateHeight()
newRoot.recalculateHeight()
return newRoot
}
func (n *node[K, V]) rotateRight() *node[K, V] {
newRoot := n.left
n.left = newRoot.right
newRoot.right = n
n.recalculateHeight()
newRoot.recalculateHeight()
return newRoot
}
func (n *node[K, V]) findSmallest() *node[K, V] {
if n.left != nil {
return n.left.findSmallest()
} else {
return n
}
}
func (n *node[K, V]) size() int {
if n == nil {
return 0
}
return 1 + n.left.size() + n.right.size()
}