Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Computer Science II Recursion Professor: Evan Korth New York University.
More on Recursive Methods KFUPM- ICS Data Structures.
Recursion. Binary search example postponed to end of lecture.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Programming with Recursion
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Recursion Gordon College CPS212
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
Recursion and Implementation of Functions
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
M180: Data Structures & Algorithms in Java
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
ICS220 – Data Structures and Algorithms Dr. Ken Cosh Week 5.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Data Structures & Algorithms
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
1 Recursion. 2 A process by which a function calls itself repeatedly  Either directly. X calls X  Or cyclically in a chain. X calls Y, and Y calls X.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
CS314 – Section 5 Recitation 9
Recursion.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
RECURSION.
Stacks Chapter 4.
Java Software Structures: John Lewis & Joseph Chase
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Stacks & Recursion.
Module 1-10: Recursion.
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Stacks & Recursion

Stack pushpop LIFO list - only top element is visible top

Defining the ADT "Stack" Data: – a linear collection of data items in which all operations occur at one end, called the top Basic Operations: – construct a stack (usually starts empty) – find out if stack is empty – push: add an item on top of the stack – accessing the topmost item Top: retrieve the top item of the stack Pop: remove the top item of the stack

Some Implementation choices fixed-size array – capacity (max # elements) decided at compile-time could be too small for expected amount of data could be too large, so space is wasted – size (current # elements used) – fast dynamic array – capacity decided at run-time – size may be less than or equal to the capacity – uses pointers linked list – size changes as needed during program execution – uses pointers

Implementation example myTop myArray typedef Complx StackElement; const int CAPACITY = 8; int myTop; StackElement myStack[CAPACITY]; Complx X; push(&myStack,X); initially empty (myTop is negative)

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Using the stack - 1 Model with an array – Let position 0 be top of stack Problem … consider pushing and popping – Requires much shifting

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Using the stack - 2 A better approach is to let position 0 be the bottom of the stack Thus our design will include – An array to hold the stack elements – An integer to indicate the top of the stack

Addressing the problem Too much or too little space Whatever size you pick, some day, there will be too much data In embedded systems, RAM is a premium Need a dynamic solution (TBD) – Size changes as needed – No waste – Slower when adding new elements

The "node" concept a "node" is one element of a stack more than just the data – index of next and/or previous node – trivial for array implementations each node is a struct – data may be a struct INSIDE the node struct struct node {int next; Complx data;}; node myStackNode[CAPACITY];

Recursion An alternative to loops

What is recursion? a function calls itself – direct recursion a function calls its caller – indirect recursion f f1 f2

Recursion-1 Alternative to iteration (looping) – often more "elegant" and concise than a loop – sometimes very inefficient – easier to program than loops Useful when – a problem can be defined in terms of similar sub-problems – eventually reach a "known" answer (base case) Inefficient if duplicate values exist

Head vs. Tail Recursion head recursion – requires "deepest" call to complete before any values are known – current stack state must be preserved tail recursion – compiler can "collapse" the stack

Head vs. Tail recursion Note: base case is ALWAYS 1st tail(3) is: void tail(int n) { if(n == 0) return; else printf("tail - n=%i\n",n); tail(n-1); //  } head(3) is: 2 3 void head(int n) { if(n == 1) return; else head(n-1); //  printf("head - n=%i\n",n);); }

Caveats Static data is NOT on the "stack" – never gets re-allocated – same values for EVERY call Must have an "exit" – prevent an infinite loop

Outline of a Recursive Function if (answer is known) provide the answer & exit else call same function with a smaller version of the same problem base case recursive case

Factorial (n) - looping fact (int n) {if (n<0) exit(1); answer=1; for (i=1; i<=n; i++) answer=answer*i; // loop n times return (answer); } This is a simple problem

Factorial (n) – head recursive definition: Factorial (n) = n * Factorial (n-1) int Fact (int n) {if (n<0) exit(1); if (n == 0 | n==1) return 1; return n * Fact (n-1); // stack must be saved // cannot do the *'s until last value }

Factorial "n" (tail recursive) tail_fact (n, sofar) // set "sofar" same as "n" { if (n == 0 | n==1) return sofar; else // nothing to save on the stack // because it is in the 2 nd parameter return tail_fact (--n, sofar * n); } // here's how to run it printf ("5!=%i",tail_fact(5,5));

Keeping track compiler builds code to: – create a new stack pointer and stack space – put local variables & parameters on stack each "return" returns control to caller's next instruction (the inst after the call) returned value, is in caller's stack space – same as ANY function this is called "unwinding"

Recursive Call Tree int Fact (int n) { if (n == 0 | n==1) return 1; return n * Fact (n-1); } Fact (4) 3*Fact (2) 4*Fact (3) 2*Fact (1)

Fibonacci Series Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, …. n= for n <= 2 fib(n) = fib(n-2) + fib(n-1) for n>2 for n=4: fib(2)+fib(3)  3

Tracing fib(6)

Ackermann's function A(0, n) = n + 1 A(m, 1) = A(m+1, 0) A(m+1, n+1) = A(m, A(m+1, n))

What Does a Compiler Do? Lexical analysis – divide a stream of characters into a stream of tokens total = cost * cost; if ( ( cond1 && ! cond2 ) ) Parsing – do the tokens form a valid program, – i.e. does it follow the syntax rules? Generate object code

BNF (Backus-Naur form) (also called Backus Normal Form) a language used to define the syntax rules of a programming language consists of – productions – rules for forming some construct of the language – meta-symbols – symbols of BNF that are NOT part of the language being compiled – terminals – appear as shown – non-terminals – syntax defined by another production

BNF Syntax Rules for a simplified boolean expression – bexpr -> bterm || bexpr | bterm – bterm -> bfactor && bterm | bfactor – bfactor -> !bfactor | (bexpr) | true | false | ident – ident -> alpha { alpha | digit|_} – alpha -> a.. z | A.. Z – digit -> meta-symbols are in red terminals are in blue non-terminals are in black special rules needed for "|" as part of a language

bexpr -> bterm || bterm | bterm What sequence of tokens is a valid bexpr? if (there is a valid bterm) if (nextToken is ||) if (there is a valid bterm) return true else return false else return true else return false Note: tokenizer must watch for the "||" without stopping at the first "|"