1

For more basic information (for starters)... you may want to read this question: How to Implement a Binary Tree?

For more information on how to implement a binary tree... you may want to open this site: http://www.openbookproject.net/thinkcs/python/english2e/ch21.html

The binary operation tree is way different from its father... the Binary Tree. This is the expression tree for the string '(7+3)(5-2):Expression Tree for (7+3)(5-2).

The snippet way way below should do the following...

  • Read a string
  • When it encounters '(', then it creates a new node and goes to the left child of the current node
  • When it encounters any digit, it places the data then goes up
  • When it encounters an operator '+','-','*','/', then it places the data then goes to the right child of the current node
  • When it encounters ')', then it backtracks and goes up.

There are few problems with this code that need to be solved...

  • the int(strng) does not function well in this code: BinaryOperationTree instance has no attribute 'len'

Or maybe you may add your own code to answer this question... If you do this, then you may disregard the code below. Take note that it should print the nodes infix, prefix, and postfix...

        class Node:

            def __init__ (self, data, currNode):
                self.data = data
                self.left = None
                self.right = None
                self.parent = currNode
                currNode = self.right
                return currNode

        class BinaryOperationTree():

            def __init__(self):
                strng = raw_input('Please enter the operation to turn it into a binary tree.\n')
                print strng


            def readCharacter(self):
                for i in range(-1, str_len, -1):                     
                    k = k + 1
                    if (k >= 2):
                        j = j * 16
                l = 0                                                   #Counter
                currNode = Node
                while (l <= k - 1):                                     
                    if isDigit(hexa[l]):
                        encntrDigit(hexa[l], currNode)
                    elif isRPar(hexa[l]):
                        enctrRpar(currNode)
                    elif isLPar(hexa[l]):
                        enctrLpar(currNode)
                    elif isOperator(hexa[l]):
                        enctrOperator(hexa[1], currNode)

                def isDigit(x):
                    chars = ['0','1','2','3','4','5','6','7','8','9']
                    if chars in x:
                        return True
                    else:
                        return False

                def isRPar(x):
                    if ')' in x:
                        return True
                    else:
                        return False

                def isLPar(x):
                    if '(' in x:
                        return True
                    else:
                        return False

                def isOperator(x):
                    chars = ['+','-','*','/']
                    if chars in x:
                        return True
                    else:
                        return False

                def encntrDigit(x, currNode):
                    currNode.data = x
                    currNode = currNode.parent
                    return currNode

                def enctrRpar(self, currNode):
                    currNode = currNode.parent

                def enctrLPar(self, currNode):
                    currNode = Node()

                def enctrOperator(self, x, currNode):
                    currNode.data = x
                    currNode = currNode.parent

                #Prints the tree in Pre-Order Format
                def preOrder (node):

                    if (node is not None):
                        print node.data, "",
                        preOrder(node.left)
                        preOrder(node.right)

                #Prints the tree in Order Format   
                def inOrder (node):

                    if (node is not None):
                        inOrder (node.left)
                        print node.data, "",
                        inOrder (node.right)

                #Prints the tree in Post-Order Format
                def postOrder (node):

                    if (node is not None):
                        postOrder (node.left)
                        postOrder (node.right)
                        print node.data, "",


        def main():
            string = BinaryOperationTree()
            string.readCharacter()

        main()
Community
  • 1
  • 1

0 Answers0