|
Algorithm 1 The process of pruning tree construction |
Input: postfix form of the logical expression pfix; empty stack stack; the value of the node value; child node children; the number of nodes in the subtree num_children; parent node parent Output: pruning tree
-
1:
Class ExpTree: //Constructing nodes in an expression tree
-
2:
-
3:
procedure add_child(children): //Adding child nodes to a subtree
-
4:
if children is a list then: //The children is a list
-
5:
for each ch in children do:
-
6:
ch.parent ← this //Set the current node (this) to be the parent of the child ch
-
7:
end for
-
8:
append children to this.children
-
9:
else: //The children is just a node
-
10:
children.parent ← this //Set the current node (this) to be the parent of children
-
11:
append children to this.children
-
12:
procedure constructTree(pfix):
-
13:
for i from length of pfix to 0 step do //Iterate over pfix in reverse order
-
14:
if isOperand(pfix[i]) then //pfix[i] is the operand
-
15:
node ← new ExpTree(pfix[i]) //Use pfix[i] to create a new ExpTree node
-
16:
push node to stack
-
17:
else: //pfix[i] is the operator
-
18:
v1 ← pop from stack //v1 is the current operand or subtree
-
19:
v2 ← pop from stack //v2 is the subtree generated in the previous loop
-
20:
nn ←new ExpTree(pfix[i]) //nn is the root node
-
21:
if v1.value pfix[i] then //v1 is equal to the current operator pfix[i]
-
22:
nn.add_child(v1.children) //Constructing the pruning tree
-
23:
else:
-
24:
nn.add_child(v1)
-
25:
if v2.value pfix[i] then //pfix[i] is equal to the operator of subtree v2
-
26:
nn.add_child(v2.children) //Constructing the pruning tree
-
27:
else:
-
28:
nn.add_child(v2)
-
29:
push nn to stack
-
30:
end for
-
31:
return purning tree ← pop from stack
|