def test_inorder(): for i in range(20): bst = BST() randomlist = random.sample(range(1, 100), 35) for x in randomlist: bst.insert(x) assert bst.inorder(bst.root) == bst.inorder_iterative()
def test_rb_tree(): bst = BST() bst.put('a', 1) bst.put('b', 1) bst.put('c', 1) bst.put('d', 1) bst.put('e', 1) assert bst.root.key == 'd' assert bst.root.left.key == 'b' a = bst.inorder() assert a == ['a', 'b', 'c', 'd', 'e']
def test_order(): bst = BST() bst.put('s', 3) bst.put('e', 5) bst.put('a', 1) bst.put('x', 0) bst.put('r', 4) bst.put('h', 7) a = bst.inorder() assert a == ['a', 'e', 'h', 'r', 's', 'x']
from BST import BST, Node tree = BST() tree.add(2) tree.add(1) tree.add(3) tree.add(6) tree.add(4) tree.add(7) print(">>> in-order:") for i in tree.inorder(): print(i) print(">>> post-order:") for i in tree.postorder(): print(i) print("min()", min(tree)) print("max()", max(tree)) assert max(tree) == 7 # k-th max value (order statistics) assert tree.max(2) == 6 assert tree.max(3) == 4 assert 3 in tree tree.remove(3) assert 3 not in tree