Skip to content

Commit 9d88246

Browse files
committed
Finished delete function and added some prints functions too
1 parent f766bb4 commit 9d88246

File tree

1 file changed

+52
-18
lines changed

1 file changed

+52
-18
lines changed

data_structures/Binary Tree/binary_search_tree.py

+52-18
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,12 @@ def delete(self, label):
8585
self.__reassignNodes(node, node.getLeft())
8686
#Has two children
8787
else:
88+
#Gets the max value of the left branch
8889
tmpNode = self.getMax(node.getLeft())
90+
#Deletes the tmpNode
8991
self.delete(tmpNode.getLabel())
90-
self.__reassignNodes(node, tmpNode)
92+
#Assigns the value to the node to delete and keesp tree structure
93+
node.setLabel(tmpNode.getLabel())
9194

9295
def getNode(self, label):
9396
curr_node = None
@@ -135,11 +138,13 @@ def empty(self):
135138
return True
136139
return False
137140

138-
def preShow(self, curr_node):
141+
def __InOrderTraversal(self, curr_node):
142+
nodeList = []
139143
if curr_node is not None:
140-
print(curr_node.getLabel())
141-
self.preShow(curr_node.getLeft())
142-
self.preShow(curr_node.getRight())
144+
nodeList.insert(0, curr_node)
145+
nodeList = nodeList + self.__InOrderTraversal(curr_node.getLeft())
146+
nodeList = nodeList + self.__InOrderTraversal(curr_node.getRight())
147+
return nodeList
143148

144149
def getRoot(self):
145150
return self.root
@@ -149,19 +154,44 @@ def __isRightChildren(self, node):
149154
return True
150155
return False
151156

152-
def __reassignNodes(self,node, newChildren):
157+
def __reassignNodes(self, node, newChildren):
153158
if(newChildren is not None):
154159
newChildren.setParent(node.getParent())
155160
if(node.getParent() is not None):
156161
#If it is the Right Children
157-
if(self.__isRightChildren(node)):a
158-
node.getParent().setRight(newChildren)
162+
if(self.__isRightChildren(node)):
163+
node.getParent().setRight(newChildren)
159164
else:
160165
#Else it is the left children
161166
node.getParent().setLeft(newChildren)
167+
168+
#This function traversal the tree. By default it returns an
169+
#In order traversal list. You can pass a function to traversal
170+
#The tree as needed by client code
171+
def traversalTree(self, traversalFunction = None, root = None):
172+
if(traversalFunction is None):
173+
#Returns a list of nodes in preOrder by default
174+
return self.__InOrderTraversal(self.root)
162175
else:
163-
#It is the root of the tree
164-
node.setLabel(newChildren.getLabel())
176+
#Returns a list of nodes in the order that the users wants to
177+
return traversalFunction(self.root)
178+
179+
#Returns an string of all the nodes labels in the list
180+
#In Order Traversal
181+
def __str__(self):
182+
list = self.__InOrderTraversal(self.root)
183+
str = ""
184+
for x in list:
185+
str = str + " " + x.getLabel().__str__()
186+
return str
187+
188+
def InPreOrder(curr_node):
189+
nodeList = []
190+
if curr_node is not None:
191+
nodeList = nodeList + InPreOrder(curr_node.getLeft())
192+
nodeList.insert(0, curr_node.getLabel())
193+
nodeList = nodeList + InPreOrder(curr_node.getRight())
194+
return nodeList
165195

166196
def testBinarySearchTree():
167197
'''
@@ -179,16 +209,12 @@ def testBinarySearchTree():
179209
Example After Deletion
180210
7
181211
/ \
182-
3 14
183-
/ \
184-
1 6
185-
/
186-
4
212+
1 4
213+
187214
'''
188215
t = BinarySearchTree()
189216
t.insert(8)
190217
t.insert(3)
191-
t.insert(10)
192218
t.insert(6)
193219
t.insert(1)
194220
t.insert(10)
@@ -197,7 +223,8 @@ def testBinarySearchTree():
197223
t.insert(4)
198224
t.insert(7)
199225

200-
t.preShow(t.getRoot())
226+
#Prints all the elements of the list in order traversal
227+
print(t.__str__())
201228

202229
if(t.getNode(6) is not None):
203230
print("The label 6 exists")
@@ -216,8 +243,15 @@ def testBinarySearchTree():
216243
t.delete(13)
217244
t.delete(10)
218245
t.delete(8)
246+
t.delete(3)
247+
t.delete(6)
248+
t.delete(14)
219249

220-
t.preShow(t.getRoot())
250+
#Gets all the elements of the tree In pre order
251+
#And it prints them
252+
list = t.traversalTree(InPreOrder, t.root)
253+
for x in list:
254+
print(x)
221255

222256
if __name__ == "__main__":
223257
testBinarySearchTree()

0 commit comments

Comments
 (0)