I have a pseudo code that Im tracing Tried it with pen and p
I have a pseudo code that I\'m tracing. Tried it with pen and paper and I still can\'t figure it out and having trouble. Can someone please explain to me step by step what happens each line? Thanks in advance!
public int Evaluate 2 if(root null) return -999; 4 Stack S Evaluate (root); 6 return S. pop 9 private static Stack Evaluate (bnode t)t 10 if (t null) S Evaluate (t.left); 12 S Evaluate (t.right); 14 if(t.data S. push (S.pop() S. pop 16 else if (t.data S push (S.pop() S. pop()); 18 19 else if (t.data \"-\")t S. pop() S. pop(); S. push (b a); else S. push (t.data); return SSolution
Here first function Evaluate is checking following:
If the value of root is null then it return - 999
else calling another function Evaluate passing root as parameter to it and return S.pop() function which is used to remove top element from Stack
Now second function Evaluate(bnode t)
it first check the passed node is not null and if not then calls the same function passing the left of the node t and another call to same function with right of t.Basically if the provided t is not null then it recursively calls the left and right of node till it reaches end.
Now while traversing through each node it will check the data/value of that node.
and if its \'+\' then it takes two elements (S.pop()+S.pop()) add them and put to the stack (S.push())
else if its \'*\' then take two elements((S.pop()*S.pop())) multiply them and put to the stack (S.push())
else if its \'-\' then take two elements in two variables a and b and push result of b-a to stack.
else just simply push the value to stack.
In short if we say its creating a tree and pushing the values after evaluating as per above expalined logic
