Advertisement
gur111

Basic Tree with Ranges

Mar 12th, 2021
1,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1.  
  2. class Node:
  3.     def __init__(self, data, offset=0, subTotal=0, lazySubTotal=0):
  4.         self.data = data
  5.         self.offset, self.subTotal, self.lazySubTotal = offset, subTotal, lazySubTotal
  6.         self.left = self.right = self.parent = None
  7.  
  8.  
  9. class Vaccines:
  10.     def __init__(self, lst: list) -> None:
  11.         size = len(lst)
  12.         self.root = Vaccines.insert_level_order(range(1, size), None, 0, size)
  13.         for _ in lst:
  14.             pass  # TODO: Implement array of non zeroes init
  15.  
  16.     @staticmethod
  17.     def insert_level_order(lst, root, i, n):
  18.         if i < n:
  19.             temp = Node(lst[i])
  20.             root = temp
  21.  
  22.             # Left
  23.             root.left = Vaccines.insert_level_order(lst, root.left,
  24.                                                     2 * i + 1, n)
  25.             if root.left:
  26.                 root.left.parent = root
  27.  
  28.             # Right
  29.             root.right = Vaccines.insert_level_order(lst, root.right,
  30.                                                      2 * i + 2, n)
  31.             if root.right:
  32.                 root.right.parent = root
  33.  
  34.         return root
  35.  
  36.     def in_order(self):
  37.         self._in_order(self.root)
  38.  
  39.     def _in_order(self, root):
  40.         if root.left:
  41.             Vaccines.in_order(root.left)
  42.             print(root.left.data, end=" ")
  43.  
  44.         print(root.data, end=" ")
  45.  
  46.         if root.right:
  47.             Vaccines.in_order(root.right)
  48.             print(root.right.data, end=" ")
  49.  
  50.     def add(self, i,j, x):
  51.         assert i <= j, f"i ({i}) must be smaller from j ({j})"
  52.  
  53.         # Left bound
  54.        
  55.  
  56.         # Right bound
  57.         pass
  58.  
  59.     def find(self, i):
  60.         pass
  61.  
  62.  
  63. if __name__ == '__main__':
  64.     lst = list(range(1, 31))
  65.     n = len(lst)
  66.     root = None
  67.     tree = Vaccines.insert_level_order(lst, root, 0, n)
  68.     tree.in_order()
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement