-
Notifications
You must be signed in to change notification settings - Fork 13
/
delete.go
269 lines (243 loc) · 4.63 KB
/
delete.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
260
261
262
263
264
265
266
267
268
269
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2020 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package avl
// delete: tree balancer
func balanceLeft(pp **Node) bool {
h := true
p := *pp
// h; left branch has shrunk
if -1 == p.balance {
p.balance = 0
} else if 0 == p.balance {
p.balance = 1
h = false
} else { // balance = 1, rebalance
p1 := p.right
if p1.balance >= 0 {
// single RR rotation
p.right = p1.left
p1.left = p
if 0 == p1.balance {
p.balance = 1
p1.balance = -1
h = false
} else {
p.balance = 0
p1.balance = 0
}
nn := 1 + p.leftNodes + p1.leftNodes
p.rightNodes = p1.leftNodes
p1.leftNodes = nn
p1.up = p.up
p.up = p1
if nil != p.right {
p.right.up = p
}
*pp = p1
} else {
// double RL rotation
p2 := p1.left
p1.left = p2.right
p2.right = p1
p.right = p2.left
p2.left = p
if +1 == p2.balance {
p.balance = -1
} else {
p.balance = 0
}
if -1 == p2.balance {
p1.balance = 1
} else {
p1.balance = 0
}
p2.balance = 0
nl := 1 + p.leftNodes + p2.leftNodes
nr := 1 + p2.rightNodes + p1.rightNodes
p.rightNodes = p2.leftNodes
p1.leftNodes = p2.rightNodes
p2.leftNodes = nl
p2.rightNodes = nr
p2.up = p.up
if nil != p.right {
p.right.up = p
}
if nil != p1.left {
p1.left.up = p1
}
p.up = p2
p1.up = p2
*pp = p2
}
}
return h
}
// delete: tree balancer
func balanceRight(pp **Node) bool {
h := true
p := *pp
// h; right branch has shrunk
if 1 == p.balance {
p.balance = 0
} else if 0 == p.balance {
p.balance = -1
h = false
} else { // balance = -1, rebalance
p1 := p.left
if p1.balance <= 0 {
// single LL rotation
p.left = p1.right
p1.right = p
if 0 == p1.balance {
p.balance = -1
p1.balance = 1
h = false
} else {
p.balance = 0
p1.balance = 0
}
nn := 1 + p1.rightNodes + p.rightNodes
p.leftNodes = p1.rightNodes
p1.rightNodes = nn
p1.up = p.up
p.up = p1
if nil != p.left {
p.left.up = p
}
*pp = p1
} else {
// double LR rotation
p2 := p1.right
p1.right = p2.left
p2.left = p1
p.left = p2.right
p2.right = p
if -1 == p2.balance {
p.balance = 1
} else {
p.balance = 0
}
if +1 == p2.balance {
p1.balance = -1
} else {
p1.balance = 0
}
p2.balance = 0
nl := 1 + p1.leftNodes + p2.leftNodes
nr := 1 + p2.rightNodes + p.rightNodes
p1.rightNodes = p2.leftNodes
p.leftNodes = p2.rightNodes
p2.leftNodes = nl
p2.rightNodes = nr
p2.up = p.up
if nil != p.left {
p.left.up = p
}
if nil != p1.right {
p1.right.up = p1
}
p.up = p2
p1.up = p2
*pp = p2
}
}
return h
}
// delete: rearrange deleted node
func del(qq **Node, rr **Node) bool {
h := false
if nil != (*rr).right {
h = del(qq, &(*rr).right)
(*rr).rightNodes -= 1
if h {
h = balanceRight(rr)
}
} else {
q := *qq
r := *rr
rl := r.left
if nil != rl {
rl.up = r.up
}
if r != q.left {
r.left = q.left
r.leftNodes = q.leftNodes - 1
}
r.right = q.right
r.up = q.up
r.balance = q.balance
r.rightNodes = q.rightNodes
if nil != r.right {
r.right.up = r
}
if nil != r.left {
r.left.up = r
}
*qq = r
*rr = rl
h = true
}
return h
}
// Delete - removes a specific item from the tree
func (tree *Tree) Delete(key Item) interface{} {
value, removed, _ := delete(key, &tree.root)
if removed {
tree.count -= 1
}
return value
}
// internal delete routine
func delete(key Item, pp **Node) (interface{}, bool, bool) {
h := false
if nil == *pp { // key not in tree
return nil, false, h
}
value := interface{}(nil)
removed := false
switch (*pp).key.Compare(key) {
case +1: // (*pp).key > key
value, removed, h = delete(key, &(*pp).left)
if removed {
(*pp).leftNodes -= 1
}
if h {
h = balanceLeft(pp)
}
case -1: // (*pp).key < key
value, removed, h = delete(key, &(*pp).right)
if removed {
(*pp).rightNodes -= 1
}
if h {
h = balanceRight(pp)
}
default: // found: delete p
q := *pp
value = q.value // preserve the value part
if nil == q.right {
if nil != q.left {
q.left.up = q.up
}
*pp = q.left
h = true
} else if nil == q.left {
if nil != q.right {
q.right.up = q.up
}
*pp = q.right
h = true
} else {
h = del(pp, &q.left)
(*pp).left = q.left // p has changed, but q.left has left link value
if h {
h = balanceLeft(pp)
}
}
freeNode(q) // return deleted node to pool
removed = true // indicate that an item was removed
}
return value, removed, h
}