22C:21 Discrete Structures

Slides:



Advertisements
Similar presentations
Stacks.
Advertisements

Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Priority Queues Briana B. Morrison Adapted from Alan Eugenio Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118.
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
Stacks. An alternative storage structure for collections of entities is a stack. A stack is a simplified form of a linked list in which all insertions.
TECH Computer Science Data Abstraction and Basic Data Structures Improving efficiency by building better  Data Structure Object IN  Abstract Data Type.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Object-Oriented Programming Simple Stack Implementation.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
Programming Languages and Paradigms Activation Records in Java.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
STACK Data Structure
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
An Array-Based Implementation of the ADT List
Chapter 4 Stacks
Java Memory Management
Stacks.
Java Memory Management
CS 211 Object Oriented Programming
Intro To Classes Review
Stacks.
Memory leaks in Java? Not exactly memory leak
Stack and Queue APURBO DATTA.
Bohyung Han CSE, POSTECH
COMPUTER 2430 Object Oriented Programming and Data Structures I
ADT Heap data structure
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
CMSC 341 Lecture 5 Stacks, Queues
Stacks.
Stacks, Queues, and Deques
CSC 205 – Java Programming II
Stacks, Queues, and Deques
Recursion Data Structures.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Abstract Data Types and Stacks
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
UNIT-II.
Stacks CS-240 Dick Steflik.
Lists.
EE 312 Final Exam Review.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Stacks.
Corresponds with Chapter 5
Abstract Data Types and Stacks
Presentation transcript:

22C:21 Discrete Structures Problem 7.2. System Stacks on Recursion

Stacks – just to refresh your memory List FIFO – push/pop There is a built-in Stack ADT in java. However, I used my own implementation… … because I can … yes, it’s THAT easy … and a little personal touch is always better

The myStack class // Instance variables private static int[] stack; private static int top=0; private static int MAX=100; // Default constructor public myStack() { stack=new int[MAX]; top=-1; }

Push(element) public void push(int b) { if(top>=MAX) System.out.println("Overflow!"); return; } stack[++top]=b; this.print();

Pop() public int pop() { if(top<0) System.out.println("Underflow!"); return(-1); } int temp=top--; this.print(); return stack[temp];

Fibonacci Numbers F0 = 1; F1 = 1; Fn = Fn-1 + Fn-2 for n ≥ 2 So… F2 = 2, F3 = 3, F4 = 5, F5 = 8 …and so on

Modified Fibonacci Code public static BigInteger fibonacci(int n) { System.out.println("fibonacci("+n+") has been called..."); SysStack.push(n); if((n == 0) || (n == 1)) System.out.println("fibonacci("+n+") returning value..."); int garbage = SysStack.pop(); return new BigInteger ("1"); } else BigInteger number1 = fibonacci(n-1); BigInteger number2 = fibonacci(n-2); int moregrabage=SysStack.pop(); return number1.add(number2);

Output for n=3 fibonacci(3) has been called... Current Stack: -- 3 2

Output for n=3 contd. fibonacci(1) has been called... Current Stack: -- 1 2 3 fibonacci(1) returning value...

Output for n=3 contd. fibonacci(0) has been called... Current Stack: -- 2 3 fibonacci(0) returning value...

Output for n=3 contd. fibonacci(2) returning value... Current Stack: -- 3 fibonacci(1) has been called... 1

… should be empty at the end Output for n=3 contd. fibonacci(1) returning value... Current Stack: -- 3 fibonacci(3) returning value... … should be empty at the end

What happens for n=5? 5 Fibonacci(5) 4 3 5 System Stack Empty

What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 4 5 System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3) 1 3 4 5 System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3) 1 2 Fibonacci(2) 3 1 4 5 System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3) 1 2 1 2 Fibonacci(2) 3 4 Fibonacci(1) 1 5 System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3) 1 2 Fibonacci(2) 3 4 5 System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3) 2 1 2 Fibonacci(2) 3 4 Fibonacci(0) 5 System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3) 1 2 Fibonacci(2) 3 4 5 System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3) 1 3 4 5 System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3) 1 1 Fibonacci(1) 3 4 5 System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 3 2 Fibonacci(3) System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(4) 4 3 2 4 5 System Stack

What happens for n=5? 5 Fibonacci(5) 4 3 Fibonacci(4) 2 Fibonacci(2) 1 2 4 5 System Stack

What happens for n=5? 5 Fibonacci(5) 4 3 Fibonacci(4) 2 Fibonacci(2) 1 Fibonacci(1) 2 4 5 System Stack

What happens for n=5? 5 Fibonacci(5) 4 3 Fibonacci(4) 2 Fibonacci(2) 2 2 4 5 System Stack

What happens for n=5? 5 Fibonacci(5) 4 3 Fibonacci(4) 2 Fibonacci(2) Fibonacci(0) 2 4 5 System Stack

What happens for n=5? 5 Fibonacci(5) 4 3 Fibonacci(4) 2 Fibonacci(2) 2 System Stack

What happens for n=5? 5 Fibonacci(5) 4 3 Fibonacci(4) 4 5 System Stack

What happens for n=5? 5 Fibonacci(5) 3 5 System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(3) 3 2 1 3 5 System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(3) 3 2 1 Fibonacci(2) 1 2 3 5 System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(3) 3 2 1 Fibonacci(2) 1 2 3 5 System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(3) 3 2 1 Fibonacci(2) 2 2 3 5 System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(3) 3 2 1 Fibonacci(2) Fibonacci(0) 2 3 5 System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(3) 3 2 1 Fibonacci(2) 2 System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(3) 3 1 3 5 System Stack

What happens for n=5? Fibonacci(5) 5 Fibonacci(3) 3 1 Fibonacci(1) 1 3 System Stack

What happens for n=5? 5 Fibonacci(5) Fibonacci(3) 3 3 5 System Stack

What happens for n=5? 5 Fibonacci(5) 5 System Stack

What happens for n=5? System Stack Empty

Note Actual system stack holds returned values Just the indices has been shown Compare with FastFibonacci()

Now you know!

Quiz 7 You are given a collection with 6 items. The priorities of these items are shown below: 9 30 25 22 7 50 1. Draw all possible max-heaps (as binary trees) for this collection of items. 2. Start with the max-heap (30, 50, 9, 25, 7, 22). Perform the following sequence of operations on this max-heap: Insert(45) Insert(29) Delete() Show the max-heap after each operation. 3. Answer in a sentence or two: Given a max-heap can you find the item with 2nd highest priority in O(1) time? Explain your answer. 4. Answer in a sentence or two: Given a max-heap can you find the item with smallest priority in O(1) time? Explain your answer.