BVB Source Codes

Algorithm-Implementations Show bst.py Source code

Return Download Algorithm-Implementations: download bst.py Source code - Download Algorithm-Implementations Source code - Type:.py
  1.  
  2. class TreeNode:
  3.     def __init__(self, value):
  4.         self.value = value
  5.         self.right = None
  6.         self.left = None
  7.  
  8.  
  9. class BinarySearchTree:
  10.     def __init__(self):
  11.         self.root = None
  12.  
  13.     def insert(self, value):
  14.         if self.root is None:
  15.             self.root = TreeNode(value)
  16.         else:
  17.             current = self.root
  18.             while True:
  19.                 if value <= current.value:
  20.                     if current.left:
  21.                         current = current.left
  22.                     else:
  23.                         current.left = TreeNode(value)
  24.                         break
  25.                 else:
  26.                     if current.right:
  27.                         current = current.right
  28.                     else:
  29.                         current.right = TreeNode(value)
  30.                         break
  31.  
  32.     @staticmethod
  33.     def find_min(node):
  34.         if not node:
  35.             return None
  36.         else:
  37.             current = node
  38.             parent = None
  39.             while True:
  40.                 if current.left:
  41.                     parent = current
  42.                     current = current.left
  43.                 else:
  44.                     break
  45.             return current, parent
  46.  
  47.     def delete_node(self, node, parent):
  48.         if node.right and node.left:
  49.             min_node, min_parent = self.find_min(node.right)
  50.             node.value = min_node.value
  51.             if min_parent is None:
  52.                 min_parent = node
  53.             self.delete_node(min_node, min_parent)
  54.         elif node.left:
  55.             if parent.right == node:
  56.                 parent.right = node.left
  57.             else:
  58.                 parent.left = node.left
  59.         elif node.right:
  60.             if parent.right == node:
  61.                 parent.right = node.right
  62.             else:
  63.                 parent.left = node.right
  64.         else:
  65.             if parent.right == node:
  66.                 parent.right = None
  67.             else:
  68.                 parent.left = None
  69.  
  70.     def delete(self, value):
  71.         if not self.root:
  72.             return None
  73.         else:
  74.             current = self.root
  75.             parent = None
  76.             while True:
  77.                 if value == current.value:
  78.                     break
  79.                 elif value <= current.value:
  80.                     if current.left:
  81.                         parent = current
  82.                         current = current.left
  83.                     else:
  84.                         return
  85.                 else:
  86.                     if current.right:
  87.                         parent = current
  88.                         current = current.right
  89.                     else:
  90.                         return
  91.  
  92.             self.delete_node(current, parent)
  93.  
  94.     def search(self, value):
  95.         if not self.root:
  96.             return None
  97.         else:
  98.             current = self.root
  99.             while True:
  100.                 if value == current.value:
  101.                     return current
  102.                 elif value <= current.value:
  103.                     if current.left:
  104.                         current = current.left
  105.                     else:
  106.                         return None
  107.                 else:
  108.                     if current.right:
  109.                         current = current.right
  110.                     else:
  111.                         return None
  112.  
downloadbst.py Source code - Download Algorithm-Implementations Source code
Related Source Codes/Software:
raty - 2017-04-22
RDVTabBarController - Highly customizable tabBar and tabBarController fo... 2017-04-22
material-icon-lib - Library containing over 1500 material vector icons... 2017-04-21
httpdiff - Perform the same request against two HTTP servers ... 2017-04-21
jquerytools - The missing UI library for the Web ... 2017-04-21
mcrouter - Mcrouter is a memcached protocol router for scalin... 2017-04-22
dynomite - A generic dynamo implementation for different k-v ... 2017-04-22
kityminder - Baidu brain figure 2017-04-22
llvm - Mirror of official llvm git repository located at ... 2017-04-22
RBBAnimation - Block-based animations made easy, comes with easin... 2017-04-22
ied - 2017-04-29
Nimble - A Matcher Framework for Swift and Objective-C 2017-04-29
MHVideoPhotoGallery - A Photo and Video Gallery 2017-04-29
shoulda-matchers - Collection of testing matchers extracted from Shou... 2017-04-29
Android-SlideExpandableListView - A better ExpandableListView, with animated expanda... 2017-04-29
AppSales-Mobile - App Sales allows iPhone and Mac App Store develope... 2017-04-29
react-templates - Light weight templates for react ... 2017-04-28
afterglow-theme - A minimal dark Theme for Sublime Text 2 and 3 2017-04-28
jwt-go - Golang implementation of JSON Web Tokens (JWT) 2017-04-28
DeerResume - Tool MarkDown online resume, online preview, edit,... 2017-04-28

 Back to top