CS 2412 Data Structures. Chapter 2 Stacks and recursion



Similar documents
Stacks. Linear data structures

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Chapter 5. Recursion. Data Structures and Algorithms in Java

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

DATA STRUCTURE - STACK

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Data Structure with C

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

Module 2 Stacks and Queues: Abstract Data Types

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Lecture Notes on Stacks & Queues

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

What Is Recursion? Recursion. Binary search example postponed to end of lecture

10CS35: Data Structures Using C

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Solving Problems Recursively

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

St S a t ck a ck nd Qu Q eue 1

Data Structures and Algorithms C++ Implementation

Data Structures. Level 6 C Module Descriptor

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Ordered Lists and Binary Trees

Common Data Structures

Linked Lists Linked Lists, Queues, and Stacks

Data Structures and Algorithms

Lecture 12 Doubly Linked Lists (with Recursion)

PES Institute of Technology-BSC QUESTION BANK

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

CSE 211: Data Structures Lecture Notes VII

DATA STRUCTURES USING C

Data Structures and Data Manipulation

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

What Is Recursion? 5/12/10 1. CMPSC 24: Lecture 13 Recursion. Lecture Plan. Divyakant Agrawal Department of Computer Science UC Santa Barbara

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

TREE BASIC TERMINOLOGIES

Lecture Notes on Binary Search Trees

CS 2412 Data Structures. Chapter 3 Queues

Recursion and Recursive Backtracking

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

List, Stack and Queue. Tom Chao Zhou CSC2100B Data Structures Tutorial 3

Introduction to Stacks

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

Pseudo code Tutorial and Exercises Teacher s Version

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Stack Allocation. Run-Time Data Structures. Static Structures

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Binary Search Trees (BST)

csci 210: Data Structures Recursion

Basic Data Structures and Algorithms

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Patterns in Pascal s Triangle

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Data Structures and Algorithms Written Examination

Chapter Objectives. Chapter 7. Stacks. Various Types of Stacks LIFO. Empty Stack. Stacks

Section IV.1: Recursive Algorithms and Recursion Trees

Lecture Notes on Linear Search

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

COMPUTER SCIENCE. Paper 1 (THEORY)

Binary Search Trees CMPSC 122

Introduction to Data Structures

Recursion vs. Iteration Eliminating Recursion

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Why Use Binary Trees?

Lecture Notes on Binary Search Trees

7.1 Our Current Model

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Algorithms and Data Structures

CompSci-61B, Data Structures Final Exam

Parallelization: Binary Tree Traversal

Data Structure [Question Bank]

Binary Heap Algorithms

Inside the Java Virtual Machine

Converting a Number from Decimal to Binary

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

C++ INTERVIEW QUESTIONS

Chapter 3: Restricted Structures Page 1

CmpSci 187: Programming with Data Structures Spring 2015

Data Structures Fibonacci Heaps, Amortized Analysis

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Analysis of a Search Algorithm

Lecture 11 Array of Linked Lists

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Heaps & Priority Queues in the C++ STL 2-3 Trees

Transcription:

CS 2412 Data Structures Chapter 2 Stacks and recursion 1

2.1 Stacks A stack is a data structure in which all insertions and deletions of entries are made at one end, called top of the stack. Examples: A stack of files. DOS command line LIFO: last in first out (or first in last out FILO). Data Structure 2014 R. Wei 2

Stacks are very important data structure. In any kind of computer, there is a stack area in memory (usually in higher memory addresses). When a subprogram is called, a return address is put in the stack. In this way, the computer can return to the place after the subprogram is finished. Data Structure 2014 R. Wei 3

Main operations of stack Creation (create a stack). Clearance (damage a stack). Push (add an element to the stack) Pop (get an element from the stack) Data Structure 2014 R. Wei 4

Specification for a stack To specify a stack, we consider the required functions and the preconditions and post-conditions for each function. We will use type Stack and StackEntry to denote the type stack and the entries of the stack. In general, for a stack, all the entries should be the same type data. Data Structure 2014 R. Wei 5

