mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 13:31:07 +00:00
Merge pull request #200 from pablo9891/master
Added delete function to binary_search_tree.py and print function
This commit is contained in:
commit
9dfd0a6e73
|
@ -1,13 +1,14 @@
|
||||||
'''
|
'''
|
||||||
A binary search Tree
|
A binary search Tree
|
||||||
'''
|
'''
|
||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
|
|
||||||
def __init__(self, label):
|
def __init__(self, label, parent):
|
||||||
self.label = label
|
self.label = label
|
||||||
self.left = None
|
self.left = None
|
||||||
self.right = None
|
self.right = None
|
||||||
|
#Added in order to delete a node easier
|
||||||
|
self.parent = parent
|
||||||
|
|
||||||
def getLabel(self):
|
def getLabel(self):
|
||||||
return self.label
|
return self.label
|
||||||
|
@ -27,6 +28,11 @@ class Node:
|
||||||
def setRight(self, right):
|
def setRight(self, right):
|
||||||
self.right = right
|
self.right = right
|
||||||
|
|
||||||
|
def getParent(self):
|
||||||
|
return self.parent
|
||||||
|
|
||||||
|
def setParent(self, parent):
|
||||||
|
self.parent = parent
|
||||||
|
|
||||||
class BinarySearchTree:
|
class BinarySearchTree:
|
||||||
|
|
||||||
|
@ -35,13 +41,12 @@ class BinarySearchTree:
|
||||||
|
|
||||||
def insert(self, label):
|
def insert(self, label):
|
||||||
# Create a new Node
|
# Create a new Node
|
||||||
new_node = Node(label)
|
new_node = Node(label, None)
|
||||||
# If Tree is empty
|
# If Tree is empty
|
||||||
if self.empty():
|
if self.empty():
|
||||||
self.root = new_node
|
self.root = new_node
|
||||||
else:
|
else:
|
||||||
#If Tree is not empty
|
#If Tree is not empty
|
||||||
parent_node = None
|
|
||||||
curr_node = self.root
|
curr_node = self.root
|
||||||
#While we don't get to a leaf
|
#While we don't get to a leaf
|
||||||
while curr_node is not None:
|
while curr_node is not None:
|
||||||
|
@ -59,6 +64,33 @@ class BinarySearchTree:
|
||||||
parent_node.setLeft(new_node)
|
parent_node.setLeft(new_node)
|
||||||
else:
|
else:
|
||||||
parent_node.setRight(new_node)
|
parent_node.setRight(new_node)
|
||||||
|
#Set parent to the new node
|
||||||
|
new_node.setParent(parent_node)
|
||||||
|
|
||||||
|
def delete(self, label):
|
||||||
|
if (not self.empty()):
|
||||||
|
#Look for the node with that label
|
||||||
|
node = self.getNode(label)
|
||||||
|
#If the node exists
|
||||||
|
if(node is not None):
|
||||||
|
#If it has no children
|
||||||
|
if(node.getLeft() is None and node.getRight() is None):
|
||||||
|
self.__reassignNodes(node, None)
|
||||||
|
node = None
|
||||||
|
#Has only right children
|
||||||
|
elif(node.getLeft() is None and node.getRight() is not None):
|
||||||
|
self.__reassignNodes(node, node.getRight())
|
||||||
|
#Has only left children
|
||||||
|
elif(node.getLeft() is not None and node.getRight() is None):
|
||||||
|
self.__reassignNodes(node, node.getLeft())
|
||||||
|
#Has two children
|
||||||
|
else:
|
||||||
|
#Gets the max value of the left branch
|
||||||
|
tmpNode = self.getMax(node.getLeft())
|
||||||
|
#Deletes the tmpNode
|
||||||
|
self.delete(tmpNode.getLabel())
|
||||||
|
#Assigns the value to the node to delete and keesp tree structure
|
||||||
|
node.setLabel(tmpNode.getLabel())
|
||||||
|
|
||||||
def getNode(self, label):
|
def getNode(self, label):
|
||||||
curr_node = None
|
curr_node = None
|
||||||
|
@ -78,45 +110,121 @@ class BinarySearchTree:
|
||||||
curr_node = curr_node.getRight()
|
curr_node = curr_node.getRight()
|
||||||
return curr_node
|
return curr_node
|
||||||
|
|
||||||
|
def getMax(self, root = None):
|
||||||
|
if(root is not None):
|
||||||
|
curr_node = root
|
||||||
|
else:
|
||||||
|
#We go deep on the right branch
|
||||||
|
curr_node = self.getRoot()
|
||||||
|
if(not self.empty()):
|
||||||
|
while(curr_node.getRight() is not None):
|
||||||
|
curr_node = curr_node.getRight()
|
||||||
|
return curr_node
|
||||||
|
|
||||||
|
def getMin(self, root = None):
|
||||||
|
if(root is not None):
|
||||||
|
curr_node = root
|
||||||
|
else:
|
||||||
|
#We go deep on the left branch
|
||||||
|
curr_node = self.getRoot()
|
||||||
|
if(not self.empty()):
|
||||||
|
curr_node = self.getRoot()
|
||||||
|
while(curr_node.getLeft() is not None):
|
||||||
|
curr_node = curr_node.getLeft()
|
||||||
|
return curr_node
|
||||||
|
|
||||||
def empty(self):
|
def empty(self):
|
||||||
if self.root is None:
|
if self.root is None:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def preShow(self, curr_node):
|
def __InOrderTraversal(self, curr_node):
|
||||||
|
nodeList = []
|
||||||
if curr_node is not None:
|
if curr_node is not None:
|
||||||
print(curr_node.getLabel())
|
nodeList.insert(0, curr_node)
|
||||||
self.preShow(curr_node.getLeft())
|
nodeList = nodeList + self.__InOrderTraversal(curr_node.getLeft())
|
||||||
self.preShow(curr_node.getRight())
|
nodeList = nodeList + self.__InOrderTraversal(curr_node.getRight())
|
||||||
|
return nodeList
|
||||||
|
|
||||||
def getRoot(self):
|
def getRoot(self):
|
||||||
return self.root
|
return self.root
|
||||||
|
|
||||||
'''
|
def __isRightChildren(self, node):
|
||||||
Example
|
if(node == node.getParent().getRight()):
|
||||||
8
|
return True
|
||||||
/ \
|
return False
|
||||||
3 10
|
|
||||||
/ \ \
|
|
||||||
1 6 14
|
|
||||||
/ \ /
|
|
||||||
4 7 13
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
def __reassignNodes(self, node, newChildren):
|
||||||
|
if(newChildren is not None):
|
||||||
|
newChildren.setParent(node.getParent())
|
||||||
|
if(node.getParent() is not None):
|
||||||
|
#If it is the Right Children
|
||||||
|
if(self.__isRightChildren(node)):
|
||||||
|
node.getParent().setRight(newChildren)
|
||||||
|
else:
|
||||||
|
#Else it is the left children
|
||||||
|
node.getParent().setLeft(newChildren)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
#This function traversal the tree. By default it returns an
|
||||||
|
#In order traversal list. You can pass a function to traversal
|
||||||
|
#The tree as needed by client code
|
||||||
|
def traversalTree(self, traversalFunction = None, root = None):
|
||||||
|
if(traversalFunction is None):
|
||||||
|
#Returns a list of nodes in preOrder by default
|
||||||
|
return self.__InOrderTraversal(self.root)
|
||||||
|
else:
|
||||||
|
#Returns a list of nodes in the order that the users wants to
|
||||||
|
return traversalFunction(self.root)
|
||||||
|
|
||||||
|
#Returns an string of all the nodes labels in the list
|
||||||
|
#In Order Traversal
|
||||||
|
def __str__(self):
|
||||||
|
list = self.__InOrderTraversal(self.root)
|
||||||
|
str = ""
|
||||||
|
for x in list:
|
||||||
|
str = str + " " + x.getLabel().__str__()
|
||||||
|
return str
|
||||||
|
|
||||||
|
def InPreOrder(curr_node):
|
||||||
|
nodeList = []
|
||||||
|
if curr_node is not None:
|
||||||
|
nodeList = nodeList + InPreOrder(curr_node.getLeft())
|
||||||
|
nodeList.insert(0, curr_node.getLabel())
|
||||||
|
nodeList = nodeList + InPreOrder(curr_node.getRight())
|
||||||
|
return nodeList
|
||||||
|
|
||||||
|
def testBinarySearchTree():
|
||||||
|
'''
|
||||||
|
Example
|
||||||
|
8
|
||||||
|
/ \
|
||||||
|
3 10
|
||||||
|
/ \ \
|
||||||
|
1 6 14
|
||||||
|
/ \ /
|
||||||
|
4 7 13
|
||||||
|
'''
|
||||||
|
|
||||||
|
'''
|
||||||
|
Example After Deletion
|
||||||
|
7
|
||||||
|
/ \
|
||||||
|
1 4
|
||||||
|
|
||||||
|
'''
|
||||||
t = BinarySearchTree()
|
t = BinarySearchTree()
|
||||||
t.insert(8)
|
t.insert(8)
|
||||||
t.insert(3)
|
t.insert(3)
|
||||||
t.insert(1)
|
|
||||||
t.insert(6)
|
t.insert(6)
|
||||||
t.insert(4)
|
t.insert(1)
|
||||||
t.insert(7)
|
|
||||||
t.insert(10)
|
t.insert(10)
|
||||||
t.insert(14)
|
t.insert(14)
|
||||||
t.insert(13)
|
t.insert(13)
|
||||||
|
t.insert(4)
|
||||||
|
t.insert(7)
|
||||||
|
|
||||||
t.preShow(t.getRoot())
|
#Prints all the elements of the list in order traversal
|
||||||
|
print(t.__str__())
|
||||||
|
|
||||||
if(t.getNode(6) is not None):
|
if(t.getNode(6) is not None):
|
||||||
print("The label 6 exists")
|
print("The label 6 exists")
|
||||||
|
@ -128,3 +236,22 @@ if __name__ == "__main__":
|
||||||
else:
|
else:
|
||||||
print("The label -1 doesn't exist")
|
print("The label -1 doesn't exist")
|
||||||
|
|
||||||
|
if(not t.empty()):
|
||||||
|
print("Max Value: ", t.getMax().getLabel())
|
||||||
|
print("Min Value: ", t.getMin().getLabel())
|
||||||
|
|
||||||
|
t.delete(13)
|
||||||
|
t.delete(10)
|
||||||
|
t.delete(8)
|
||||||
|
t.delete(3)
|
||||||
|
t.delete(6)
|
||||||
|
t.delete(14)
|
||||||
|
|
||||||
|
#Gets all the elements of the tree In pre order
|
||||||
|
#And it prints them
|
||||||
|
list = t.traversalTree(InPreOrder, t.root)
|
||||||
|
for x in list:
|
||||||
|
print(x)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
testBinarySearchTree()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user