ayaderaghul

binary search tree

Dec 18th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.75 KB | None | 0 0
  1. #########################################################
  2. # CODE INSTRUCTIONS:                                    #
  3. # 1) The method findInOrderSuccessor you're asked      #
  4. #    to implement is located at line 30.                #
  5. # 2) Use the helper code below to implement it.         #
  6. # 3) In a nutshell, the helper code allows you to       #
  7. #    to build a Binary Search Tree.                     #
  8. # 4) Jump to line 88 to see an example for how the      #
  9. #    helper code is used to test findInOrderSuccessor.  #
  10. #########################################################
  11.  
  12.  
  13. # A node
  14. class Node:
  15.  
  16.   # Constructor to create a new node
  17.   def __init__(self, key):
  18.     self.key = key
  19.     self.left = None
  20.     self.right = None
  21.     self.parent = None
  22.  
  23. # A binary search tree
  24. class BinarySearchTree:
  25.  
  26.   # Constructor to create a new BST
  27.   def __init__(self):
  28.     self.root = None
  29.  
  30.   def find_in_order_successor(self, inputNode):
  31.     # pass # your code goes here
  32.     # if node doesnt have children,
  33.     # - then go up (left) til it can turn right
  34.     # if node has children
  35.     # - then it is the rightest child
  36.     if inputNode == None:
  37.       return None
  38.     if inputNode.right == None:
  39.       while inputNode.parent != None and inputNode.parent.key < inputNode.key:
  40.         inputNode = inputNode.parent
  41.       return inputNode.parent
  42.     else:
  43.       inputNode = inputNode.right
  44.       while inputNode.left != None:
  45.         inputNode = inputNode.left
  46.       return inputNode
  47.      
  48.        
  49.        
  50.        
  51.    
  52.      
  53.    
  54.  
  55.   # Given a binary search tree and a number, inserts a
  56.   # new node with the given number in the correct place
  57.   # in the tree. Returns the new root pointer which the
  58.   # caller should then use(the standard trick to avoid
  59.   # using reference parameters)
  60.   def insert(self, key):
  61.    
  62.     # 1) If tree is empty, create the root
  63.     if (self.root is None):
  64.       self.root = Node(key)
  65.       return
  66.        
  67.     # 2) Otherwise, create a node with the key
  68.     #    and traverse down the tree to find where to
  69.     #    to insert the new node
  70.     currentNode = self.root
  71.     newNode = Node(key)
  72.     while(currentNode is not None):
  73.      
  74.       if(key < currentNode.key):
  75.         if(currentNode.left is None):
  76.           currentNode.left = newNode;
  77.           newNode.parent = currentNode;
  78.           break
  79.         else:
  80.           currentNode = currentNode.left;
  81.       else:
  82.         if(currentNode.right is None):
  83.           currentNode.right = newNode;
  84.           newNode.parent = currentNode;
  85.           break
  86.         else:
  87.           currentNode = currentNode.right;
  88.  
  89.   # Return a reference to a node in the BST by its key.
  90.   # Use this method when you need a node to test your
  91.   # findInOrderSuccessor function on
  92.   def getNodeByKey(self, key):
  93.    
  94.     currentNode = self.root
  95.     while(currentNode is not None):
  96.       if(key == currentNode.key):
  97.         return currentNode
  98.        
  99.       if(key < currentNode.key):
  100.         currentNode = currentNode.left
  101.       else:
  102.         currentNode = currentNode.right
  103.        
  104.     return None
  105.        
  106. #########################################
  107. # Driver program to test above function #
  108. #########################################
  109.  
  110. # Create a Binary Search Tree
  111. bst  = BinarySearchTree()
  112. bst.insert(20)
  113. bst.insert(9);
  114. bst.insert(25);
  115. bst.insert(5);
  116. bst.insert(12);
  117. bst.insert(11);  
  118. bst.insert(14);    
  119.  
  120. # Get a reference to the node whose key is 9
  121. test = bst.getNodeByKey(14)
  122.  
  123. # Find the in order successor of test
  124. succ = bst.find_in_order_successor(test)
  125.  
  126. # Print the key of the successor node
  127. if succ is not None:
  128.     print ("\nInorder Successor of %d is %d " \
  129.             %(test.key , succ.key))
  130. else:
  131.     print ("\nInorder Successor doesn't exist")
Advertisement
Add Comment
Please, Sign In to add comment