gur111 icon

Basic Tree with Ranges

gur111 | PRO | 03/12/21 05:17:11 PM UTC | 0 ⭐ | 1344 👁️ | Never ⏰ | []
Python |

1.83 KB

|

None

|

0 👍

/

0 👎

 
class Node:
    def __init__(self, data, offset=0, subTotal=0, lazySubTotal=0):
        self.data = data
        self.offset, self.subTotal, self.lazySubTotal = offset, subTotal, lazySubTotal
        self.left = self.right = self.parent = None
 
 
class Vaccines:
    def __init__(self, lst: list) -> None:
        size = len(lst)
        self.root = Vaccines.insert_level_order(range(1, size), None, 0, size)
        for _ in lst:
            pass  # TODO: Implement array of non zeroes init
 
    @staticmethod
    def insert_level_order(lst, root, i, n):
        if i < n:
            temp = Node(lst[i])
            root = temp
 
            # Left
            root.left = Vaccines.insert_level_order(lst, root.left,
                                                    2 * i + 1, n)
            if root.left:
                root.left.parent = root
 
            # Right
            root.right = Vaccines.insert_level_order(lst, root.right,
                                                     2 * i + 2, n)
            if root.right:
                root.right.parent = root
 
        return root
 
    def in_order(self):
        self._in_order(self.root)
 
    def _in_order(self, root):
        if root.left:
            Vaccines.in_order(root.left)
            print(root.left.data, end=" ")
 
        print(root.data, end=" ")
 
        if root.right:
            Vaccines.in_order(root.right)
            print(root.right.data, end=" ")
 
    def add(self, i,j, x):
        assert i <= j, f"i ({i}) must be smaller from j ({j})"
 
        # Left bound
        
 
        # Right bound
        pass
 
    def find(self, i):
        pass
 
 
if __name__ == '__main__':
    lst = list(range(1, 31))
    n = len(lst)
    root = None
    tree = Vaccines.insert_level_order(lst, root, 0, n)
    tree.in_order()
 

Comments