Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.69 KB | None | 0 0
  1. class Node:
  2.     def __init__(self, value):
  3.  
  4.         # node properties
  5.         self.left = None
  6.         self.right = None
  7.         self.value = value
  8.  
  9.  
  10. class BinarySearchTree:
  11.     def __init__(self):
  12.  
  13.         # define root node
  14.         self.root = None
  15.  
  16.    
  17.     def insert(self, value):
  18.        
  19.         # create new node
  20.         newNode = Node(value)
  21.  
  22.         # if root node does not exist, assign new node as root
  23.         if self.root is None:
  24.             self.root = newNode
  25.         else:
  26.  
  27.             # Keep track of current node
  28.             currentNode = self.root
  29.  
  30.             # Traverse tree
  31.             while True:
  32.  
  33.                 # Add new node to the left if value is less than current node's value. Otherwise add to right side
  34.                 if value < currentNode.value:
  35.  
  36.                     # Check if current node has left child
  37.                     # If empty, add new node
  38.                     if currentNode.left is None:
  39.                
  40.                         currentNode.left = newNode
  41.                         return newNode
  42.  
  43.                     currentNode = currentNode.left
  44.  
  45.                 else:
  46.  
  47.                      # Check if current node has right child
  48.                      # If empty, add new node
  49.                     if currentNode.right is None:
  50.                         currentNode.right = newNode
  51.                         return newNode
  52.  
  53.                     currentNode = currentNode.right
  54.  
  55.  
  56.     def lookup(self, value):
  57.  
  58.         # Check if root node exists
  59.         if self.root is None:
  60.             return False
  61.  
  62.         # Keep track of current node
  63.         currentNode = self.root
  64.  
  65.         # Traverse tree
  66.         while currentNode:
  67.  
  68.             # If lookup value is less than current node's value, traverse left else traverse right. Update the current node
  69.             # If current node's value equals the look up value, return the node
  70.             if value < currentNode.value:
  71.                 currentNode = currentNode.left
  72.             elif value > currentNode.value:
  73.                 currentNode = currentNode.right
  74.             elif value == currentNode.value:
  75.                 print(True)
  76.                 return currentNode
  77.  
  78.         # return None if lookup value not found
  79.         return None
  80.  
  81.     def remove(self, root, key):
  82.      
  83.         if not root:
  84.             return
  85.        
  86.         # we always want to delete the node when it is the root of a subtree,
  87.         # so we handle left or right according to the val.
  88.         # if the node does not exist, we will hit the very first if statement and return None.
  89.         if key > root.val:
  90.             root.right = self.remove(root.right, key)
  91.            
  92.         elif key < root.val:
  93.             root.left = self.remove(root.left, key)
  94.        
  95.         # now the key is the root of a subtree
  96.         else:
  97.             # if the subtree does not have a left child, we just return its right child
  98.             # to its father, and they will be connected on the higher level recursion.
  99.             if not root.left:
  100.                 return root.right
  101.            
  102.             # if it has a left child, we want to find the max val on the left subtree to
  103.             # replace the node we want to delete.
  104.             else:
  105.                 # try to find the max value on the left subtree
  106.                 tmp = root.left
  107.                 while tmp.right:
  108.                     tmp = tmp.right
  109.                    
  110.                 # replace
  111.                 root.val = tmp.val
  112.                 root.left = self.remove(root.left, tmp.val)
  113.        
  114.         return root
  115.  
  116. tree = BinarySearchTree();
  117. tree.insert(9)
  118. tree.insert(4)
  119. tree.insert(6)
  120. tree.insert(20)
  121. tree.remove(tree, 20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement