Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab class 9: 1. The tree shown in the following figure represents an expression: (((( 3 + 1 ) * 3 ) / (( 9 - 5 ) + 2 )) - (( 3 * ( 7 - 4 )) + 6 )) Please.

Similar presentations


Presentation on theme: "Lab class 9: 1. The tree shown in the following figure represents an expression: (((( 3 + 1 ) * 3 ) / (( 9 - 5 ) + 2 )) - (( 3 * ( 7 - 4 )) + 6 )) Please."— Presentation transcript:

1 Lab class 9: 1. The tree shown in the following figure represents an expression: (((( 3 + 1 ) * 3 ) / (( 9 - 5 ) + 2 )) - (( 3 * ( 7 - 4 )) + 6 )) Please write a Java program to generate this tree and then compute the expression using the postorder searching method.

2 public class ExpressionCalculation { public static void main(String[] args) { generate a tree; call ArithCalculation(…) { … } … } public static double ArithCalculation(LinkedBinaryTree t, BTNode v) { …} }

3 public class ArrayQueueCircular implements Queue{ LinkedBinaryTree tree = new LinkedBinaryTree(); Position p0 = tree.addRoot(new Character('-')); Position p1 = tree.insertLeft(p0, new Character('/')); Position p2 = tree.insertRight(p0, new Character('+')); Position p3 = tree.insertLeft(p1, new Character('*')); Position p4 = tree.insertRight(p1, new Character('+')); Position p5 = tree.insertLeft(p2, new Character('*')); Position p6 = tree.insertRight(p2, new Integer(6)); Position p7 = tree.insertLeft(p3, new Character('+')); Position p8 = tree.insertRight(p3, new Integer(3)); Position p9 = tree.insertLeft(p4, new Character('-'));

4 Position p10 = tree.insertRight(p4, new Integer(2)); Position p11 = tree.insertLeft(p5, new Integer(3)); Position p12 = tree.insertRight(p5, new Character('-')); Position p13 = tree.insertLeft(p7, new Integer(3)); Position p14 = tree.insertRight(p7, new Integer(1)); Position p15 = tree.insertLeft(p9, new Integer(9)); Position p16 = tree.insertRight(p9, new Integer(5)); Position p17 = tree.insertLeft(p12, new Integer(7)); Position p18 = tree.insertRight(p12, new Integer(4));

5 Algorithm ArithCalculation(T, v) – return a number Begin if v is a leaf node, then return v’s value; else { a = ArithCalculation(T, v’s leftChild); b = ArithCalculation(T, v’s rightChild); if v == ‘+’, then return a + b; if v == ‘-’, then return a - b; if v == ‘*’, then return a * b; if v == ‘/’, then return a / b; } End (*It is better to first download all the Java programs from my home page.)

6 2. Write a Java program to implement Preorder searching of a binary tree. public class PreOrderSreachTree { public static void preOrderSearchTree(LinkedBinaryTree tree){ Position v=tree.root(); ArrayStack d = new ArrayStack(); d.push(v);//push root while(!d.isEmpty()){ v=(Position)d.pop(); if(tree.hasRight(v)) d.push(tree.right(v)); if(tree.hasLeft(v)) d.push(tree.left(v)); visit(v);//calls the method visit } /** This Method calls System.out.println() method to print out the element **/ public static void visit(Object obj){ System.out.println(((BTPosition)((Position)obj)).element()); } public static void main(String [] args){ … }


Download ppt "Lab class 9: 1. The tree shown in the following figure represents an expression: (((( 3 + 1 ) * 3 ) / (( 9 - 5 ) + 2 )) - (( 3 * ( 7 - 4 )) + 6 )) Please."

Similar presentations


Ads by Google