1 The purpose of the exercise is to practice building and tr

1. The purpose of the exercise is to practice building and traversing an expression tree; so I want you to build a tree to hold the expression, and to evaluate the expression by traversing the tree. you have to build the expression tree, then traverse it to evaluate the expression. for each expression, you must produce - a graphical representation of the tree, - the numeric value of the expression.

Solution

public class treNode {
int val;
treNode left;
treNode right;
treNode(int x) { val = x; }
}

public class sltn {
public ArrayList<Integer> inorderTraversal(treNode root) {
  
ArrayList<Integer> lst = new ArrayList<Integer>();

if(root == null)
return lst;

Stack<treNode> stck = new Stack<treNode>();
  
treNode p = root;

while(!stck.empty() || p != null){

if(p != null){
stck.push(p);
p = p.left;

}else{
treNode t = stck.pop();
lst.add(t.val);
p = t.right;
}
}

return lst;
}
}

1. The purpose of the exercise is to practice building and traversing an expression tree; so I want you to build a tree to hold the expression, and to evaluate

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site