Exemplo n.º 1
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
Exemplo n.º 2
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]
Exemplo n.º 3
0
def test_postorder():
    root = BinarySearchTree(5)
    root.insert(2)
    root.insert(-4)
    root.insert(18)
    root.insert(3)
    assert list(root.postorder()) == [-4, 3, 2, 18, 5]
Exemplo n.º 4
0
def test_inorder():
    root = BinarySearchTree(5)
    root.insert(2)
    root.insert(-4)
    root.insert(18)
    root.insert(3)
    assert list(root.inorder()) == [-4, 2, 3, 5, 18]
Exemplo n.º 5
0
def test_preorder():
    root = BinarySearchTree(5)
    root.insert(2)
    root.insert(-4)
    root.insert(18)
    root.insert(3)
    assert list(root.preorder()) == [5, 2, -4, 3, 18]
Exemplo n.º 6
0
def test_maxNode():
    root = BinarySearchTree(2)
    root.insert(0)
    root.insert(-1)
    root.insert(1)
    root.insert(4)
    assert root.maxNode().data == 4
Exemplo n.º 7
0
def test_maxValue():
    root = BinarySearchTree(2)
    root.insert(0)
    root.insert(-1)
    root.insert(1)
    root.insert(4)
    assert root.maxValue() == 4
Exemplo n.º 8
0
def test___str__():
    root = BinarySearchTree(5)
    root.insert(2)
    root.insert(-4)
    root.insert(18)
    root.insert(3)
    assert root.__str__() == str([5, 2, 18, -4, 3])
Exemplo n.º 9
0
def test_toList():
    root = BinarySearchTree(5)
    root.insert(2)
    root.insert(-4)
    root.insert(18)
    root.insert(3)
    assert root.toList() == [5, 2, 18, -4, 3]
Exemplo n.º 10
0
def test_levelordernested():
    root = BinarySearchTree(5)
    root.insert(2)
    root.insert(-4)
    root.insert(18)
    root.insert(3)
    assert list(root.levelordernested()) == [[5], [2, 18], [-4, 3]]
Exemplo n.º 11
0
def test_search():
    root = BinarySearchTree(2)
    root.insert(0)
    root.insert(1)
    root.insert(4)
    assert root.right.data == 4
    assert root.search(4)
    assert not root.search(3)
Exemplo n.º 12
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]
Exemplo n.º 13
0
def test_creation():
    root = BinarySearchTree(7)
    assert root.data == 7
    assert root.left is None
    assert root.right is None
Exemplo n.º 14
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]]
Exemplo n.º 15
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]]