-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST_Iter.py
316 lines (282 loc) · 12.1 KB
/
BST_Iter.py
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
306
307
308
309
310
311
312
313
314
315
316
"""
Iterative version
Contains methods for tree traversal.
This version assumes that keys of all nodes in a tree are different, that they don't repeat.
"""
from Node import Node
from collections import deque
class BinarySearchTree(object):
def __init__(self, root):
"""Expects a root node object as input."""
self.root = root
def __str__(self):
return "Root = {}".format(repr(self.root))
def __repr__(self):
"""Useful for debugging.
The first part returns the original value of __repr__(), which contains type of the object and its address in memory.
The second part gives us information about the root of the tree, which is its key, because it calls its __repr__() method.
"""
return super(BinarySearchTree, self).__repr__() + " Root = {}".format(repr(self.root))
def getRoot(self):
return self.root
def inOrder(self):
"""Returns the whole tree (nodes' keys) in in-order traversal, as a list. Has no input parameters. Works iteratively."""
current = self.root
if not current:
return []
self.result = []
stack = [] # stack contains nodes
while True:
while current:
stack.append(current)
current = current.getLeftChild()
if stack:
current = stack.pop()
self.result.append(current.getKey()) # visit()
current = current.getRightChild()
else:
return self.result
def preOrder(self):
"""Returns the whole tree (nodes' keys) in pre-order traversal, as a list. Has no input parameters. Works iteratively."""
root = self.root
self.result = []
stack = [root] # stack contains nodes
while stack:
current = stack.pop()
self.result.append(current.getKey()) # visit()
if current.getRightChild():
stack.append(current.getRightChild())
if current.getLeftChild():
stack.append(current.getLeftChild())
return self.result
def postOrder(self):
"""Returns the whole tree (nodes' keys) in post-order traversal, as a list. Has no input parameters. Works iteratively."""
self.result = []
stack = [] # stack contains nodes
boolStack = [] # For each element on the node stack, a corresponding value is stored on the bool stack. If this value is true, then we need to pop and visit the node on next encounter.
current = self.root
while current:
stack.append(current)
boolStack.append(False)
current = current.getLeftChild()
while stack:
current = stack[-1]
alreadyEncountered = boolStack[-1]
if alreadyEncountered:
self.result.append(current.getKey()) # visit()
stack.pop()
boolStack.pop()
else:
boolStack.pop()
boolStack.append(True)
current = current.getRightChild()
while current:
stack.append(current)
boolStack.append(False)
current = current.getLeftChild()
return self.result
def BFS(self):
"""Returns the whole tree (nodes' keys) in level-order traversal, as a list. Has no input parameters. Works iteratively."""
root = self.root
self.result = []
queue = deque([root]) # queue contains nodes
while queue:
current = queue.popleft()
self.result.append(current.getKey()) # visit()
if current.getLeftChild():
queue.append(current.getLeftChild())
if current.getRightChild():
queue.append(current.getRightChild())
return self.result
def find(self, key):
"""Inputs: key is a numerical value.
Returns a node object - if the key (value) is found exactly, than it returns that node, otherwise it returns the node under which the searched key should be.
"""
# Only in the first iteration will the root be the root node object; in subsequent iterations it will actually be a node object, but not the root anymore.
root = self.root
while root:
if root.getKey() == key:
return root
elif root.getKey() > key:
if root.getLeftChild():
root = root.getLeftChild()
continue
return root
else: # root.getKey() < key:
if root.getRightChild():
root = root.getRightChild()
continue
return root
def next(self, node):
"""Input is a node object.
Outputs the next node object in terms of key value, or None, if we input the largest one.
"""
if node.getRightChild():
return self.leftDescendant(node.getRightChild())
else:
return self.rightAncestor(node)
def leftDescendant(self, node):
if not node.getLeftChild():
return node
else:
return self.leftDescendant(node.getLeftChild())
def rightAncestor(self, node):
if node.getParent():
if node.getKey() < node.getParent().getKey():
return node.getParent()
else:
return self.rightAncestor(node.getParent())
else:
return None
def previous(self, node):
"""Input is a node object.
Outputs the previous node object in terms of key value, or None, if we input the smallest one.
"""
if node.getLeftChild():
return self.rightDescendant(node.getLeftChild())
else:
return self.leftAncestor(node)
def rightDescendant(self, node):
if not node.getRightChild():
return node
else:
return self.rightDescendant(node.getRightChild())
def leftAncestor(self, node):
if node.getParent():
if node.getKey() > node.getParent().getKey():
return node.getParent()
else:
return self.leftAncestor(node.getParent())
else:
return None
def rangeSearch(self, x, y):
"""Inputs: numbers x, y
Output: A list of nodes with key between x and y
"""
L = []
node = self.find(x)
while node and node.getKey() <= y:
if node.getKey() >= x:
L.append(node)
node = self.next(node)
return L
def insert(self, key):
"""Inputs: key is a numerical value.
Adds node with key key to the tree. Returns nothing.
"""
parent = self.find(key)
node = Node(key)
node.setParent(parent)
if key < parent.getKey():
parent.setLeftChild(node)
else:
parent.setRightChild(node)
while parent:
parent.incrementSize()
parent = parent.getParent()
def delete(self, node):
"""Input: node object
Removes the node from the tree.
Returns nothing.
"""
parent = node.getParent()
left = node.getLeftChild()
right = node.getRightChild()
if not right: # node doesn't have right child
if not left and not parent: # "node" is the only node in the tree
return None # Returns nothing. We could set self.root to be None, but we would have a problem with printing (traversing) it, unless we changed the traversing methods - so, this leaves it in the tree
if left:
left.setParent(parent)
if parent:
if node.getKey() < parent.getKey(): # node is left child
parent.setLeftChild(left)
elif node.getKey() > parent.getKey(): # node is right child
parent.setRightChild(left)
else: # We're deleting the root.
self.root = left # Left becomes the new root.
else: # node has right child
X = self.next(node) # meaning, x doesn't have left child
Y = X.getRightChild()
pX = X.getParent()
if Y:
if X != right:
Y.setParent(pX)
pX.setLeftChild(Y)
else:
pX.setLeftChild(None)
if X != right:
X.setRightChild(right)
right.setParent(X)
X.setParent(parent)
if parent: # We're not deleting the root.
if node.getKey() < parent.getKey(): # node is left child
parent.setLeftChild(X)
elif node.getKey() > parent.getKey(): # node is right child
parent.setRightChild(X)
else: # We're deleting the root.
self.root = X # X becomes new root.
if left:
X.setLeftChild(left)
left.setParent(X)
node.setParent(None)
node.setLeftChild(None)
node.setRightChild(None)
del node
def rotateRight(self, node):
"""Input: A node object that we want to rotate right.
Returns nothing.
"""
parent = node.getParent()
Y = node.getLeftChild()
if not Y:
return None # we can't rotate the node with nothing!
B = Y.getRightChild()
Y.setParent(parent)
if parent:
if node.getKey() < parent.getKey(): # node is left child
parent.setLeftChild(Y)
elif node.getKey() > parent.getKey(): # node is right child
parent.setRightChild(Y)
else:
self.root = Y
node.setParent(Y)
Y.setRightChild(node)
if B:
B.setParent(node)
node.setLeftChild(B)
def rotateLeft(self, node):
"""Input: A node object that we want to rotate left.
Returns nothing.
"""
# we're just rotating back now, to the left
parent = node.getParent()
X = node.getRightChild()
if not X:
return None # we can't rotate the node with nothing!
B = X.getLeftChild()
X.setParent(parent)
if parent:
if node.getKey() < parent.getKey(): # node is left child
parent.setLeftChild(X)
elif node.getKey() > parent.getKey(): # node is right child
parent.setRightChild(X)
else:
self.root = X
node.setParent(X)
X.setLeftChild(node)
if B:
B.setParent(node)
node.setRightChild(B)
def printTree(bst, verbose = False):
"""If boolean verbose is True, it will print(all nodes, in BFS order.
"""
print()
print("In order: ", bst.inOrder())
print("Pre order: ", bst.preOrder())
print("BFS: ", bst.BFS())
if verbose:
print("Nodes (in BFS order):")
nodes = bst.BFS()
for node in nodes:
bst.find(node).printNode()
print()