Esempio n. 1
0
def test_levelorder():
    root = BinarySearchTree(5)
    root.insert(2)
    root.insert(-4)
    root.insert(18)
    root.insert(3)
    assert list(root.levelorder()) == [5, 2, 18, -4, 3]
Esempio n. 2
0
def test_insertion():
    root = BinarySearchTree(1)
    root.insert(2)
    root.insert(0)
    root.insert(0)
    assert root.data == 1
    assert root.left.data == 0
    assert root.right.data == 2
    assert list(root.levelorder()) == [1, 0, 2]
Esempio n. 3
0
def test_height():
    root = BinarySearchTree(4)
    root.insert(2)
    root.insert(6)
    root.insert(1)
    root.insert(3)
    root.insert(5)
    root.insert(7)
    assert list(root.levelorder()) == [4, 2, 6, 1, 3, 5, 7]
    assert root.height() == 3
Esempio n. 4
0
def test_remove_root():
    root = BinarySearchTree(4)
    root.insert(2)
    root.insert(6)
    root.insert(1)
    root.insert(3)
    root.insert(5)
    root.insert(7)
    assert list(root.levelorder()) == [4, 2, 6, 1, 3, 5, 7]
    root.remove(4)
    assert list(root.levelordernested()) == [[5], [2, 6], [1, 3, 7]]
Esempio n. 5
0
def test_remove_twochildren():
    root = BinarySearchTree(4)
    root.insert(2)
    root.insert(6)
    root.insert(1)
    root.insert(3)
    root.insert(5)
    root.insert(7)
    assert list(root.levelorder()) == [4, 2, 6, 1, 3, 5, 7]
    root.remove(7)
    root.remove(6)
    root.remove(2)
    assert list(root.levelordernested()) == [[4], [3, 5], [1]]