convert a fully paranthesized, arithmetic expression to a binary tree
1) Convert the infix expression to postfix expression
e.g. (a+b)*c = ab+c* ( you can get the standard infix to postfix algos )
2) Then use a stack to build the tree
Algo:
1)check the current char
i)if its a operand make a tree node with data as the char and put it in the stack
ii) if its an operator make a node with data as the operator. pop 2 elements from the stack(these has to be operand as we use postfix expr). make them the children of the current node. Push the current node back on the stack
2) if the input is over pop the content of the stack .... it is the root of the binary expression tree
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment