Last active
August 29, 2015 14:07
-
-
Save alexandervasyuk/86f565825498c28252c9 to your computer and use it in GitHub Desktop.
Simple infixToBinaryTree Pseudocode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Declare BinaryTreeNode result; | |
While there are tokens to be read: | |
Read a token. | |
If the token is a number, then add it to the output stack. | |
If the token is an operator, o1, then: | |
while there is an operator token, o2, at the top of the operator stack, and | |
o1 has precedence less than or equal to that of o2, | |
operator = pop o2 off the operator stack; | |
if result is null: | |
set result's data to operator | |
set result's left child to output stack pop() | |
set result's right child to output stack pop() | |
else | |
BinarTreeNode right = result; | |
result = new BinaryTreeNode | |
set result's data to operator | |
set result's right to right | |
set result's left to output stack pop() | |
push o1 onto the operator stack. | |
When there are no more tokens to read: | |
While there are still operator tokens in the stack: | |
if result is null: | |
set result's data to operator | |
set result's left child to output stack pop() | |
set result's right child to output stack pop() | |
else | |
BinarTreeNode right = result; | |
result = new BinaryTreeNode | |
set result's data to operator | |
set result's right to right | |
set result's left to output stack pop() | |
Return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment