コード例 #1
0
def test_BinaryTree():
    r = BinaryTree('a')
    assert r.get_root_val() == 'a'
    assert r.get_left_child() is None
    r.insert_left('b')
    assert str(r.get_left_child()) == 'BinaryTree object: val=b'
    assert r.get_left_child().get_root_val() == 'b'
    r.insert_right('c')
    assert str(r.get_right_child()) == 'BinaryTree object: val=c'
    assert r.get_right_child().get_root_val() == 'c'
    r.get_right_child().set_root_val('hello')
    assert r.get_right_child().get_root_val() == 'hello'
コード例 #2
0
from binarytree import BinaryTree
root = BinaryTree('a')
print(root)
print(root.get_root_val())
print(root.get_left_child())
root.insert_left('b')
print(root.get_left_child().get_root_val())
root.insert_right('c')
print(root.get_right_child().get_root_val())
root.get_right_child().set_root_val('hello')
print(root.get_right_child().get_root_val())
root.insert_left('d')
print(root.get_left_child())
print(root.get_left_child().get_left_child().get_root_val())
コード例 #3
0
        preorder(tree.get_right_child())


def postorder(tree):
    if tree != None:
        postorder(tree.get_left_child())
        postorder(tree.get_right_child())
        print(tree.get_root_val())


def inorder(tree):
    if tree != None:
        inorder(tree.get_left_child())
        print(tree.get_root_val())
        inorder(tree.get_right_child())


t = BinaryTree('a')
t.insert_left('b')
t.insert_right('C')
t.get_left_child().insert_right('d')
t.get_left_child().insert_left('e')
t.get_right_child().insert_right('f')

print("Preorder")
preorder(t)
print("Postorder")
postorder(t)
print("Inorder")
inorder(t)