Initialization: Void CreateStack(Stack *s); precondition: None. postcondition: The stack s has been created and is initialized to be empty. Data Structure 2014 R. Wei 6

Status: Boolean StackEmpty(Stack *s); precondition: The stack exists and it has been initialized. postcondition: Return TRUE if the stack is empty, FALSE otherwise. Boolean StackFull(Stack *s); precondition: The stack exists and it has been initialized. postcondition: Return TRUE if the stack is full, FALSE otherwise. Data Structure 2014 R. Wei 7

Basic operations; void Push(StackEntry item, Stack *s); precondition: The stack exists and it is not full. postcondition: The argument item the stack. has been stored at the top of void Pop(StackEntry *item, Stack *s); precondition: The stack exists and it is not empty. postcondition: The top of the stack has been removed and returned in *itme. Data Structure 2014 R. Wei 8

Other operations: int StackSize(Stack *s); precondition: The stack exists and it has been initialized. postcondition: The function returns the number of entries in the stack. int StackTop(StackEntry *item, Stack *s); precondition: The stack exists and it is not empty. postcondition: The item at the top of the stack is returned without being removed. Data Structure 2014 R. Wei 9

Termination void ClearStack(Stack *s); precondition: The stack exists and it has been initialized. postcondition: All entries in the stack have been deleted; the stack is empty. Note: The postcondition can be defined to: deleted all the entries and the stack itself. Data Structure 2014 R. Wei 10

Traversable stack: This is not a ordinary stack and the following operation is not an operation of a stack. void TraverseStack(Stack *s, void(*visit)()); precondition: The stack exists and it has been initialized. postcondition: The function that Visit points to, has been invoked for each entry in the stack, beginning with the entry at the top and proceeding toward the bottom of stack. Data Structure 2014 R. Wei 11

Implementation of stacks Declarations #define MAXSTACK 10 //size of the stack typedef char StackEntry; //stack of char typedef struct { int top; StackEntry entry[maxstack]; Stack; Data Structure 2014 R. Wei 12

