My MIPS Assembly class required me to read in an expression of unknown size into a Parse Tree. I've never had to deal with trees, so this is how I went around storing values:
Lets say the user entered the expression 1 + 3 - 4 (each operand could only be a digit 1-9)
My leftmost child node would be the starting point and contain 2 pieces of data
1. The operand
2. Pointer to the next node (operator)
This is how I constructed the tree. I would point from operand to operator to next operand to next operator until there were no more values left to be read in.
My next task was to traverse the tree recursively and output the values in infix/prefix/postfix notation.
Infix traversal was no problem considering how I constructed my tree.
I'm stuck on the prefix. Firstly, I don't fully understand it.
When I output our expression (1 + 3 - 4) in prefix should it come out - + 1 3 4? I'm having trouble following the online examples.
Also do you think my tree is constructed properly? I mean, I have no way to go to a previous node from the current node which means I always have to begin traversal from the leftmost child node which instinctively doesn't sound right even though my TA said that was the way to go.
Thank you for any help.