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

Similar documents
Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

Formal Languages and Automata Theory, SS Project (due Week 14)

PA3 Design Specification

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

DEEPIKA KAMBOJ UNIT 2. What is Stack?

CS 206 Introduction to Computer Science II

Linear Data Structure

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Bachelor Level/ First Year/ Second Semester/ Science Full Marks: 60 Computer Science and Information Technology (CSc. 154) Pass Marks: 24

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

ASSIGNMENTS. Progra m Outcom e. Chapter Q. No. Outcom e (CO) I 1 If f(n) = Θ(g(n)) and g(n)= Θ(h(n)), then proof that h(n) = Θ(f(n))

Infix to Postfix Conversion

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

STACKS AND QUEUES. Problem Solving with Computers-II

Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

INSTITUTE OF AERONAUTICAL ENGINEERING

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Tribhuvan University Institute of Science and Technology Computer Science and Information Technology (CSC. 154) Section A Attempt any Two questions:

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

Backtracking. The Eight Queens Problem. The Eight Queens Problem. The Eight Queens Problem. The Eight Queens Problem. Chapter 5

Types of Data Structures

Recursion. Tracing Method Calls via a Stack. Beyond this lecture...

CS W3134: Data Structures in Java

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

17CS33:Data Structures Using C QUESTION BANK

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Stacks. Revised based on textbook author s notes.

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

Lecture Data Structure Stack

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Backtracking. Module 6. The Eight Queens Problem. CS 147 Sam Houston State University Dr. McGuire. Backtracking. Recursion Revisited

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Stacks, Queues and Hierarchical Collections

Top of the Stack. Stack ADT

8/22/12. Outline. Backtracking. The Eight Queens Problem. Backtracking. Part 1. Recursion as a Problem-Solving Technique

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

Discuss the following operations on One-Dimensional array with algorithms.

Programming Abstractions

The Stack and Queue Types

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

BBM 201 DATA STRUCTURES

Stacks. CONTENTS 3.1 Introduction 1. Stack as an abstract data type 2. Representation of a Stack as an array 3.2Applications of Stack.

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

BBM 201 DATA STRUCTURES

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Recursion as a Problem-Solving Technique. Chapter 5

Postfix (and prefix) notation

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Data Structures. 1. Each entry in a linked list is a called a (a)link (b)node (c)data structure (d)none of the above

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

CS 206 Introduction to Computer Science II

CS302 - Data Structures using C++

CmpSci 187: Programming with Data Structures Spring 2015

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Content: Learning Objectives

Lab 7 1 Due Thu., 6 Apr. 2017

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

Introduction to Computer and Program Design 2. Lesson 6. Stacks. James C.C. Cheng Department of Computer Science National Chiao Tung University

Top of the Stack. Stack ADT

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

March 13/2003 Jayakanth Srinivasan,

Data Structures Week #3. Stacks


CS 171: Introduction to Computer Science II. Stacks. Li Xiong

F453 Module 7: Programming Techniques. 7.2: Methods for defining syntax

CS200: Recursion and induction (recap from cs161)

Prefix/Infix/Postfix Notation

Bharati Vidyapeeth s College Of Engineering for Women Pune-43 Department E & TC. SE- Unit Test I Subject-DS

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

recursive algorithms 1

1. Algorithm. Data Structures & Algorithms. How many times you weight? Algo 2 : Half Cutting. How many times you weight? Algo.1 :Two coins at a time

Recursion (Rosen, 6 th edition, Section 4.3, 4.4)

DATA STRUCTURE UNIT I

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

Computer Science. Section 1B

An Introduction to Trees

12/30/2013 S. NALINI,AP/CSE

Table of Contents. Chapter 1: Introduction to Data Structures... 1

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

Massachusetts Institute of Technology Department of Mechanical Engineering

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

Transcription:

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