-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBinaryTreeLL.py
239 lines (209 loc) · 7.17 KB
/
BinaryTreeLL.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
'''
Creation of Binary Tree using Linked List
- Creation of Tree
- Insertion of a node
- Deletion of a node
- Search for a value
- Traverse all nodes -> 4 ways of traversing
- Deletion of tree
* Traversal of Binary Tree
- Depth First Search
- Preorder traversal
- Inorder traversal
- Post order traversal
- Breadth first search
- Level order traversal
'''
from QueueLinkedList import Queue
class TreeNode:
def __init__(self, data) -> None:
self.data = data
self.leftChild = None
self.rightChild = None
BT = TreeNode("Gadgets")
leftChild = TreeNode("IPad")
leftChild.leftChild = TreeNode("IPadCharger")
leftChild.rightChild = TreeNode("IPadEarphone")
rightChild = TreeNode("Mobile")
rightChild.rightChild = TreeNode("MobileCharger")
BT.leftChild = leftChild
BT.rightChild = rightChild
# Time: O(n) ; Space: O(n)
def preOrderTraversal(rootNode):
if not rootNode:
return
print(rootNode.data)
preOrderTraversal(rootNode.leftChild)
preOrderTraversal(rootNode.rightChild)
# Time: O(n); Space: O(n)
def inOrderTraversal(rootNode):
if not rootNode:
return
inOrderTraversal(rootNode.leftChild)
print(rootNode.data)
inOrderTraversal(rootNode.rightChild)
# Time: O(n); Space: O(n)
def postOrderTraversal(rootNode):
if not rootNode:
return
postOrderTraversal(rootNode.leftChild)
postOrderTraversal(rootNode.rightChild)
print(rootNode.data)
# Time: O(n); Space: O(n)
def levelOrderTraversal(rootNode):
if not rootNode:
return
else:
customQueue = Queue()
customQueue.enqueue(rootNode)
while not customQueue.isEmpty():
root = customQueue.dequeue()
print(root.value.data)
if (root.value.leftChild is not None):
customQueue.enqueue(root.value.leftChild)
if (root.value.rightChild is not None):
customQueue.enqueue(root.value.rightChild)
# Time: O(n); Space: O(n)
def searchValueBT(rootNode, nodeValue):
if not rootNode:
return "The Binary Tree does not exist"
else:
customQueue = Queue()
customQueue.enqueue(rootNode)
while not customQueue.isEmpty():
root = customQueue.dequeue()
if root.value.data == nodeValue:
return "Value found Sucessfully!"
if (root.value.leftChild is not None):
customQueue.enqueue(root.value.leftChild)
if (root.value.rightChild is not None):
customQueue.enqueue(root.value.rightChild)
return "Value not found!"
# Insert a node in Binary Tree
# we do level order traversal to insert node
# Since it is queue and it is efficient than recursion(PreOrder, InOrder, PostOrder)
# Case1: A root node is blank
# Case2: The tree exists and we have to look for a first vacant place
# Time: O(n); Space: O(n)
def insertNodeBT(rootNode, newNode):
if not rootNode:
rootNode = newNode
else:
customQueue = Queue()
customQueue.enqueue(rootNode)
while not customQueue.isEmpty():
root = customQueue.dequeue()
if root.value.leftChild is not None:
customQueue.enqueue(root.value.leftChild)
else:
root.value.leftChild = newNode
return "New Value inserted sucessfully!"
if root.value.rightChild is not None:
customQueue.enqueue(root.value.rightChild)
else:
root.value.rightChild = newNode
return "New Value inserted sucessfully!"
def getFarthestNode(rootNode):
# Time: O(n); Space: O(n)
if not rootNode:
return
else:
customQueue = Queue()
customQueue.enqueue(rootNode)
while not customQueue.isEmpty():
root = customQueue.dequeue()
if root.value.leftChild is not None:
customQueue.enqueue(root.value.leftChild)
if root.value.rightChild is not None:
customQueue.enqueue(root.value.rightChild)
# the last node is farthest among all
return root.value
def deleteFarthestNode(rootNode, fNode):
# Time: O(n); Space: O(n)
if not rootNode:
return
else:
customQueue = Queue()
customQueue.enqueue(rootNode)
while not customQueue.isEmpty():
root = customQueue.dequeue()
if root.value is fNode:
root.value = None
return
if root.value.rightChild:
if root.value.rightChild is fNode:
root.value.rightChild = None
return
else:
customQueue.enqueue(root.value.rightChild)
if root.value.leftChild:
if root.value.leftChild is fNode:
root.value.leftChild = None
return
else:
customQueue.enqueue(root.value.leftChild)
def deleteNodeBT(rootNode, node):
# We use LevelOrderTraversal to search node
# If we found the node to delete
# We get the farthest node and
# Replace node to delete with farthest node
# And delete farthest node
# Note: We can't directly delete the given node since
# Other nodes linked with it also gets deleted
# Time: O(n); Space: O(n)
if not rootNode:
return "The BT does not exist"
else:
customQueue = Queue()
customQueue.enqueue(rootNode)
while not customQueue.isEmpty():
root = customQueue.dequeue()
if root.value.data == node:
dNode = getFarthestNode(rootNode)
root.value.data = dNode.data
deleteFarthestNode(rootNode, dNode)
return "The node has been sucesfully deleted"
if root.value.leftChild is not None:
customQueue.enqueue(root.value.leftChild)
if root.value.rightChild is not None:
customQueue.enqueue(root.value.rightChild)
return "Failed to delete"
def deleteEntireBT(rootNode):
# We have implemented BT with linked list
# Simply make rootNode -> None
# And left child and right child of rootNode both -> None
# This will delete entire BT by garbage collector
# Time: O(1); Space: O(1)
if not rootNode:
return "The BT does not exist"
rootNode.data = None
rootNode.leftChild = None
rootNode.rightChild = None
return "Sucessfully deletion of entire BT."
preOrderTraversal(BT)
print("###########")
inOrderTraversal(BT)
print("#######")
postOrderTraversal(BT)
print("############")
levelOrderTraversal(BT)
print("##########")
print(searchValueBT(BT, "MobileCharger"))
print("##########")
# Insert new node in BT
newNode = TreeNode("MobileEarphone")
print(insertNodeBT(BT, newNode))
levelOrderTraversal(BT)
print("#########")
farthestNode = getFarthestNode(BT)
print(farthestNode.data)
print("######")
# deleteFarthestNode(BT, farthestNode)
# levelOrderTraversal(BT)
# print("####")
deleteNodeBT(BT, 'Mobile')
levelOrderTraversal(BT)
# Deletion of Entire BT
print("##########")
deleteEntireBT(BT)
levelOrderTraversal(BT)