-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest BST Iterative.py
245 lines (205 loc) · 7.31 KB
/
Test BST Iterative.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
"""
Iterative version
This version assumes that keys of all nodes in a tree are different, that they don't repeat.
Uncomment desired test calls.
"""
import BST_Iter as BST
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()
def createTree():
"""We can do it like this..."""
n1 = BST.Node(1)
n4 = BST.Node(4)
n6 = BST.Node(6)
n7 = BST.Node(7) # root
n10 = BST.Node(10)
n13 = BST.Node(13)
n15 = BST.Node(15)
n1.setParent(n4)
n6.setParent(n4)
n4.setParent(n7)
n10.setParent(n13)
n15.setParent(n13)
n13.setParent(n7)
n4.setLeftChild(n1)
n4.setRightChild(n6)
n7.setLeftChild(n4)
n7.setRightChild(n13)
n13.setLeftChild(n10)
n13.setRightChild(n15)
bst = BST.BinarySearchTree(n7)
root = n7
return bst, root
def createTree():
"""...But it's definitely better this way!"""
# We must create the root node first!
n7 = BST.Node(7) # root
# Then we create the BST tree with that root node.
root = n7
bst = BST.BinarySearchTree(root)
# Only now can we add other nodes; we couldn't before creating the BST tree! We must use insert() method!
bst.insert(4)
bst.insert(1)
bst.insert(6)
bst.insert(13)
bst.insert(15)
bst.insert(10)
return bst, root
def createTree():
"""...And probably the best this way!"""
# We must create the root node first!
# Then we create the BST tree with that root node.
bst = BST.BinarySearchTree(BST.Node(7))
# Only now can we add other nodes; we couldn't before creating the BST tree! We must use insert() method!
bst.insert(4)
bst.insert(1)
bst.insert(6)
bst.insert(13)
bst.insert(15)
bst.insert(10)
return bst, bst.getRoot()
def testTree():
bst, root = createTree()
print("\nPrint:")
print("In order: ", bst.inOrder())
print("Pre order: ", bst.preOrder())
print("Post order:", bst.postOrder())
print("BFS: ", bst.BFS())
print("Root:", end=' ')
root.printNode()
print("\nFind:")
print(0, bst.find(0))
print(1, bst.find(1))
print(2, bst.find(2))
print(5, bst.find(5))
print(6, bst.find(6))
print(7, bst.find(7))
print(8, bst.find(8))
print(12, bst.find(12))
print(13, bst.find(13))
print(14, bst.find(14))
print(15, bst.find(15))
print(20, bst.find(20))
print("\nNext:")
print(0, bst.next(bst.find(0)))
print(1, bst.next(bst.find(1)))
print(2, bst.next(bst.find(2)))
print(4, bst.next(bst.find(4)))
print(5, bst.next(bst.find(5)))
print(6, bst.next(bst.find(6)))
print(7, bst.next(bst.find(7)))
print(8, bst.next(bst.find(8)))
print(10, bst.next(bst.find(10)))
print(12, bst.next(bst.find(12)))
print(14, bst.next(bst.find(14)))
print(15, bst.next(bst.find(15)))
print(16, bst.next(bst.find(16)))
print("\nPrevious:")
print(0, bst.previous(bst.find(0)))
print(1, bst.previous(bst.find(1)))
print(2, bst.previous(bst.find(2)))
print(4, bst.previous(bst.find(4)))
print(5, bst.previous(bst.find(5)))
print(6, bst.previous(bst.find(6)))
print(7, bst.previous(bst.find(7)))
print(8, bst.previous(bst.find(8)))
print(10, bst.previous(bst.find(10)))
print(12, bst.previous(bst.find(12)))
print(14, bst.previous(bst.find(14)))
print(15, bst.previous(bst.find(15)))
print(16, bst.previous(bst.find(16)))
print("\nRange search:")
print(5, 12)
for node in bst.rangeSearch(5, 12):
print(node, end=' ')
print()
if 0: # put 1 to execute this
print("\nInsert:")
print("In order: ", bst.inOrder())
print("Pre order: ", bst.preOrder())
print("BFS: ", bst.BFS())
n = 3 # key of the node to insert
bst.insert(n)
node = bst.find(n)
node.printNode()
node.getParent().printNode()
print("Inserting", n)
print("In order: ", bst.inOrder())
print("Pre order: ", bst.preOrder())
print("BFS: ", bst.BFS())
print()
print("In order: ", bst.inOrder())
print("Pre order: ", bst.preOrder())
print("Post order:", bst.postOrder())
print("BFS: ", bst.BFS())
n = 5 # key of the node to insert
bst.insert(n)
node = bst.find(n)
node.printNode()
node.getParent().printNode()
print("Inserting", n)
print("In order: ", bst.inOrder())
print("Pre order: ", bst.preOrder())
print("Post order:", bst.postOrder())
print("BFS: ", bst.BFS())
if 0: # put 1 to execute this
print("\nDelete:")
print("In order: ", bst.inOrder())
print("Pre order: ", bst.preOrder())
print("BFS: ", bst.BFS())
n = 7 # key of the node to delete (make sure to try with the root node!)
bst.delete(bst.find(n)) # We don't need to reassign the root, even in the case when we're deleting the root node!
print("Deleting", n)
print("In order: ", bst.inOrder())
print("Pre order: ", bst.preOrder())
print("BFS: ", bst.BFS())
print("This is the node under which the deleted node, {}, would come: {}.".format(n, bst.find(n)))
bst.find(n).printNode()
try:
bst.find(n).getParent().printNode()
except:
print("New root:", end=' ')
bst.find(root.getKey()).printNode()
print("Root is:", end=' ')
bst.getRoot().printNode()
print("\nRotate right:")
printTree(bst, True)
n = 7 # key of the node to rotate (make sure to try with the root node!)
bst.rotateRight(bst.find(n)) # We don't need to reassign the root, even in the case when we're deleting the root node!
print("Rotating right", n)
printTree(bst, True)
bst.find(n).printNode()
print("\nRotate left:")
printTree(bst, True)
n = 1 # key of the node to rotate (make sure to try with the root node!)
bst.rotateLeft(bst.find(n)) # We don't need to reassign the root, even in the case when we're deleting the root node!
print("Rotating left", n)
printTree(bst, True)
bst.find(n).printNode()
def test1():
bst = BST.BinarySearchTree(BST.Node(3)) # root
bst.insert(1)
bst.insert(4)
bst.insert(5)
printTree(bst, True)
bst.delete(bst.find(3))
printTree(bst, True)
def test2():
bst, root = createTree()
printTree(bst, True)
bst.delete(bst.find(7))
printTree(bst, True)
testTree()
#test1()
#test2()