Skip to content

Commit a41bb13

Browse files
committed
Updated Binary Tree Implementation
1 parent 9313b37 commit a41bb13

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

Tree/BinaryTree.py renamed to Tree/BinaryTreeLL.py

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def insertNodeBT(rootNode, newNode):
122122
return "New Value inserted sucessfully!"
123123

124124
def getFarthestNode(rootNode):
125+
# Time: O(n); Space: O(n)
125126
if not rootNode:
126127
return
127128
else:
@@ -137,6 +138,77 @@ def getFarthestNode(rootNode):
137138
# the last node is farthest among all
138139
return root.value
139140

141+
def deleteFarthestNode(rootNode, fNode):
142+
# Time: O(n); Space: O(n)
143+
if not rootNode:
144+
return
145+
146+
else:
147+
customQueue = Queue()
148+
customQueue.enqueue(rootNode)
149+
while not customQueue.isEmpty():
150+
root = customQueue.dequeue()
151+
152+
if root.value is fNode:
153+
root.value = None
154+
return
155+
if root.value.rightChild:
156+
if root.value.rightChild is fNode:
157+
root.value.rightChild = None
158+
return
159+
else:
160+
customQueue.enqueue(root.value.rightChild)
161+
162+
if root.value.leftChild:
163+
if root.value.leftChild is fNode:
164+
root.value.leftChild = None
165+
return
166+
else:
167+
customQueue.enqueue(root.value.leftChild)
168+
169+
def deleteNodeBT(rootNode, node):
170+
# We use LevelOrderTraversal to search node
171+
# If we found the node to delete
172+
# We get the farthest node and
173+
# Replace node to delete with farthest node
174+
# And delete farthest node
175+
# Note: We can't directly delete the given node since
176+
# Other nodes linked with it also gets deleted
177+
# Time: O(n); Space: O(n)
178+
if not rootNode:
179+
return "The BT does not exist"
180+
else:
181+
customQueue = Queue()
182+
customQueue.enqueue(rootNode)
183+
while not customQueue.isEmpty():
184+
root = customQueue.dequeue()
185+
if root.value.data == node:
186+
dNode = getFarthestNode(rootNode)
187+
root.value.data = dNode.data
188+
deleteFarthestNode(rootNode, dNode)
189+
return "The node has been sucesfully deleted"
190+
191+
if root.value.leftChild is not None:
192+
customQueue.enqueue(root.value.leftChild)
193+
if root.value.rightChild is not None:
194+
customQueue.enqueue(root.value.rightChild)
195+
return "Failed to delete"
196+
197+
def deleteEntireBT(rootNode):
198+
# We have implemented BT with linked list
199+
# Simply make rootNode -> None
200+
# And left child and right child of rootNode both -> None
201+
# This will delete entire BT by garbage collector
202+
# Time: O(1); Space: O(1)
203+
if not rootNode:
204+
return "The BT does not exist"
205+
206+
rootNode.data = None
207+
rootNode.leftChild = None
208+
rootNode.rightChild = None
209+
210+
return "Sucessfully deletion of entire BT."
211+
140212
preOrderTraversal(BT)
141213
print("###########")
142214
inOrderTraversal(BT)
@@ -153,4 +225,15 @@ def getFarthestNode(rootNode):
153225
levelOrderTraversal(BT)
154226
print("#########")
155227
farthestNode = getFarthestNode(BT)
156-
print(farthestNode.data)
228+
print(farthestNode.data)
229+
print("######")
230+
# deleteFarthestNode(BT, farthestNode)
231+
# levelOrderTraversal(BT)
232+
# print("####")
233+
234+
deleteNodeBT(BT, 'Mobile')
235+
levelOrderTraversal(BT)
236+
# Deletion of Entire BT
237+
print("##########")
238+
deleteEntireBT(BT)
239+
levelOrderTraversal(BT)

Tree/BinaryTreePL.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
"Creation of Binary Tree using Python List"
3+
class BinaryTree:
4+
def __init__(self, size) -> None:
5+
self.customList = size * [None]
6+
self.lastUsedIndex = 0
7+
self.maxSize = size
8+
9+
def insertNode(self, value):
10+
# Two Case: Either BT is full or there is vacant place to insert new node
11+
# We use level order traversal to search for the first vacant place.
12+
# And add using lastUsedIndex
13+
if self.lastUsedIndex + 1 == self.maxSize:
14+
return "The Binary Tree is full"
15+
16+
self.customList[self.lastUsedIndex+1] = value
17+
self.lastUsedIndex += 1
18+
return "The value has been successfully inserted"
19+
20+
def searchNode(self, nodeValue):
21+
# Search through the list
22+
# if found return Sucess message
23+
# TIme: O(n); Space: O(1)
24+
for i in range(len(self.customList)):
25+
if self.customList[i] == nodeValue:
26+
return "Success"
27+
return "Value not found"
28+
29+
BT = BinaryTree(size=8)
30+
print(BT.insertNode("Gadgets"))
31+
print(BT.insertNode("Ipad"))
32+
print(BT.insertNode("Mobile"))
33+
print(BT.insertNode("IpadEarphone"))
34+
print("####")
35+
print(BT.searchNode("Mobile"))
36+

0 commit comments

Comments
 (0)