Example #1
0
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
Example #2
0
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
Example #3
0
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
Example #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
    temp = root
    while temp != None:
        s.append(temp)
        temp = temp.left_child

    return s


def iwot(root):
    s = []
    final = []
    current = root
    while current != None or (len(s) > 0):
        s = leftnext(current, s)
        current = s.pop()
        final.append(current.value)
        current = current.right_child

    return final


bst = tree.BST()
bst.insert(5)
bst.insert(3)
bst.insert(2)
bst.insert(2.5)
bst.insert(4)
bst.insert(8)
bst.insert(6)
iwot(bst.root)
Example #6
0
        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



bst1 = tree.BST()
bst2 = tree.BST()
bst1.insert(4)
bst1.insert(2)
bst1.insert(1)
bst1.insert(5)

bst2.insert(3)
bst2.insert(1)
bst2.insert(2)
bst2.insert(6)
bst2.insert(7)
mergetree(bst1.root, bst2.root)


Example #7
0
def LRM(root):
    if root == None:
        return
    out = [root.value]
    temp = root
    while temp.left_child:
        out.append(temp.left_child.value)
        temp = temp.left_child
    temp = root
    while temp.right_child:
        out.append(temp.right_child.value)
        temp = temp.right_child
    return out


bst = tree.BST()
bst.insert(5)
bst.insert(3)
bst.insert(2)
bst.insert(2.5)
bst.insert(4)
bst.insert(8)
bst.insert(6)
LRM(bst.root)

bst2 = tree.BST()
bst2.insert(15)
bst2.insert(10)
bst2.insert(8)
bst2.insert(12)
bst2.insert(20)