Computer Science Canada Building a binary tree |
| Author: | efus [ Fri Nov 28, 2008 5:26 am ] | ||
| Post subject: | Building a binary tree | ||
Hi. I am working on a project where I convert postfix notation to infix and evaluateing it. This includes building an evaluations tree (binary tree). I get how to create nodes, but how do I get the whole tree? This is the code I have so far:
|
|||
| Author: | jernst [ Fri Nov 28, 2008 10:08 am ] |
| Post subject: | Re: Building a binary tree |
A couple suggestions so far: - Your "for loop": instead of hardcoding 3 into the loop you might want to consider using the size of the operators, in case you wish to add more operators later one - Also when you are checking if the current token is an operator, you should maybe move the else if part out of the for loop, because otherwise for every operator you have it will also check if it is a digit. For example, say what you actually have is the last operator in the list. It will look for each of the first operators, plug check each time if it is a digit. As for building the tree, you will want to keep track of a root node somewhere and then you need to figure out where to stick the children from there (I think you need to use one of the operators as the root and have the children as your digits as well as other operators as the roots of subtrees) But it also depends on the priority of the operation as well |
|