def _constructHelper(self, iStart, iEnd):
        if iEnd < iStart:
            return None

        root = BinaryTree(self._postorder[self.pIndex])
        self.pIndex -= 1

        if iStart == iEnd:
            return root

        inIndex = self._inorder.index(root.getRootVal())

        root.setRightChild(self._constructHelper(inIndex + 1, iEnd))
        root.setLeftChild(self._constructHelper(iStart, inIndex - 1))

        return root
    def _constructHelper(self, levelorder, inorder):

        if not levelorder:
            return None

        root = BinaryTree(levelorder[0])

        if len(levelorder) == 1:
            return root

        inIndex = inorder.index(root.getRootVal())

        left = inorder[:inIndex]
        right = inorder[inIndex+1:]

        root.setLeftChild(self._constructHelper(self._getSubList(levelorder, left), left))
        root.setRightChild(self._constructHelper(self._getSubList(levelorder, right), right))

        return root
    def _constructHelper(self, iStart, iEnd):

        if iStart > iEnd:
            return None

        root = BinaryTree(self._preorder[self.pIndex])
        self.pIndex += 1

        if iStart == iEnd:
            return root

        preIndex = self._postorder.index(self._preorder[self.pIndex])

        root.setLeftChild(self._constructHelper(iStart, preIndex))
        root.setRightChild(self._constructHelper(preIndex + 1, iEnd - 1))

        return root
Exemple #4
0
def buildParseTree(exp):
    tree = BinaryTree('')
    stack = []
    stack.append(tree)
    currentTree = tree
    exp = exp.split()
    for i in exp:
        if i == '(':
            currentTree.insertLeft('')
            stack.append(currentTree)
            currentTree = currentTree.getLeftChild()
        elif i not in ['+', '-', '*', '/', ')']:
            currentTree.setRootVal(i)
            currentTree = stack.pop()
        elif i in ['+', '-', '*', '/']:
            currentTree.insertRight('')
            currentTree.setRootVal(i)
            stack.append(currentTree)
            currentTree = currentTree.getRightChild()
        elif i == ')':
            currentTree = stack.pop()
        else:
            raise ValueError
    return tree
        while numberOfNodes > 0:
            node = queue.pop(0)
            nodesSameLevel.append(node)

            if node.getLeftChild():
                queue.append(node.getLeftChild())
            if node.getRightChild():
                queue.append(node.getRightChild())

            numberOfNodes -= 1

        print(printNodesByLevel(nodesSameLevel))


def printNodesByLevel(nodes):
    values = []
    for node in nodes:
        values.append(node.getRootVal())

    return ' '.join('({:d})'.format(x) for x in values)


tree = BinaryTree(1)
tree.insertLeft(2)
tree.insertRight(3)
tree.getLeftChild().insertLeft(4)
tree.getLeftChild().insertRight(5)
tree.getRightChild().insertRight(6)

printEachTreeLevel(tree)
    # print nodes in ascending order
    if tree:
        inorder(tree.getLeftChild())
        print(tree.getRootVal())
        inorder(tree.getRightChild())


def preorder(tree):
    # print root node first
    if tree:
        print(tree.getRootVal())
        preorder(tree.getLeftChild())
        preorder(tree.getRightChild())


tree = BinaryTree(2)
tree.insertLeft(1)
tree.insertRight(3)
tree.getLeftChild().insertLeft(0)
tree.getLeftChild().insertLeft(7)
tree.getRightChild().insertLeft(9)
tree.getRightChild().insertRight(1)
"""
        2
      /   \
     1     3
    / \   / \
   0   7 9   1

"""
preorder(tree)