-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathredblacktree.swift
305 lines (283 loc) · 8.37 KB
/
redblacktree.swift
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//
// redblacktree.swift
//
// Created by Michael Schmatz on 8/15/14.
// Copyright (c) 2014 Michael Schmatz. All rights reserved.
//
public class RedBlackTreeNode<K: Comparable, V> {
private var red: Bool = false
public var key: K! = nil
public var data: V? = nil
var right: RedBlackTreeNode<K,V>!
var left: RedBlackTreeNode<K,V>!
var parent: RedBlackTreeNode<K,V>!
init(tree: RedBlackTree<K, V>) {
right = tree.sentinel
left = tree.sentinel
parent = tree.sentinel
}
init() {
//This method is here to support the creation of a sentinel
}
}
public class RedBlackTree<K: Comparable, V> {
var root: RedBlackTreeNode<K,V>
let sentinel: RedBlackTreeNode<K,V>
init() {
sentinel = RedBlackTreeNode<K,V>()
sentinel.red = false
root = sentinel
}
public func insertKey(key: K, data:V?) {
let newNode = RedBlackTreeNode<K,V>(tree: self)
newNode.key = key
newNode.data = data
insertNode(newNode)
}
public func deleteKey(key: K) {
let nodeToDelete = findNode(rootNode: root, key: key)
if nodeToDelete !== sentinel {
deleteNode(nodeToDelete)
}
}
public func findKey(key: K) -> RedBlackTreeNode<K,V>? {
let foundNode = findNode(rootNode: root, key: key)
return foundNode === sentinel ? nil : foundNode
}
public func minimum(var rootNode: RedBlackTreeNode<K,V>) -> RedBlackTreeNode<K,V> {
while rootNode.left !== sentinel {
rootNode = rootNode.left
}
return rootNode
}
public func maximum(var rootNode: RedBlackTreeNode<K,V>) -> RedBlackTreeNode<K,V> {
while rootNode.right !== sentinel {
rootNode = rootNode.right
}
return rootNode
}
public func findNode(var #rootNode: RedBlackTreeNode<K,V>, key: K) -> RedBlackTreeNode<K,V> {
while rootNode !== sentinel && key != rootNode.key {
if key < rootNode.key {
rootNode = rootNode.left
} else {
rootNode = rootNode.right
}
}
return rootNode
}
public func successorOfNode(var node: RedBlackTreeNode<K,V>) -> RedBlackTreeNode<K,V> {
if node.right !== sentinel {
return minimum(node.right)
}
var successor = node.parent
while successor !== sentinel && node === successor.right {
node = successor
successor = successor.parent
}
return successor
}
public func predecessorOfNode(var node: RedBlackTreeNode<K,V>) -> RedBlackTreeNode<K,V> {
if node.left !== sentinel {
return minimum(node.left)
}
var successor = node.parent
while successor !== sentinel && node === successor.left {
node = successor
successor = successor.parent
}
return successor
}
public func insertNode(nodeToInsert: RedBlackTreeNode<K,V>) {
var y = sentinel
var x = root
while x !== sentinel {
y = x
if nodeToInsert.key < x.key {
x = x.left
} else {
x = x.right
}
}
nodeToInsert.parent = y
if y === sentinel {
root = nodeToInsert
} else if nodeToInsert.key < y.key {
y.left = nodeToInsert
} else {
y.right = nodeToInsert
}
nodeToInsert.left = sentinel
nodeToInsert.right = sentinel
nodeToInsert.red = true
insertFixup(nodeToInsert)
}
public func deleteNode(z: RedBlackTreeNode<K,V>) {
var y = z
var originallyRed = y.red
var x: RedBlackTreeNode<K,V>!
if z.left === sentinel {
x = z.right
transplant(z, v: z.right)
} else if z.right === sentinel {
x = z.left
transplant(z, v: z.left)
} else {
y = minimum(z.right)
originallyRed = y.red
x = y.right
if y.parent === z {
x.parent = y
} else {
transplant(y, v: y.right)
y.right = z.right
y.right.parent = y
}
transplant(z, v: y)
y.left = z.left
y.left.parent = y
y.red = z.red
}
if originallyRed == false {
deletionFixup(x)
}
}
private func insertFixup(var nodeToInsert: RedBlackTreeNode<K,V>) {
while nodeToInsert.parent.red {
if nodeToInsert.parent === nodeToInsert.parent.parent.left {
var y = nodeToInsert.parent.parent.right
if y.red {
nodeToInsert.parent.red = false
y.red = false
nodeToInsert.parent.parent.red = true
nodeToInsert = nodeToInsert.parent.parent
} else {
if nodeToInsert === nodeToInsert.parent.right {
nodeToInsert = nodeToInsert.parent
leftRotate(nodeToInsert)
}
nodeToInsert.parent.red = false
nodeToInsert.parent.parent.red = true
rightRotate(nodeToInsert.parent.parent)
}
} else {
var y = nodeToInsert.parent.parent.left
if y.red {
nodeToInsert.parent.red = false
y.red = false
nodeToInsert.parent.parent.red = true
nodeToInsert = nodeToInsert.parent.parent
} else {
if nodeToInsert === nodeToInsert.parent.left {
nodeToInsert = nodeToInsert.parent
rightRotate(nodeToInsert)
}
nodeToInsert.parent.red = false
nodeToInsert.parent.parent.red = true
leftRotate(nodeToInsert.parent.parent)
}
}
}
root.red = false
}
private func deletionFixup(var x: RedBlackTreeNode<K,V>) {
while x !== root && x.red == false {
if x === x.parent.left {
var w = x.parent.right
if w.red == true {
w.red = false
x.parent.red = true
leftRotate(x.parent)
w = x.parent.right
}
if w.left.red == false && w.right.red == false {
w.red = true
x = x.parent
} else {
if w.right.red == false {
w.left.red = false
w.red = true
rightRotate(w)
w = x.parent.right
}
w.red = x.parent.red
x.parent.red = false
w.right.red = false
leftRotate(x.parent)
x = root
}
} else {
var w = x.parent.left
if w.red == true {
w.red = false
x.parent.red = true
rightRotate(x.parent)
w = x.parent.left
}
if w.right.red == false && w.left.red == false {
w.red = true
x = x.parent
} else {
if w.left.red == false {
w.right.red = false
w.red = true
leftRotate(w)
w = x.parent.left
}
w.red = x.parent.red
x.parent.red = false
w.left.red = false
rightRotate(x.parent)
x = root
}
}
}
x.red = false
}
private func rightRotate(oldSubtreeRoot: RedBlackTreeNode<K,V>) {
let newSubtreeRoot = oldSubtreeRoot.left
oldSubtreeRoot.left = newSubtreeRoot.right //reassign g
if newSubtreeRoot.right !== sentinel {
newSubtreeRoot.right.parent = oldSubtreeRoot //reassign g's parent
}
newSubtreeRoot.parent = oldSubtreeRoot.parent //reassign the subtree's parent
if oldSubtreeRoot.parent === sentinel { //if the root is the root of the tree
root = newSubtreeRoot
} else if oldSubtreeRoot === oldSubtreeRoot.parent.right { //reassign the original root parent's child node
oldSubtreeRoot.parent.right = newSubtreeRoot
} else {
oldSubtreeRoot.parent.left = newSubtreeRoot
}
//establish parent/child relationship between old and new root nodes
newSubtreeRoot.right = oldSubtreeRoot
oldSubtreeRoot.parent = newSubtreeRoot
}
private func leftRotate(oldSubtreeRoot: RedBlackTreeNode<K,V>) {
let newSubtreeRoot = oldSubtreeRoot.right
oldSubtreeRoot.right = newSubtreeRoot.left
if newSubtreeRoot.left !== sentinel {
newSubtreeRoot.left.parent = oldSubtreeRoot
}
newSubtreeRoot.parent = oldSubtreeRoot.parent
if oldSubtreeRoot.parent === sentinel {
root = newSubtreeRoot
} else if oldSubtreeRoot === oldSubtreeRoot.parent.left {
oldSubtreeRoot.parent.left = newSubtreeRoot
} else {
oldSubtreeRoot.parent.right = newSubtreeRoot
}
newSubtreeRoot.left = oldSubtreeRoot
oldSubtreeRoot.parent = newSubtreeRoot
}
private func transplant(u: RedBlackTreeNode<K,V>, v: RedBlackTreeNode<K,V>) {
//Swaps two subtrees
if u.parent === sentinel {
root = v
} else if u === u.parent.left {
u.parent.left = v
} else {
u.parent.right = v
}
v.parent = u.parent
}
}