void Push(StackEntry item,stack *s) { if(stackfull(s)) Error("Stack is full"); else s->entry[s->top++]=item; void Pop(StackEntry *item, Stack *s) { if(stackempty(s)) Error("Stack is empty"); else *item=s->entry[--s->top]; Data Structure 2014 R. Wei 13

Boolean StackEmpty(Stack *s) { return s->top <=0; Boolean StackFull(Stack *s) { return s->top >=MAXSTACK; void CreateStack(Stack *s) { s->top=0; Data Structure 2014 R. Wei 14

Dynamically allocating memories: void *calloc(n,size) void free(p) void *malloc(size) void *realloc(p,size) Example: tp = (double*) malloc(n*sizeof(double)); if (tp==null) printf("could not allocate %d doubles\n",n); Data Structure 2014 R. Wei 15

Don t assume malloc will always succed. Don t assume the storage malloc provides is initialized to zero. Don t modify the pointer returned by malloc. free only pointers obtained from malloc, and don t access the storage after it s been freed. Data Structure 2014 R. Wei 16

Linked stack Dynamic memory allocation can allocate memories during the run time and free the memories after use. typedef struct node { void* dataptr; struct node* link; STACK_NODE; typedef struct { int count; STACK_NODE* top; STACK; Data Structure 2014 R. Wei 17

STACK* createstack (void) { STACK* stack; stack = (STACK*)malloc(sizeof (STACK)); if (stack) { stack->count = 0; stack->top = NULL; return stack; Data Structure 2014 R. Wei 18

bool pushstack (STACK* stack, void* datainptr) { STACK_NODE* newptr; newptr = (STACK_NODE*)malloc(sizeof(STACK_NODE)); if (!newptr) return false; newptr->dataptr = datainptr; newptr->link = stack->top; stack->top = newptr; (stack->count)++; return true; Data Structure 2014 R. Wei 19

void* popstack (STACK* stack) { void* dataoutptr; STACK_NODE* temp; if(stack->count == 0) dataoutptr = NULL; else { temp = stack->top; dataoutptr = stack->top->dataptr; stack->top = stack->top->link; free (temp); (stack->count)--; return dataoutptr; Data Structure 2014 R. Wei 20

void* stacktop (STACK* stack) { if (stack->count == 0) return NULL; else return stack->top->dataptr; bool emptystack (STACK* stack) { return (stack->count == 0); Data Structure 2014 R. Wei 21

bool fullstack(stack* stack) { STACK_NODE* temp; if((temp=(stack_node*)malloc(sizeof (*(stack->top))))) { free (temp); return false; return true; int stackcount (STACK* stack) { return stack->count; Data Structure 2014 R. Wei 22

STACK* destroystack(stack* stack) { STACK_NODE* temp; if (stack) { while(stack->top!=null) { free(stack->top->dataptr); temp = stack->top; stack->top = stack->top->link; free (temp); free (stack) return NULL; Data Structure 2014 R. Wei 23

2.2 Introduction to recursion Stack frames for subprograms Suppose we has 4 functions A, B, C and D. The main function M first calls A and A needs to call B. Then A calls C and C calls D. Finally, M calls D, D calls D. Computer uses stack frames in memory to remember the return places. Data Structure 2014 R. Wei 24

Recursion Recursion is the name for the case when a subprogram invokes itself or invokes a series of other subprograms that eventually invokes the first subprogram again. Data Structure 2014 R. Wei 25

Stack and tree of subprogram calling Theorem During the traversal of any tree, vertices are added to or deleted from the path back to the root in the fashion of a stack. Given any stack, conversely, a tree can be drawn to portray the life history of the stack, as items are pushed onto or popped from it. Data Structure 2014 R. Wei 26

Example: To implement factorial function, there are two different ways to do it. int Factorial(int n) { int i,k=1; if(n==0) return 1; else for(i=1;i<=n;i++) k=k*i; Data Structure 2014 R. Wei 27

Using recursion: int Factorial(int n) { if(n==0) return 1; else return n*factorial(n-1); The source code looks more simple if a recursion is used. But recursion uses more memory. Data Structure 2014 R. Wei 28

The towers of Hanoi There are 3 diamond needles. On the first needle are stacked 64 golden disks, each one slightly smaller than the one under it. The task is to move all the golden disks from the first needle to the third, subject to the conditions that only one disk can be moved at a time, and that no disk is ever allowed to be placed on top of a smaller disk. We describe the problem as Move(64;1,3,2) It means move 64 disk from 1 to 3 using 2 as auxiliary needle. Data Structure 2014 R. Wei 29

Use recursion solution Question: In what condition we can move the last disk to the third needle? Answer: It must be the case that the third needle is empty, the first needle only has the last disk and all others are placed on the second needle. That means we must have done Move(63;1,2,3) Data Structure 2014 R. Wei 30

After that we just need to do the following things: Move the last disks to the third needle. Move other disks from second needle to the third needle, i.e., Move(63;2,3,1) Data Structure 2014 R. Wei 31

#define DISK 64 int main(void) { Move(DISK,1,3,2); return 0; void Move(int count, int start, int finish, int temp) { if(count > 0) Move(count-1,start,temp,finish); printf("move a disk from %d to %d.\n",start,finish); Move(count-1,temp,finish,start); Data Structure 2014 R. Wei 32

To study a recursion function, try a very small example to trace its action is usually useful. We can use tree to see the cases of DISK equals to 2 and 3. The tree for 3 disk is as follows. Data Structure 2014 R. Wei 33

When DISK == 64, the number of non-leaves is 1 + 2 + 4 + + 2 63 = 2 0 + 2 1 + 2 2 + + 2 63 = 2 64 1 That means there are 2 64 1 moves to finish the towers of Hanoi problem. It is easy to estimate that 2 64 > 1.6 10 19. If we can move one disk at one second, then it will take about 5 10 11 years. I.e., it will take about 50 billion years! On the other hand, the space for keeping track of 64 recursive calls is not big. Data Structure 2014 R. Wei 34

2.3 Backtracking A puzzle: How to place eight queens on a chessboard so that no one can take another. Q Q Q Q Q Q Q Q Data Structure 2014 R. Wei 35

Q Q Q Q Q Q Q Q How to find one solutions? How to find all the solutions? Data Structure 2014 R. Wei 36

Example of four queens: Q x x x x x x x x x x Q x x x x x Q Q x x x Q x Data Structure 2014 R. Wei 37

Backtracking algorithms Used in a search problem by constructing partial solutions and then trying to extend the partial solutions. The main steps: Establish a partial solution of the search problem. Make sure that the partial solutions remain consistent with the requirement of the problem. Extend the partial solutions toward the completion. If a inconsistency occurs, then the algorithm backs up by removing the most recently constructed part of the solution and try another possibility. Data Structure 2014 R. Wei 38

Algorithm for eight-queen puzzle Try to use recursion. Define a function AddQueen which add a queen in an unguarded position on the board. We can recursively call this function until a solution is found, or backtrack a position. We use queencount to denote the number of queens have put on the board. The pseudo code of the function can be: Data Structure 2014 R. Wei 39

void AddQueen(void) { for(each unguarded position p on the board) { place a queen in position p; queencount++ and mark guarded positions; if (queencount==8) print the configuration; else AddQueen(); remove the queen from position p; queencount-- and free guarded positions; Data Structure 2014 R. Wei 40

To implement the algorithm, we need more specification. int col[8] is used to record the position of queens. col[i] = the column the ith queen is in. Boolean colfree[8] is used to record if the column is unguarded. For diagonals, it is more difficult to record and check. We can use a 8 8 array to record the guarded positions, but we have a better method. It is noted that for the position (i, j) all the positions in the downward diagonal the difference of row and column indices is the same (i j). Similarly the sum of the row and column indices is the same for all the positions in the upward diagonal. So Boolean downfree[15] is used to record the unguarded positions on the downward diagonals and Boolean upfree[15]) is used to record the unguarded positions on the upward diagonals. Data Structure 2014 R. Wei 41

2.4 Principles of recursion Design a recursion algorithm: Find the key step: Find a step which will be followed with same or similar steps towards the solution. Find a stopping rule: Each recursion algorithm has to have a stopping rule. Outline the algorithm: Abstraction, specification, details. Check termination: To avoid infinite loops. Draw a recursion tree: To analysis the algorithm. Data Structure 2014 R. Wei 42

From a recursion tree, we can estimate the requirements for space and time for running the program. The depth of the tree indicated the space requirement. The number of nodes related to the time requirement. Data Structure 2014 R. Wei 43

Tail recursion If the last-executed statement of a function is recursive call to the function itself, then this call can be eliminated by reassigning parameters to the values specific in the recursive call, and then repeating the whole function. Data Structure 2014 R. Wei 44

The algorithm of Towers of Hanoi: void Move(int count, int start, int finish, int temp) { int swap; while(count > 0) { Move(count-1,start,temp,finish); printf("move disk %d from %d to %d.\n",count,start, finish); count--; swap=start; start=temp; temp=swap; Data Structure 2014 R. Wei 45

Recursion and iteration Fibonacci number: F 0 = 0, F 1 = 1, F n = F n 1 + F n 2, for n 2. int Fibonacci(int n) { if (n<=0) return 0; else if (n==1) return 1; else return Fibonacci(n-1) + Fibonacci(n-2); Data Structure 2014 R. Wei 46

int Fibonacci(int n) { int i; int twoback,oneback,current; if (n<=0) return 0; else if (n==1) return 1; else { twoback = 0; oneback = 1; for(i=2;i<=n;i++){ current=twoback+oneback; twoback=oneback; oneback=current; return current; Data Structure 2014 R. Wei 47

Make decision of using recursion by recursion tree. If the recursion tree has a simple form, the iterative version may be better. If the recursion tree involves duplicate tasks, then data structures other than stacks will be appropriate, and the need for recursion may disappear. If the recursion tree appears quite bushy, with little duplication of tasks, then recursion is likely the nature method. Data Structure 2014 R. Wei 48