Data Structure. Recitation VII

Similar documents
STACKS AND QUEUES. Problem Solving with Computers-II

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

ITI Introduction to Computing II

1 P age DS & OOPS / UNIT II

Stack and Queue. Stack:

PA3 Design Specification

.:: UNIT 4 ::. STACK AND QUEUE

LIFO : Last In First Out

Data Structure Advanced

Postfix (and prefix) notation

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

csci 210: Data Structures Stacks and Queues

Information Science 2

CSC 1052 Algorithms & Data Structures II: Queues

CS 206 Introduction to Computer Science II

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

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

More Data Structures (Part 1) Stacks

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

Queues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

Chapter 5. ADTs Stack and Queue

Chapter 18: Stacks And Queues

ADTs Stack and Queue. Outline

Sequence Abstract Data Type

Data Structures -- Introduction

CS 206 Introduction to Computer Science II

Name CPTR246 Spring '17 (100 total points) Exam 3

// The next 4 functions return true on success, false on failure

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

CS24 Week 4 Lecture 2

CMSC 132: Object-Oriented Programming II. Stack and Queue

Announcements Queues. Queues

Chapter 9 STACK, QUEUE

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues


Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Chapter 18: Stacks And Queues

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

DATA STRUCTURE UNIT I

Computer Science 501 Data Structures & Algorithms The College of Saint Rose Fall Topic Notes: Linear Structures

Stacks and queues (chapters 6.6, 15.1, 15.5)

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

CSCA48 Term Test 1 Seminar

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

Linked Lists, Stacks, and Queues

Another common linear data structure similar to. new items enter at the back, or rear, of the queue. Queue is an ADT with following properties

Lecture 4 Stack and Queue

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

CSE 331 Midterm Exam 2/13/12

Lecture Data Structure Stack

How can we improve this? Queues 6. Topological ordering: given a sequence of. should occur prior to b, provide a schedule. Queues 5.

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

A queue is a linear collection whose elements are added on one end and removed from the other

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

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

! A data type for which: ! An ADT may be implemented using various. ! Examples:

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Separate Compilation and Namespaces Week Fall. Computer Programming for Engineers

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

Linked Structures. See Section 3.2 of the text.

Lecture 3: Stacks & Queues

CSE 331 Midterm Exam Sample Solution 2/13/12

III Data Structures. Dynamic sets

CSE 373: Data Structures and Algorithms

Queue with Array Implementation

table1 = [ [1, 2, 3, 4], [4, 5, 6, 7], [8, 9,10,11] ] we get: >>> sumcol(table1,0) 13 >>> sumcol(table1,1) 16

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

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Basic Data Structures

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Overview of Data. 1. Array 2. Linked List 3. Stack 4. Queue

Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems. COMP Week 8 & 9 1

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Matriculation number:

Answer D) ADT does not depend on the implementation

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

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

CS 222: Linked Lists, Queues, Stacks

CS S-04 Stacks and Queues 1. An Abstract Data Type is a definition of a type based on the operations that can be performed on it.

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

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

cs Java: lecture #6

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

Data Structures and Algorithms

COSC 1P03. Ch 6 Generics. Introduction to Data Structures 7.1

Basic Data Structures 1 / 24

Object Oriented Programming COP3330 / CGS5409

Transcription:

Data Structure Recitation VII

Recursion: Stack trace Queue Topic

animation Trace Recursive factorial Executes factorial(4) Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) Step 7: return 2 return 3 * factorial(2) return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 6: return 1 Step 3: executes factorial(1) return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 Main method 3

animation Trace Recursive factorial factorial(4) Step 9: return 24 Step 8: return 6 Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) Executes factorial(3) Step 7: return 2 Step 6: return 1 return 2 * factorial(1) Step 2: executes factorial(2) Step 3: executes factorial(1) Stack return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 Main method 4

animation Trace Recursive factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) Executes factorial(2) Step 7: return 2 Step 6: return 1 return 2 * factorial(1) Step 2: executes factorial(2) Step 3: executes factorial(1) Stack return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(3) Main method 5

animation Trace Recursive factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) Executes factorial(1) Step 7: return 2 return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(2) for factorial(3) Main method 6

animation Trace Recursive factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) Executes factorial(0) Step 7: return 2 return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(1) for factorial(2) for factorial(3) Main method 7

animation Trace Recursive factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) returns 1 Step 2: executes factorial(2) Step 7: return 2 return 2 * factorial(1) Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 Stack for factorial(0) for factorial(1) for factorial(2) for factorial(3) Main method 8

animation Trace Recursive factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) returns factorial(0) Step 7: return 2 return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(1) for factorial(2) for factorial(3) Main method 9

