# Lab 1: 1. Download all my programs in your computer under the same folder. 2. The tree shown in the following figure represents an expression: (((( 3 +

## Presentation on theme: "Lab 1: 1. Download all my programs in your computer under the same folder. 2. The tree shown in the following figure represents an expression: (((( 3 +"— Presentation transcript:

Lab 1: 1. Download all my programs in your computer under the same folder. 2. 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.

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

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('-'));

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));

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

3. Write a Java program to implement Preorder seraching of a binary tree. 4. Write a Java program to implement Postorder seraching of a binary tree. 5. Write a Java program to implement breadth-first seraching of a binary tree. public interface Queue { public void enqueue(Object element) throws QueueFullException; public Object dequeue() throws QueueEmptyException; public int size(); public boolean isEmpty(); public Object front() throws QueueEmptyException; }//queue

public class ArrayQueueCircular implements Queue{ public static final int ARRAYSIZE = 100; protected int arraysize; protected Object Q[]; protected int front; protected int rear; public ArrayQueueCircular(int sizemax) { arraysize = sizemax; Q = new Object[arraysize]; }//ArrayQueueCircular // Initialize the stack to use an array of default length ARRAYSIZE. public ArrayQueueCircular() { this(ARRAYSIZE); } public int size(){ return ((arraysize - front + rear) % arraysize); }// size public boolean isEmpty(){ return (front == rear); }//isEmpty

public Object front() throws QueueEmptyException { if (isEmpty()) throw new QueueEmptyException( "Queue is empty."); return Q[ front ]; }//front public void enqueue(Object obj) throws QueueFullException { if ( size() == (arraysize - 1)) throw new QueueFullException( "Queue is full." ); Q[ rear ] = obj; rear = ( rear + 1 ) % arraysize; }// enqueue public Object dequeue() throws QueueEmptyException { Object temp; if (isEmpty()) throw new QueueEmptyException( "Queue is empty - unable to dequeue." ); temp = Q[ front ]; Q[ front ] = null; front = ( front + 1) % arraysize; return temp; }// dequeue }//class ArrayQueueCircular

Download ppt "Lab 1: 1. Download all my programs in your computer under the same folder. 2. The tree shown in the following figure represents an expression: (((( 3 +"

Similar presentations