ops
for operators and nums
for operand nodes.s
.nums
stack.+
, -
, *
, /
):
a. While the ops
stack is not empty and the top operator has greater or equal precedence, pop the operator from ops
and pop two nodes from nums
, create a new node with these as children, and push the new node onto nums
.
b. Push the current operator onto the ops
stack.(
, push it onto the ops
stack.)
, keep resolving the top operator of the ops
stack until an opening parenthesis is encountered.ops
stack, resolve them.nums
stack is the root of the binary expression tree.