コード例 #1
0
ファイル: mirrortree.py プロジェクト: swang2000/BinaryTree
def mirrortree(root):
    if root == None:
        return
    mroot = tree.Node(root.value)
    mroot.left_child = mirrortree(root.right_child)
    mroot.right_child = mirrortree(root.left_child)
    return mroot
コード例 #2
0
ファイル: Mergetwotrees.py プロジェクト: swang2000/BinaryTree
def mergetree(root1, root2):
    if root1 == None and root2 == None:
        return
    if root1 and root2 == None:
        out = tree.Node(root1.value)
        out.left_child = mergetree(root1.left_child, None)
        out.right_child = mergetree(root1.right_child, None)
    if root2 and root1 == None:
        out = tree.Node(root2.value)
        out.left_child = mergetree(None, root2.left_child)
        out.right_child = mergetree(None, root2.right_child)
    if root1 and root2:
        out = tree.Node(root1.value + root2.value)
        out.left_child = mergetree(root1.left_child, root2.left_child)
        out.right_child = mergetree(root1.right_child, root2.right_child)
    return out
コード例 #3
0
ファイル: ArrayToBST.py プロジェクト: swang2000/BinaryTree
def arrayToBst(a):
    n = len(a)
    if n >= 1:
        node = Tree.Node(a[n // 2])
        node.left_child = arrayToBst(a[(n // 2) + 1:])
        node.right_child = arrayToBst(a[:(n // 2)])
        return node
コード例 #4
0
def Tree_Pre_Inorder(a, b):
    if len(a) > 0:
        root = tree.Node(a[0])
        index = b.index(a[0])
        lefta = a[1:index + 1]
        leftb = b[:index]
        righta = a[index + 1:]
        rightb = b[index + 1:]
        root.left_child = Tree_Pre_Inorder(lefta, leftb)
        root.right_child = Tree_Pre_Inorder(righta, rightb)
        return root