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) 1b) 2c) 3d) 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) 1b) 2c) 3d) 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=0b) Front=Back=-1 c) Front=Back+1d) 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=0b) Front=Back=-1 c) Front=Back+1d) Front=(Bear+1)% SIZE
Consider the following operations on a Queue. After the codebelow 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) 0b) 4c) 5d) 6
Consider the following operations on a Queue. After the codebelow 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) 0b) 4c) 5d) 6
What is the reason for using a "circular queue" instead of a regular one?
a) The running time will improveb) Can traverse all the elements easily c) Re-Use empty spacesd) None of the above
What is the reason for using a "circular queue" instead of a regular one?
a) The running time will be improveb) Can traverse all the elements easily c) Re-Use empty spacesd) 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
a2+b2
=c2C=π⋅d=2⋅π⋅r
E=mc2 f (x )=1
σ √2πe−
( x−μ)2
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
23
TOH Solution (N=No. of Disks)
moveTower(N, Source, Spare, Dest)IF n==1move that disk from Source to Dest
ELSEmoveTower(N-1, Source, Dest, Spare)moveTower(1, Source, Spare, Dest)moveTower(N-1, Spare, Source, Dest)
TOH Solution (N=No. of Disks)
moveTower(N, Source, Spare, Dest)IF n==1move that disk from Source to Dest
ELSEmoveTower(N-1, Source, Dest, Spare)moveTower(1, Source, Spare, Dest)moveTower(N-1, Spare, Source, Dest)
O(2n)
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: 23-1=7 Steps● 4 Disks: 24-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(2n)
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)=2n-1 for all n PROVED
26
…?
Thank You
27
Top Related