Advertisement
Guest User

Untitled

a guest
Aug 11th, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.28 KB | None | 0 0
  1. class AVLTree:
  2.     def insert(self, root, key, value):
  3.         #Normal BST Insertion
  4.         if root is None:
  5.             return AVLTreeNode(key, value)
  6.         elif key < root.key:
  7.             root.left = self.insert(root.left, key, value)
  8.         elif key > root.key:
  9.             root.right = self.insert(root.right, key, value)
  10.         else:
  11.             root.values.insert(0, value)
  12.  
  13.         #Update Height
  14.         root.height = 1+max(self.height(root.left), self.height(root.right))
  15.  
  16.         #Check balance
  17.         balance = self.balance(root)
  18.  
  19.         #Rotate if needed
  20.         #Left-Left
  21.         if balance > 1 and key < root.left.key:
  22.             return self.rRotate(root)
  23.  
  24.         #Right Right
  25.         if balance < -1 and key > root.right.key:
  26.             return self.lRotate(root)
  27.  
  28.         #Left Right
  29.         if balance > 1 and key > root.left.key:
  30.             root.left = self.lRotate(root.left)
  31.             return self.rRotate(root)
  32.  
  33.         #Right Left
  34.         if balance < -1 and key < root.right.key:
  35.             root.right = self.rRotate(root.right)
  36.             return self.lRotate(root)
  37.         return root
  38.     def inside(self, root, key, value):
  39.         if root is None:
  40.             return False
  41.         elif key == root.key:
  42.             return value in root.values
  43.         elif key < root.key:
  44.             return self.search(root.left, key, value)
  45.         else:
  46.             return self.search(root.right, key, value)
  47.     def search(self, root, key):
  48.         if root is None:
  49.             return None
  50.         elif key == root.key:
  51.             return root
  52.         elif key < root.key:
  53.             return self.search(root.left, key)
  54.         else:
  55.             return self.search(root.right, key)
  56.     def lessThan(self, root, high, equal):
  57.         if root is None:
  58.             return []
  59.         r = []
  60.         if root.left is not None:
  61.             r += self.lessThan(root.left, high, equal)
  62.         if root.key < high or (equal and root.key == high):
  63.             r.append(root)
  64.         if root.right is not None:
  65.             r += self.lessThan(root.right, high, equal)
  66.         return r
  67.     def greaterThan(self, root, low, equal):
  68.         if root is None:
  69.             return []
  70.         r = []
  71.         if root.left is not None:
  72.             r += self.lessThan(root.left, high, equal)
  73.         if root.key > low or (equal and root.key == low):
  74.             r.append(root)
  75.         if root.right is not None:
  76.             r += self.lessThan(root.right, high, equal)
  77.         return r
  78.     def inRange(self, root, low, high, equal1, equal2):
  79.         if root is None:
  80.             return []
  81.         r = []
  82.         if root.left is not None:
  83.             r += self.inRange(root.left, low, high, equal1, equal2)
  84.         if root.key < high and root.key > low or (equal2 and root.key == high) or (equal1 and root.key == low):
  85.             r.append(root)
  86.         if root.right is not None:
  87.             r += self.inRange(root.right, low, high, equal1, equal2)
  88.         return r
  89.     def deleteLessThan(self, root, high, equal):
  90.         if root is None:
  91.             return
  92.         if root.left is not None:
  93.             root.left = self.deleteLessThan(root.left, high, equal)
  94.         if root.right is not None:
  95.             root.right = self.deleteLessThan(root.right, high, equal)
  96.         if root.key < high or (equal and root.key == high):
  97.             root = self.delete(root, root.key, None)
  98.         return root
  99.     def deleteGreaterThan(self, root, low, equal):
  100.         if root is None:
  101.             return
  102.         if root.left is not None:
  103.             root.left = self.deleteGreaterThan(root.left, low, equal)
  104.         if root.right is not None:
  105.             root.right = self.deleteGreaterThan(root.right, low, equal)
  106.         if root.key > low or (equal and root.key == low):
  107.             root = self.delete(root, root.key, None)
  108.         return root
  109.     def deleteInRange(self, root, low, high, equal1, equal2):
  110.         return root
  111.     def delete(self, root, key, value):
  112.         #Standard BST Delete
  113.         if root is None:
  114.             return root
  115.         elif key < root.key:
  116.             root.left = self.delete(root.left, key, value)
  117.         elif key > root.key:
  118.             root.right = self.delete(root.right, key, value)
  119.         else:
  120.             if value != None and len(root.values) > 1:
  121.                     root.values.remove(value)
  122.                     return root
  123.             if root.left is None:
  124.                 temp = root.right
  125.                 root = None
  126.                 return temp
  127.             elif root.right is None:
  128.                 temp = root.left
  129.                 root = None
  130.                 return temp
  131.             temp = self.minimum(root.right)
  132.             root.key = temp.key
  133.             root.values = temp.values
  134.             root.right = self.delete(root.right, temp.key,value)
  135.  
  136.         #If it is gone
  137.         if root is None:
  138.             return root
  139.  
  140.         #Update Height
  141.         root.height = 1+max(self.height(root.left), self.height(root.right))
  142.  
  143.  
  144.         #Check balance
  145.         balance = self.balance(root)
  146.  
  147.         #Rotate if needed
  148.         #Left-Left
  149.         if balance > 1 and self.balance(root.left) >= 0:
  150.             return self.rRotate(root)
  151.  
  152.         #Right Right
  153.         if balance < -1 and self.balance(root.right) <= 0:
  154.             return self.lRotate(root)
  155.  
  156.         #Left Right
  157.         if balance > 1 and self.balance(root.left) < 0:
  158.             root.left = self.lRotate(root.left)
  159.             return self.rRotate(root)
  160.  
  161.         #Right Left
  162.         if balance < -1 and self.balance(root.right) > 0:
  163.             root.right = self.rRotate(root.right)
  164.             return self.lRotate(root)
  165.         return root
  166.     def height(self, root):
  167.         #If it's not a tree it doesn't have a height
  168.         #Else just get the height
  169.         if root is None: return 0
  170.         return root.height
  171.     def balance(self, root):
  172.         #If it's not a tree it doesn't have a balance
  173.         #Else get the difference between height of subtrees
  174.         if root is None: return 0;
  175.         return self.height(root.left)-self.height(root.right)
  176.     def lRotate(self, root):
  177.         #Temporary Storage
  178.         r = root.right
  179.         rl = r.left
  180.  
  181.         #Swap Around
  182.         r.left = root
  183.         root.right = rl
  184.  
  185.         #Update Heights
  186.         root.height = 1+max(self.height(root.left), self.height(root.right))
  187.         r.height = 1+max(self.height(r.left), self.height(r.right))
  188.         return r
  189.     def rRotate(self, root):
  190.         #Temporary Storage
  191.         l = root.left
  192.         lr = l.right
  193.  
  194.         #Swap Around
  195.         l.right = root
  196.         root.left = lr
  197.        
  198.         #Update Heights
  199.         root.height = 1+max(self.height(root.left), self.height(root.right))
  200.         l.height = 1+max(self.height(l.left), self.height(l.right))
  201.         return l
  202.     def minimum(self, root):
  203.         #If we reach the end just return
  204.         #Else continue going left
  205.         if root is None or root.left is None: return root
  206.         return self.minimum(root.left)
  207.     def string(self, root):
  208.         #If it isn't a node represent it as a blank
  209.         #Else return the left child, itself, and the right child
  210.         if root is None: return "(-)"
  211.         return "("+self.string(root.left)+str(root.key)+self.string(root.right)+")"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement