Advertisement
BOT_Yokel

ESC180 - Node and Binary Search Tree Classes

Dec 15th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. class Node:
  2.     def __init__(self, cargo = None, left = None, right = None):
  3.         self.cargo = cargo
  4.         self.left = left
  5.         self.right = right
  6.    
  7.     def __str__(self):
  8.         return str(self.cargo)
  9.    
  10. class BinarySearchTree:
  11.     def __init__(self, root = None):
  12.         self.root = root
  13.    
  14.     def insert(self, val):
  15.         #if there is no root, create a Node at the root
  16.         if self.root == None:
  17.             self.root = Node(val)
  18.         #if there is a root, check whether the value is greater than or less than the root
  19.         #Check the appropriate left or right pointer to see if a Node exists, and continue evaluating
  20.         #or assign to the left or right pointer when appropriate
  21.         #else call Insert_Helper
  22.         else:
  23.             self.Insert_Helper(self.root, val)
  24.    
  25.     def Insert_Helper(self, current_node, val):
  26.         #If the value is less than the current node's cargo
  27.         if val < current_node.cargo:
  28.             #if the current node's left is None
  29.             if current_node.left == None:
  30.                 current_node.left = Node(val)
  31.             else:
  32.                 self.Insert_Helper(current_node.left, val)
  33.         elif val > current_node.cargo:
  34.             #if the current node's right is None
  35.             if current_node.right == None:
  36.                 #Create a node with that value
  37.                 current_node.right = Node(val)
  38.             else:
  39.                 self.Insert_Helper(current_node.right, val)
  40.    
  41.     def search(self, val):
  42.         search_t1 = perf_counter()
  43.         result = self.search_helper(self.root, val)
  44.         if result:
  45.             search_t2 = perf_counter()
  46.             time_elapsed = search_t2 - search_t1
  47.             print('Elapsed time: ' + str(time_elapsed))
  48.             return True
  49.         else:
  50.             search_t2 = perf_counter()
  51.             time_elapsed = search_t2 - search_t1            
  52.             print('Elapsed time: ' + str(time_elapsed))
  53.             return False
  54.    
  55.     def search_helper(self, current_node, val):
  56.         #if the current node is empty, return False
  57.         if current_node == None:
  58.             return False
  59.         #elif the current node is the value, return True
  60.         elif current_node.cargo == val:
  61.             return True
  62.         elif val < current_node.cargo:
  63.             self.search_helper(current_node.left, val)
  64.         elif val > current_node.cargo:
  65.             self.search_helper(current_node.right, val)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement