Esempio n. 1
0
def constructParseTree(expr):
    print expr
    if (expr == []):
        return
    expTree = binTree.BinaryTree('')
    for char in expr:
        if (char == '('):
            expTree.leftChild = constructParseTree(expr[1:])
        elif (char in '+-*/'):
            expTree.setRootVal(char)
            expTree.rightChild = constructParseTree(expr[1:])
        elif (char in '0123456789'):
            expTree.setRootVal(char)
#            return expTree
        elif (char == ')'):
            return expTree
    return expTree
Esempio n. 2
0

def sumLeftLeaves(tree):
    if (tree == None):
        return 0
    if (isLeaf(tree.getLeftChild())):
        return tree.getLeftChild().getRootVal() + sumLeftLeaves(
            tree.getRightChild())
    else:
        return sumLeftLeaves(tree.getLeftChild()) + sumLeftLeaves(
            tree.getRightChild())


def isLeaf(tree):
    if (tree == None):
        return False
    return (tree.getLeftChild() == None and tree.getRightChild() == None)


tree = binTree.BinaryTree(20)
tree.insertLeft(9)
tree.getLeftChild().insertLeft(5)
tree.getLeftChild().insertRight(12)
tree.getLeftChild().getRightChild().insertRight(15)
tree.insertRight(49)
tree.getRightChild().insertLeft(23)
tree.getRightChild().insertRight(52)
tree.getRightChild().getRightChild().insertLeft(50)

print sumLeftLeaves(tree)
Esempio n. 3
0
    rightLeft.rightChild = middle
    middle.rightChild = right
    right.leftChild = middle

    # complete circle
    left.leftChild = rightRight
    rightRight.rightChild = left

    # return head
    return left


def traverse(head):
    current = head
    print current.getRootVal()
    current = current.rightChild
    while (current != head):
        print current.getRootVal()
        current = current.rightChild


tree = binTree.BinaryTree(10)
tree.insertLeft(9)
tree.insertRight(12)
tree.getLeftChild().insertLeft(15)
tree.getLeftChild().insertRight(6)
tree.getRightChild().insertLeft(16)
tree.getRightChild().insertRight(18)
l = treeToList(tree)
traverse(l)
Esempio n. 4
0
            # odd level, pop front, push rear
            while (numProcess > 0 and l):
                t = l.pop(0)
                if (t):
                    print t.getRootVal()
                    l.append(t.getLeftChild())
                    l.append(t.getRightChild())
                numProcess -= 1
            level += 1
        else:
            # even level, pop rear, push front
            while (numProcess > 0 and l):
                t = l.pop()
                if (t):
                    print t.getRootVal()
                    l.insert(0, t.getRightChild())
                    l.insert(0, t.getLeftChild())
                numProcess -= 1
            level += 1


t = binTree.BinaryTree(1)
t.insertLeft(2)
t.insertRight(3)
#t.getLeftChild().insertLeft(7)
t.getLeftChild().insertRight(6)
t.getRightChild().insertLeft(5)
#t.getRightChild().insertRight(4)

levelOrderTraversalSpiral(t)
Esempio n. 5
0
import binTree

def equalTrees(t1, t2):
    if (t1 == None or t2 == None):
        return compNoneTrees(t1,t2)
    else:
        if (t1.getRootVal() != t2.getRootVal()):
            return False
        else:
            return equalTrees(t1.getLeftChild(), t2.getLeftChild()) and equalTrees(t1.getRightChild(), t2.getRightChild())

def compNoneTrees(t1,t2):
    return (t1 == None and t2 == None)


t1 = binTree.BinaryTree(1)
t1.insertLeft(2)
t1.insertRight(3)
t1.getLeftChild().insertLeft(4)
t1.getLeftChild().insertRight(5)

t2 = binTree.BinaryTree(1)
t2.insertLeft(2)
t2.getLeftChild().insertLeft(4)
t2.getLeftChild().insertRight(5)

print equalTrees(t1,t2)