m2skills

bst min max python

May 31st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 KB | None | 0 0
  1. # program to find the min and max element in bst
  2.  
  3. class node:
  4.    
  5.     # constructor method
  6.     # setting default value of left and right links to null
  7.     def __init__(self,element,left = None,right = None):
  8.         self.element = element
  9.         self.left = left
  10.         self.right = right
  11.  
  12.  
  13.     # method to update left Link   
  14.     def updateLeftLink(self,link):
  15.         self.left = link
  16.  
  17.  
  18.     # method to update right link  
  19.     def updateRightLink(self,link):
  20.         self.right = link  
  21.  
  22.  
  23.     # method to get the data element of the node
  24.     def getElement(self):
  25.         return self.element
  26.  
  27.     # method to get the Left node  
  28.     def getLeftNode(self):
  29.         return self.left
  30.    
  31.     # method to get the right node
  32.     def getRightNode(self):
  33.         return self.right  
  34.  
  35.  
  36. class BST:
  37.     def __init__(self):
  38.         self.root = None
  39.         self.pHeight = 0
  40.    
  41.    
  42.     # method returns true if the bst is empty
  43.     def isEmpty(self):
  44.         return self.root is None
  45.  
  46.     # method returns root of the binary search tree
  47.     def getRoot(self):
  48.         return self.root
  49.    
  50.    
  51.     # method inserts element in the binary search tree 
  52.     def insert(self,element):
  53.         tempNode = node(element)
  54.        
  55.         if self.getRoot() is None:
  56.             self.root = tempNode
  57.         else:
  58.             inserted = False
  59.             p = self.root
  60.             while not inserted:
  61.                 # if new element is greater the current element then insert it to the right
  62.                 if tempNode.getElement() > p.getElement():
  63.                     # if right link of the current is null then directly insert
  64.                     if p.getRightNode() is None:
  65.                         p.updateRightLink(tempNode)
  66.                         inserted = True
  67.                     else:
  68.                         p = p.getRightNode()
  69.                
  70.                 # if new element is less the current element then insert it to the left
  71.                 elif tempNode.getElement() < p.getElement():
  72.                     # if left link of the current is null then directly insert
  73.                     if p.getLeftNode() is None:
  74.                         p.updateLeftLink(tempNode)
  75.                         inserted = True
  76.                     else:
  77.                         p = p.getLeftNode()
  78.        
  79.     # method to display the max element of the Binary Tree
  80.     # max element is the rightmost node of the bst
  81.     def maxElement(self):
  82.        
  83.         # check if the bst is empty or not
  84.         if self.isEmpty():
  85.             return -999999      # assuming that -999999 never appears in the BST
  86.            
  87.         tempNode = self.getRoot()
  88.         # traverse to the rightmost node in the bst
  89.         while tempNode.getRightNode() is not None:
  90.             tempNode = tempNode.getRightNode()
  91.        
  92.         return tempNode.getElement()
  93.    
  94.    
  95.     # method to display the min element of the Binary Tree
  96.     # min element is the leftmost node of the bst
  97.     def minElement(self):
  98.        
  99.         # check if the bst is empty or not
  100.         if self.isEmpty():
  101.             return 999999       # assuming that 999999 never appears in the BST
  102.            
  103.         tempNode = self.getRoot()
  104.         # traverse to the rightmost node in the bst
  105.         while tempNode.getLeftNode() is not None:
  106.             tempNode = tempNode.getLeftNode()
  107.        
  108.         return tempNode.getElement()
  109.    
  110.    
  111.  
  112. # main function
  113.  
  114. b1 = BST()
  115. b1.insert(7)
  116. b1.insert(29)
  117. b1.insert(25)
  118. b1.insert(36)
  119. b1.insert(71)
  120. b1.insert(24)
  121. b1.insert(5)
  122. b1.insert(9)
  123. b1.insert(1)
  124. root = b1.getRoot()
  125.  
  126. print("\n\nThe Largest Element of the BST is : " + str(b1.maxElement()))
  127.  
  128. print("\nThe Smallest Element of the BST is : " + str(b1.minElement()))
Add Comment
Please, Sign In to add comment