Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi
? 2
Consider the following operation performed on a stack of size 5. Push(1); Pop(); Push(2); Push(3); Pop(); Push(4); Pop(); Pop(); Push(5); a) 1 b) 2 c) 3 d) 4
Consider the following operation performed on a stack of size 5. Push(1); Pop(); Push(2); Push(3); Pop(); Push(4); Pop(); Pop(); Push(5); a) 1 b) 2 c) 3 d) 4
If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to the first element in the queue, and back point to the last element in the Queue. Which of the following condition specify that circular queue is completely EMPTY? a) Front=Back=0 b) Front=Back=-1 c) Front=Back+1 d) Front=(Back+1)% SIZE
If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to the first element in the queue, and back point to the last element in the Queue. Which of the following condition specify that circular queue is completely EMPTY? a) Front=Back=0 b) Front=Back=-1 c) Front=Back+1 d) Front=(Bear+1)% SIZE
Consider the following operations on a Queue. After the code below executes, how many elements would remain in the queue? Queue q = new Queue( ); q.enqueue(3); q.enqueue(5); q.enqueue(9); q.dequeue( ); q.enqueue(2); q.enqueue(4); q.dequeue( ); q.dequeue( ); q.enqueue(1); q.enqueue(8); a) 0 b) 4 c) 5 d) 6
Consider the following operations on a Queue. After the code below executes, how many elements would remain in the queue? Queue q = new Queue( ); q.enqueue(3); q.enqueue(5); q.enqueue(9); q.dequeue( ); q.enqueue(2); q.enqueue(4); q.dequeue( ); q.dequeue( ); q.enqueue(1); q.enqueue(8); a) 0 b) 4 c) 5 d) 6
What is the reason for using a "circular queue" instead of a regular one? a) The running time will improve b) Can traverse all the elements easily c) Re-Use empty spaces d) None of the above
What is the reason for using a "circular queue" instead of a regular one? a) The running time will be improve b) Can traverse all the elements easily c) Re-Use empty spaces d) None of the above
Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi
Agenda Infix, Postfix, Prefix Postfix & Prefix Calculation using Stack Tower of Hanoi 12
Algebraic Expression An algebraic expression is a legal combination of operands and the operators Operand is the quantity on which a mathematical operation is performed Operand may be a variable like x, a, z or a constant like 3,7,8,1,0 Operator: +, -, / 13
a 2 +b 2 =c 2 C=π d=2 π r E=mc 2 f (x)= 1 ( x μ) 2 σ 2π e 2σ 2 14
Infix, Postfix, Prefix Infix: The expressions in which operands surround the operator. Example: X+Y Postfix: Reverse Polish Notation(RPN) Operators are written after their operands. Example: XY+ Prefix: Polish Notation(PN) Operators are written before their operands. Example: +XY 15
Infix Postfix Prefix A*B+C/D AB*CD/+ +*AB/CD A*(B+C)/D ABC+*D/ /*A+BCD A*(B+C/D) ABCD/+* *A+B/CD 16
A+B/C*(D+E)-F (X+Y/Z*(A+B)-C) A*B^C+D (a+b+c)*(e/f)+(g-h/i) (4+8)*(6-5)/(3-2)*(2+2) 17
Infix to Postfix using Stack Order: ( ) { } [ ], Exponent, Multiplication, Divide, A, S ^: Priority 3 *, /: Priority 2 +,-: Priority 1 No two operators of same priority can stay together If operator is in between ( and ), that will be popped out from stack 18
Infix: A*B^C+D Characters Stack Postfix A Empty A * * A B * AB ^ * ^ AB C * ^ ABC + * ^ + ABC^ ABC^* D + ABC^*D Postfix: ABC^*D+
Infix to Prefix using Stack Procedure Reverse the infix Calculate postfix Then, Reverse again Infix: (A*B)+C C+(B*A) Postfix: CBA*+ Prefix: +*ABC 20
Tower of Hanoi Tower of Brahma(Lucas Tower) 1883: Edouard Lucas 21
movetower(n, Source, Spare, Dest): General Notation Solution movetower(n-1, Source, Dest, Spare) movetower(1, Source, Spare, Dest) movetower(n-1, Spare, Source, Dest) 22
TOH Solution (N=No. of Disks) O(2 n ) movetower(n, Source, Spare, Dest) IF n==1 move that disk from Source to Dest ELSE movetower(n-1, Source, Dest, Spare) movetower(1, Source, Spare, Dest) movetower(n-1, Spare, Source, Dest) 23
public class towerhanoi { public towerhanoi() { Scanner sc=new Scanner(System.in); System.out.print("Enter Number Of Discs:"); int n=sc.nextint(); TOH(n,1,2,3); } public void TOH(int n, int A, int B, int C) { if(n>0){ TOH(n-1,A,C,B); System.out.println("Move Disk "+n +" from Tower "+ A +" to Tower "+C); TOH(n-1,B,A,C); } } public static void main(string args[]) { towerhanoi obj=new towerhanoi(); } } 24
3 Disks: 2 3-1=7 Steps 4 Disks: 2 4-1= 15 Steps The no of moves doubles if we add a single disk Increasing disks from 1 to n, the computation will double at each stage So O(2 n ) 25
Proof by Induction Claim: f(n)=2n -1 Basis: Suppose n=1, f(1)=1 (TRUE) Suppose n=3, f(3)=7 (TRUE) Induction Step The claim is true for n. So, It must support for n+1 too. f(n)=2 n -1 for all n PROVED 26
? Thank You 27