Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in...

27
Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi

Transcript of Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in...

Page 1: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi

Page 2: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

…?

2

Page 3: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

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

Page 4: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

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

Page 5: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

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

Page 6: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

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

Page 7: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

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

Page 8: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

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

Page 9: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

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

Page 10: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

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

Page 11: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi

Page 12: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

Agenda● Infix, Postfix, Prefix● Postfix & Prefix Calculation using Stack● Tower of Hanoi

12

Page 13: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

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

Page 14: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

a2+b2

=c2C=π⋅d=2⋅π⋅r

E=mc2 f (x )=1

σ √2πe−

( x−μ)2

2σ2

14

Page 15: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

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

Page 16: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

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

Page 17: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

● 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

Page 18: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

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

Page 19: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

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+

Page 20: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

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

Page 21: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

Tower of Hanoi● Tower of Brahma(Lucas Tower)● 1883: Edouard Lucas

21

Page 22: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

● 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

Page 23: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

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)

Page 24: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

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

Page 25: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

● 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

Page 26: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

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

Page 27: Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi · If the SIZE is the size of the array used in the implementation of circular queue, array index start with 0, front point to

…?

Thank You

27