Beispiel #1
0
    def test_add(self):
        bst = BinarySearchTree()
        bst.add(7)
        self.assertEqual(bst.root.value, 7)
        self.assertIsNone(bst.root.left)
        self.assertIsNone(bst.root.right)

        bst.add(4)
        self.assertIsNotNone(bst.root.left)
        self.assertEqual(bst.root.left.value, 4)

        bst.add(10)
        self.assertIsNotNone(bst.root.right)
        self.assertEqual(bst.root.right.value, 10)

        bst.add(6)
        self.assertEqual(bst.root.left.right.value, 6)
Beispiel #2
0
            second = root
        prev = root
        first, second, prev = traverse(root.right, first, second, prev)
    return first, second, prev


if __name__ == "__main__":
    bst = BinarySearchTree.from_array([1, 4, 5, 7, 8, 9, 10])
    print bst
    bst.root.left.value, bst.root.right.left.value = bst.root.right.left.value, bst.root.left.value
    print bst
    fix_bst(bst)
    print bst

    print
    bst = BinarySearchTree()
    bst.add(0)
    bst.root.left = BinaryTreeNode(1)
    print bst
    fix_bst(bst)
    print bst
    print

    bst = BinarySearchTree()
    bst.add(2)
    bst.root.left = BinaryTreeNode(1)
    bst.root.left.right = BinaryTreeNode(3)
    print bst
    fix_bst(bst)
    print bst