animation Trace Recursive factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) returns factorial(1) Step 7: return 2 return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(2) for factorial(3) Main method 10

animation Trace Recursive factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) returns factorial(2) Step 7: return 2 Step 6: return 1 return 2 * factorial(1) Step 2: executes factorial(2) Step 3: executes factorial(1) Stack return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(3) Main method 11

animation Trace Recursive factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) returns factorial(3) Step 7: return 2 Step 6: return 1 return 2 * factorial(1) Step 2: executes factorial(2) Step 3: executes factorial(1) Stack return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 Main method 12

animation Trace Recursive factorial returns factorial(4) Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) Step 7: return 2 return 3 * factorial(2) return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 6: return 1 Step 3: executes factorial(1) return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 Main method 13

factorial(4) Stack Trace 5 for factorial(0) 4 for factorial(1) for factorial(1) 3 for factorial(2) for factorial(2) for factorial(2) 2 for factorial(3) for factorial(3) for factorial(3) for factorial(3) 1 6 for factorial(1) for factorial(2) 7 for factorial(2) for factorial(3) for factorial(3) 8 for factorial(3) 9 14

Typical call sequence (1) Evaluate arguments left-to-right. If an argument is a simple variable or a literal value, there is no need to evaluate it. When an expression is used, the expression must be evaluated before the call can be made. (2) Push a new stack frame on the call stack. When a method is called, memory is required to store the following information. Parameter and local variable storage. The storage that is needed for each of the parameters and local variables is reserved in the stack frame. Where to continue execution when the called method returns. it's automatically saved.

Typical call sequence (3)Initialize the parameters. When the arguments are evaluated, they are assigned to the local parameters in the called method. (4)Execute the method. After the stack frame for this method has been initialized, execution starts with the first statement and continues as normal. Execution may call on other methods, which will push and pop their own stack frames on the call stack.(recursion)

Typical call sequence (5)Return from the method. When a return statement is encountered, or the end of a void method is reached, the method returns. For non-void methods, the return value is passed back to the calling method. The stack frame storage for the called method is popped off the call stack. Popping something off the stack is really efficient - a pointer is simply moved to previous stack frame. This means that the current stack frame can be reused by other methods. Execution is continued in the called method immediately after where the call took place.

Queue The basic concept of queues is one people normally find easy to understand, because we encounter them on a daily basis. When you go to the bank, unless you're rich enough to avoid having to wait in line, you will wait in a queue for the teller. The person at the front of the queue gets served as soon as a teller becomes free. When a new person enters the bank, they go to the back of the line (queue). This means that queues work on a FIFO (first in first out) basis.

Queue Operations

Queue Implementation Bounded Queue: Array based implementation Fixed-Front Design Approach (Front Always Zero) Floating-Front Design Approach (Circular Arrays) Un-bounded Queue: Linked based implementation

Fixed-Front Design Approach Similar solution like stack: keeping the front of the queue fixed in the first array slot and letting the rear move as we add new elements. To keep the front of the queue fixed at the top of the array, we need to move every element in the queue up one slot

Fixed-Front Design Approach What is the weakness of the design? The need to move all of the elements up every time we remove an element from the queue increases the amount of work needed to dequeue items. The technical terminology is that enqueue() is O(1) and dequeue() is O(N).

Floating-Front Design Approach If we keep track of the index of the front as well as the rear, we can let both ends of the queue float in the array.

Instead of moving elements up to the beginning of the array, it merely increments the front indicator to the next slot.

Letting the queue elements float in the array creates a new problem when the rear indicator reaches the end of the array. In our first design, this situation told us that the queue was full. Now, however, the rear of the queue might potentially reach the end of the (physical) array when the (logical) queue is not yet full.

Because space may still be available at the beginning of the array, the obvious solution is to let the queue elements "wrap around" the end of the array. In other words, we can treat the array as a circular structure. If not full, enqueue at: rear = (rear + 1) % maxque;

An Interesting Question How to implement a queue by using two stacks? Stack: FILO Queue:FIFO

An Interesting Question We'll implement a FIFO queue using two stacks. Lets call the stacks InBox and OutBox. An element is inserted in the queue by pushing it into the InBox. An element is extracted from the queue by popping it from the OutBox. If the OutBox is empty then all elements currently in InBox are transferred to OutBox but in the reverse order.

Code public class Queue<E> { private Stack<E> inbox = new Stack<E>(); private Stack<E> outbox = new Stack<E>(); } public void queue(e item) { inbox.push(item); } public E dequeue() { if (outbox.isempty()) { while (!inbox.isempty()) { outbox.push(inbox.pop()); } } return outbox.pop(); }

So Build a stack using two queues.