Пример #1
0
    def test_question_1(self):

        tree_1 = BinaryNode.from_array([1, 2 ,3, 4, 5, 6, 7])

        assert tree_1.is_balanced()

        leaf_node = next(tree_1.get_leaf_nodes())

        leaf_node.rnode = BinaryNode(0)
        leaf_node.rnode.rnode = BinaryNode(0)

        assert not tree_1.is_balanced()
Пример #2
0
    def insert(self, value):
        """
        Insert a node at the bottom of the tree and percolate it up
        """
        parent = self.__find_new_parent()

        node = BinaryNode(value)

        if parent is None:
            self._root = node
        else:
            if parent.lnode:
                parent.rnode = node
            else:
                parent.lnode = node

        self.__percolate_up(node)

        self.heap_size += 1