Barb Ericson Georgia Tech

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
What gets printed out? def f(n): if (n == 0): return (" ") else: print(“blug ") return f(n-1) f(3)
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
Chapter 11 UNIX Printing. have to be root to setup a printer local printer is directly connected remote printer is a network printer print queue is nothing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 5 Accessing Files and Directories. How Directories Get Created OS installation: usr, dev, etc, export, kernel and others places to store installation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab.
Recursion. Sum a list of numbers Iterative def sum(L): total = 0 for i in L: total += i return total Recursive def sum(L): if len(L) == 0: return 0 else:
Introduction to Data Structures. Data Structures A data structure is a scheme for organizing data in the memory of a computer. Some of the more commonly.
Trees CS /02/05 L7: Trees Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Introduction to Data Structures. About the document. The document is prepared by Prof. Shannon Bradshaw at Drew University. The key concepts in data structure.
DataStructures1 Barb Ericson Georgia Tech July 2008.
TECH Computer Science Data Abstraction and Basic Data Structures Improving efficiency by building better  Data Structure Object IN  Abstract Data Type.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Stacks And Queues Chapter 18.
Arrays and Collections Tonga Institute of Higher Education.
Data Structures Types of Data Structure Data Structure Operations Examples Choosing Data Structures Data Structures in Alice.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Chapter 15: Topics in Computer Science: Functional Programming.
Topic 2 Collections. 2-2 Objectives Define the concepts and terminology related to collections Discuss the abstract design of collections.
Stacks. What is a Stack? A stack is a type of data structure (a way of organizing and sorting data so that it can be used efficiently). To be specific,
Level Order Traversal of a Binary Tree Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals.
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
INTRODUCTION TO DATA STRUCTURES 1. DATA STRUCTURES A data structure is a scheme for organizing data in the memory of a computer. Some of the more commonly.
An Introduction to Programming Using Alice Data Structures.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Chapter 12 – Data Structures
5.13 Recursion Recursive functions Functions that call themselves
Lecture 7 Queues Stacks Trees.
COSC160: Data Structures: Lists and Queues
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Fundamentals of structural programming
Introduction to Data Structures
i206: Lecture 13: Recursion, continued Trees
Pointers and Linked Lists
i206: Lecture 10: Lists, Stacks, Queues
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
Recursion Output Input
Another problem to solve…
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
Introduction to Data Structures
Calculate n! Multiply my number by the factorial of the rest of the numbers. if( num > 2 ) return num * factorialR2( num - 1 ); else return 2;
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Another problem to solve…
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Recursion Taken from notes by Dr. Neil Moore
Abstract Data Type (ADT)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 19: Recursion.
[Most of the details about queues are left for to read about and work out in Lab 6.] Def. As a data structure, a queue is an ordered collection of data.
Another problem to solve…
From Barb Ericson Georgia Institute of Technology June 2008
Presentation transcript:

Barb Ericson ericson@cc.gatech.edu Georgia Tech Recursion Barb Ericson ericson@cc.gatech.edu Georgia Tech

Recursion When a function calls itself def downUp(word ): print word Used to break problems into smaller problems Used to solve recursion problems def downUp(word ): print word if len(word )==1: return downUp(word [1:]) >>> downUp("Hello")

Print filenames in a directory tree import os import java.io.File as File def printAllFiles(directory ): files = os.listdir(directory) for file in files: fullname = directory+"/"+file if isDirectory(fullname ): printAllFiles(fullname) else: print fullname def isDirectory(filename ): filestatus = File(filename) return filestatus.isDirectory ()

Decrease Red Recursively def decreaseRedR(aList ): if aList == []: # empty return setRed(aList [0], getRed(aList [0])*0.8) decreaseRedR(aList [1:])

What is the output? def test(num): Call Stack if num > 0: test(0) return 0 test(1) return test(0) + 1 test(2) return test(1) + 2 test(3) return test(2) + 3 def test(num): if num > 0: return test(num -1) + num else: return 0 test(0) returns 0, test(1) returns 1, test(2) returns 3, test(3) returns 6

Stacks Add and remove from the top of the stack Last in first out type of structure Like a stack of plates in a cafeteria Or a Pez container Or a childs toy

Queue Add to the end and remove from the front First in and first out type of structure Like a line at a store checkout Or a printer queue