This repository has been archived by the owner on Nov 24, 2020. It is now read-only.
forked from ancientlore/go-avltree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
treeremove.go
259 lines (219 loc) · 6 KB
/
treeremove.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package avltree
type removeData struct {
lookingFor interface{}
compare CompareFunc
}
func findPredecessor(node *treeNode) *treeNode {
if node != nil {
pred := node.left
if pred != nil {
for pred.right != nil {
pred = pred.right
}
}
return pred
}
return nil
}
func remLeftSubBalance(node *treeNode, shorter bool) (*treeNode, bool) {
q := node.right // q: root of taller subtree
var w *treeNode
switch q.bal {
case equal:
node.bal = rightHigh
q.bal = leftHigh // q will be the new root node
node = rotateLeft(node)
shorter = false // next level not shorter
case rightHigh:
node.bal = equal
q.bal = equal // q will be the new root node
node = rotateLeft(node)
case leftHigh:
w = q.left
if w.bal == leftHigh {
q.bal = rightHigh
} else {
q.bal = equal
}
if w.bal == rightHigh {
node.bal = leftHigh
} else {
node.bal = equal
}
w.bal = equal // w will be the new root node
q = rotateRight(q)
node.right = q
node = rotateLeft(node)
}
return node, shorter
}
func remRightSubBalance(node *treeNode, shorter bool) (*treeNode, bool) {
q := node.left // q: root of taller subtree
var w *treeNode
switch q.bal {
case equal:
node.bal = leftHigh
q.bal = rightHigh // q will be the new root node
node = rotateRight(node)
shorter = false // next level not shorter
case leftHigh:
node.bal = equal
q.bal = equal // q will be the new root node
node = rotateRight(node)
case rightHigh:
w = q.right
if w.bal == rightHigh {
q.bal = leftHigh
} else {
q.bal = equal
}
if w.bal == leftHigh {
node.bal = rightHigh
} else {
node.bal = equal
}
w.bal = equal // w will be the new root node
q = rotateLeft(q)
node.left = q
node = rotateRight(node)
}
return node, shorter
}
func removePredecessor(node *treeNode, shorter bool) (*treeNode, bool) {
if node.right != nil {
node.right, shorter = removePredecessor(node.right, shorter)
if shorter { // left subtree was shortened
node, shorter = remRightBalance(node, shorter)
}
node.size = node.leftSize() + node.rightSize() + 1
} else {
node = remNode(node)
}
return node, shorter
}
func remLeftBalance(node *treeNode, shorter bool) (*treeNode, bool) {
switch node.bal {
case equal: // one subtree shortened
node.bal = rightHigh // now it's right high
shorter = false // overall tree same height
case leftHigh: // taller subtree shortened
node.bal = equal // now it's equal
case rightHigh: // shorter subtree shortened
node, shorter = remLeftSubBalance(node, shorter)
}
return node, shorter
}
func remRightBalance(node *treeNode, shorter bool) (*treeNode, bool) {
switch node.bal {
case equal: // one subtree shortened
node.bal = leftHigh // now it's left high
shorter = false // overall tree same height
case rightHigh: // taller subtree shortened
node.bal = equal // now it's equal
case leftHigh: // shorter subtree shortened
node, shorter = remRightSubBalance(node, shorter)
}
return node, shorter
}
func remNode(node *treeNode) *treeNode {
if node.left != nil {
node = node.left
} else if node.right != nil {
node = node.right
} else {
node = nil
}
return node
}
func (d *removeData) remove(node **treeNode, shorter *bool) interface{} {
*shorter = true // default: shorter
var ptr interface{}
ptr = nil
code := d.compare(d.lookingFor, (*node).value)
if code < 0 {
if (*node).left != nil {
ptr = d.remove(&((*node).left), shorter)
if *shorter && ptr != nil { // left subtree was shortened
*node, *shorter = remLeftBalance(*node, *shorter)
}
}
} else if code > 0 {
if (*node).right != nil {
ptr = d.remove(&((*node).right), shorter)
if *shorter && ptr != nil { // left subtree was shortened
*node, *shorter = remRightBalance(*node, *shorter)
}
}
} else {
ptr = (*node).value
if (*node).left != nil && (*node).right != nil { // do the switch to find the prev.
// node with only one subtree
pred := findPredecessor(*node)
(*node).value = pred.value
(*node).left, *shorter = removePredecessor((*node).left, *shorter)
if *shorter { // left subtree was shortened
*node, *shorter = remLeftBalance(*node, *shorter)
}
} else { // we found the node; it has 1 subtree
*node = remNode(*node)
}
}
if ptr != nil && *node != nil {
(*node).size = (*node).leftSize() + (*node).rightSize() + 1
}
return ptr
}
// Remove removes the element matching the given value.
func (t *Tree) Remove(ptr interface{}) interface{} {
if ptr != nil && t.root != nil {
d := &removeData{ptr, t.compare}
var shorter bool
return d.remove(&(t.root), &shorter)
}
return nil
}
func remove(node **treeNode, index int, shorter *bool) interface{} {
*shorter = true // default: shorter
var ptr interface{}
ptr = nil
if index < (*node).leftSize() {
if (*node).left != nil {
ptr = remove(&((*node).left), index, shorter)
if *shorter && ptr != nil { // left subtree was shortened
*node, *shorter = remLeftBalance(*node, *shorter)
}
}
} else if index == (*node).leftSize() {
ptr = (*node).value
if (*node).left != nil && (*node).right != nil { // do the switch to find the prev.
// node with only one subtree
pred := findPredecessor(*node)
(*node).value = pred.value
(*node).left, *shorter = removePredecessor((*node).left, *shorter)
if *shorter { // left subtree was shortened
*node, *shorter = remLeftBalance(*node, *shorter)
}
} else { // we found the node; it has 1 subtree
*node = remNode(*node)
}
} else {
if (*node).right != nil {
ptr = remove(&((*node).right), index-((*node).leftSize()+1), shorter)
if *shorter && ptr != nil { // left subtree was shortened
*node, *shorter = remRightBalance(*node, *shorter)
}
}
}
if ptr != nil && *node != nil {
(*node).size = (*node).leftSize() + (*node).rightSize() + 1
}
return ptr
}
// Remove removes the element at the given index
func (t *Tree) RemoveAt(index int) interface{} {
if t.root != nil && index < t.root.size && index >= 0 {
var shorter bool
return remove(&(t.root), index, &shorter)
}
return nil
}