-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST.py
343 lines (308 loc) · 10.4 KB
/
BST.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import time
from nodes import BinaryTreeNode as Node
import nodes
import Queue
class BST():
"""An instance of the BST contains a root of a tree"""
def __init__(self,root=None):
if root:
self.root = Node(root)
else:
self.root = None
def find(self,target):
return self.__contains(self.root,target)
def __contains(self,node,target):
if node == None:
return False
if node.value < target:
return self.__contains(node.rightChild,target)
elif node.value > target:
return self.__contains(node.leftChild,target)
else:
return True
def select(self,target):
return self.__getNode(self.root,target)
def __getNode(self,node,target):
if node == None:
return None
if node.value < target:
return self.__getNode(node.rightChild,target)
elif node.value > target:
return self.__getNode(node.leftChild,target)
else:
return node
def min(self,node=None):
if node:
if node.leftChild == None:
return node.value
return self.min(node.leftChild)
else:
node = self.root
if node.leftChild == None:
return node.value
return self.min(node.leftChild)
def minNode(self,node=None):
if node:
if node.leftChild == None:
return node
return self.minNode(node.leftChild)
else:
node = self.root
if node.leftChild == None:
return node
return self.minNode(node.leftChild)
def max(self,node=None):
if node:
if node.rightChild == None:
return node.value
return self.max(node.rightChild)
else:
node = self.root
if node.rightChild == None:
return node.value
return self.max(node.rightChild)
def maxNode(self,node=None):
if node:
if node.rightChild == None:
return node
return self.maxNode(node.rightChild)
else:
node = self.root
if node.rightChild == None:
return node
return self.maxNode(node.rightChild)
def prior(self,node):
"""node can either be a number or Node instance. If it is a number
it runs in O(log(height)) time.
Returns a number.
"""
check = type(node)
if check==int or check==float:
node = self.select(node)
if node.leftChild:
return self.max(node.leftChild)
parent = node.parent
while parent:
if parent.value < node.value:
return parent.value
parent = parent.parent
def priorNode(self,node):
"""node must be a Node instance.
Returns a Node instance."""
if node.leftChild:
return self.maxNode(node.leftChild)
parent = node.parent
while parent:
if parent.value < node.value:
return parent
parent = parent.parent
def next(self,node):
"""node can either be a number or Node instance. If it is a number
it runs in O(log(height)) time. Returns a number.
"""
check = type(node)
if check==int or check==float:
node = self.select(node)
if node.rightChild:
return self.min(node.rightChild)
parent = node.parent
while parent:
if parent.value > node.value:
return parent.value
parent = parent.parent
def nextNode(self,node):
"""node must be a Node instance.
Returns a Node instance.
"""
if node.rightChild:
return self.minNode(node.rightChild)
parent = node.parent
while parent:
if parent.value > node.value:
return parent
parent = parent.parent
def insert(self,value,subNode=None):
"""This needs work in order to handle double entries
"""
if subNode == None:
subNode = self.root
try:
if subNode.value < value:
if subNode.rightChild:
self.insert(value,subNode.rightChild)
else:
subNode.rightChild = Node(value)
subNode.rightChild.parent = subNode
elif subNode.value > value:
if subNode.leftChild:
self.insert(value,subNode.leftChild)
else:
subNode.leftChild = Node(value)
subNode.leftChild.parent = subNode
else:
return None
except AttributeError:
print "CREATED ROOT!"
self.root = Node(value)
def delete(self,node):
"""If node is a number we find the node instance corresponding to the
number. This runs in O(log(height)) time. If node is already a node instance
this runs in O(1) time
"""
check = type(node)
if check==int or check==float:
node = self.select(node)
if node.rightChild and node.leftChild:
predecessor = self.priorNode(node)
tempValue = predecessor.value
self.delete(predecessor)
node.value = tempValue
elif node.rightChild:
self.__replaceNodeWith(node,node.rightChild)
elif node.leftChild:
self.__replaceNodeWith(node,node.leftChild)
else:
self.__replaceNodeWith(node,None)
def __replaceNodeWith(self,node,child):
if node.parent.value > node.value:
node.parent.leftChild = child
else:
node.parent.rightChild = child
if child:
child.parent = node.parent
def height(self,node=None,height = 1):
if node==None:
node=self.root
if node.leftChild:
leftHeight = self.height(node.leftChild,height+1)
else:
leftHeight = height
if node.rightChild:
rightHeight = self.height(node.rightChild,height+1)
else:
rightHeight = height
if leftHeight > rightHeight:
return leftHeight
else:
return rightHeight
def rotateRight(self,node):
check = type(node)
if check==int or check==float:
node = self.select(node)
if node.leftChild==None:
print "Argument has no left child!"
return
if node == self.root:
self.root = node.leftChild
else:
self.__replaceNodeWith(node,node.leftChild)
#The actiual rotation:
pivot = node.leftChild
node.leftChild = pivot.rightChild
pivot.rightChild = node
node = pivot
#Fix the parental relations:
node.parent = node.rightChild.parent
node.rightChild.parent = node
if node.rightChild.leftChild:
node.rightChild.leftChild.parent = node.rightChild
def rotateLeft(self,node):
check = type(node)
if check==int or check==float:
node = self.select(node)
if node.rightChild==None:
print "Argument has no right child!"
return
if node == self.root:
self.root = node.rightChild
else:
self.__replaceNodeWith(node,node.rightChild)
#The actiual rotation:
pivot = node.rightChild
node.rightChild = pivot.leftChild
pivot.leftChild = node
node = pivot
#Fix the parental relations:
node.parent = node.leftChild.parent
node.leftChild.parent = node
if node.leftChild.rightChild:
node.leftChild.rightChild.parent = node.leftChild
def relations(self):
return self.__relations(self.root)
def __relations(self,node=None):
"""Returns a list of where each element lists a node and its
direct relatives: [node value, left child, right child, parent]
"""
if node == None:
return []
relationList = []
leftResults = self.__relations(node.leftChild)
if leftResults:
for element in leftResults:
relationList.append(element)
value = node.value
lc = -1
rc = -1
parent = -1
if node.leftChild:
lc = node.leftChild.value
if node.rightChild:
rc = node.rightChild.value
if node.parent:
parent = node.parent.value
relationList.append([value,lc,rc,parent])
rightResults = self.__relations(node.rightChild)
if rightResults:
for element in rightResults:
relationList.append(element)
return relationList
def printTree(self,node=None):
if node:
self.__printTree(node)
else:
self.__printTree(self.root)
def __printTree(self,node=None):
if node == None:
return
self.__printTree(node.leftChild)
print node.value
self.__printTree(node.rightChild)
def levelPrint(self):
currentLvl = Queue.Queue()
currentLvl.put(self.root)
nextLvl = Queue.Queue()
level = ""
while not currentLvl.empty():
node=currentLvl.get()
level += str(node.value) + " "
for child in node.getChildren():
if child:
nextLvl.put(child)
if currentLvl.empty():
print level
level = ""
while not nextLvl.empty():
currentLvl.put(nextLvl.get())
if __name__ == "__main__":
tree = BST(7)
root = tree.root
tree.insert(4)
tree.insert(1)
tree.insert(5)
tree.insert(8)
tree.insert(7.5)
tree.insert(6)
print 'Nodes in tree in order:'
tree.printTree(root)
print 'Tree ends'
tree.levelPrint()
#tree2 = BST(4)
#tree2.insert(7)
#tree2.insert(1)
#tree2.insert(8)
#tree2.insert(5)
#tree2.insert(6)
#tree2.insert(7.5)
#tree2.levelPrint()
tree3=BST()
tree3.insert(4)