More Data Structures (Part 1)

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Stacks A stack is a data structure that only allows items to be inserted and removed at one end We call this end the top of the stack The other end is.
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.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Building Java Programs
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 3 Introduction to Collections – Stacks Modified
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Week 3 – Wednesday.  What did we talk about last time?  ADTs  List implementation with a dynamic array.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
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.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Inheritance Notes Chapter 6 1. Inheritance  you know a lot about an object by knowing its class  for example what is a Komondor? 2
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Click to edit Master text styles Stacks Data Structure.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
An Array-Based Implementation of the ADT List
Building Java Programs
Stacks II David Lillis School of Computer Science and Informatics
Review Array Array Elements Accessing array elements
Stacks.
Stacks and Queues.
CSCI 3333 Data Structures Stacks.
Exam information in lab Tue, 18 Apr 2017, 9:00-noon programming part
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
MEMORY REPRESENTATION OF STACKS
Week 3 - Friday CS221.
Stacks.
Cinda Heeren / Geoffrey Tien
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Building Java Programs Chapter 14
Building Java Programs
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues.
Building Java Programs
CMSC 341 Lecture 5 Stacks, Queues
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks and Queues.
Stacks, Queues, and Deques
Lesson Objectives Aims
Stacks.
Building Java Programs
Building Java Programs
Stacks and Queues CLRS, Section 10.1.
CSC 143 Stacks [Chapter 6].
Stacks Abstract Data Types (ADTs) Stacks
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Abstract Data Type Abstract Data Type as a design tool
slides created by Marty Stepp
Stacks.
More Data Structures (Part 1)
Stacks CS-240 Dick Steflik.
Recursive Objects Singly Linked Lists.
Generics, Stack, Queue Based on slides by Alyssa Harding
Stacks, Queues, and Deques
CS210- Lecture 3 Jun 6, 2005 Announcements
Dynamic Array: Implementation of Stack
Stacks and Queues.
CHAPTER 3: Collections—Stacks
Presentation transcript:

More Data Structures (Part 1) Stacks

Stack examples of stacks

Top of Stack top of the stack

Stack Operations classically, stacks only support two operations push add to the top of the stack pop remove from the top of the stack throws an exception if there is nothing on the stack

Stack Optional Operations size number of elements in the stack isEmpty is the stack empty? peek get the top element (without removing it) search find the position of the element in the stack isFull is the stack full? (for stacks with finite capacity) capacity total number of elements the stack can hold (for stacks with finite capacity)

Push st.push("A") st.push("B") st.push("C") st.push("D") st.push("E") top "E" top "D" top "C" top "B" top "A"

Pop String s = st.pop() s = st.pop() top "E" top "D" top "C" top "B" "A"

LIFO stack is a Last-In-First-Out (LIFO) data structure the last element pushed onto the stack is the first element that can be accessed from the stack

Applications stacks are used widely in computer science and computer engineering undo/redo widely used in parsing a call stack is used to store information about the active methods in a Java program convert a recursive method into a non-recursive one

Example: Reversing a sequence a silly and usually inefficient way to reverse a sequence is to use a stack

Don't do this public static <E> List<E> reverse(List<E> t) { List<E> result = new ArrayList<E>(); Stack<E> st = new Stack<E>(); for (E e : t) { st.push(e); } while (!st.isEmpty()) { result.add(st.pop()); return result;

Converting a Recursive Method a stack can be used to convert a recursive method to a non-recursive method the key to understanding how to do this lies in the memory diagram for a recursive memory the following slides are from the Day 25 lecture

public static double powerOf10(int n) { double result; if (n < 0) { result = 1.0 / powerOf10(-n); } else if (n == 0) { result = 1.0; else { result = 10 * powerOf10(n - 1); return result;

double x = Recursion.powerOf10(3); 100 main method x powerOf10(3)

600 powerOf10 method 3 100 main method powerOf10(3) double x = Recursion.powerOf10(3); 600 powerOf10 method n 3 result 100 main method x powerOf10(3)

600 powerOf10 method 3 10 * powerOf10(2) 100 main method powerOf10(3) double x = Recursion.powerOf10(3); 600 powerOf10 method n 3 result 10 * powerOf10(2) 100 main method x powerOf10(3)

600 powerOf10 method 3 10 * powerOf10(2) 100 main method powerOf10(3) double x = Recursion.powerOf10(3); 600 powerOf10 method n 3 result 10 * powerOf10(2) 100 main method x powerOf10(3) 750 powerOf10 method n 2 result

600 3 10 * powerOf10(2) 100 powerOf10(3) 750 2 10 * powerOf10(1) double x = Recursion.powerOf10(3); 600 powerOf10 method n 3 result 10 * powerOf10(2) 100 main method x powerOf10(3) 750 powerOf10 method n 2 result 10 * powerOf10(1)

600 powerOf10 method 3 10 * powerOf10(2) 100 main method powerOf10(3) double x = Recursion.powerOf10(3); 600 powerOf10 method n 3 result 10 * powerOf10(2) 100 main method x powerOf10(3) 750 powerOf10 method n 2 result 10 * powerOf10(1) 800 powerOf10 method n 1 result 10 * powerOf10(0)

600 powerOf10 method 3 10 * powerOf10(2) 100 main method powerOf10(3) double x = Recursion.powerOf10(3); 600 powerOf10 method n 3 result 10 * powerOf10(2) 100 main method x powerOf10(3) 750 powerOf10 method n 2 result 10 * powerOf10(1) 800 powerOf10 method n 1 result 10 * powerOf10(0) 950 powerOf10 method n result

600 powerOf10 method 3 10 * powerOf10(2) 100 main method powerOf10(3) double x = Recursion.powerOf10(3); 600 powerOf10 method n 3 result 10 * powerOf10(2) 100 main method x powerOf10(3) 750 powerOf10 method n 2 result 10 * powerOf10(1) 800 powerOf10 method n 1 result 10 * powerOf10(0) 950 powerOf10 method n result 1

600 powerOf10 method 3 10 * powerOf10(2) 100 main method powerOf10(3) double x = Recursion.powerOf10(3); 600 powerOf10 method n 3 result 10 * powerOf10(2) 100 main method x powerOf10(3) 750 powerOf10 method n 2 result 10 * powerOf10(1) 800 powerOf10 method n 1 result 10 * 1 950 powerOf10 method n result 1

600 powerOf10 method 3 10 * powerOf10(2) 100 main method powerOf10(3) double x = Recursion.powerOf10(3); 600 powerOf10 method n 3 result 10 * powerOf10(2) 100 main method x powerOf10(3) 750 powerOf10 method n 2 result 10 * powerOf10(1) 800 powerOf10 method n 1 result 10

600 3 10 * powerOf10(2) 100 powerOf10(3) 750 2 10 * 10 800 1 10 double x = Recursion.powerOf10(3); 600 powerOf10 method n 3 result 10 * powerOf10(2) 100 main method x powerOf10(3) 750 powerOf10 method n 2 result 10 * 10 800 powerOf10 method n 1 result 10

600 powerOf10 method 3 10 * powerOf10(2) 100 main method powerOf10(3) double x = Recursion.powerOf10(3); 600 powerOf10 method n 3 result 10 * powerOf10(2) 100 main method x powerOf10(3) 750 powerOf10 method n 2 result 100

600 powerOf10 method 3 10 * 100 100 main method powerOf10(3) 750 double x = Recursion.powerOf10(3); 600 powerOf10 method n 3 result 10 * 100 100 main method x powerOf10(3) 750 powerOf10 method n 2 result 100

600 powerOf10 method 3 1000 100 main method powerOf10(3) double x = Recursion.powerOf10(3); 600 powerOf10 method n 3 result 1000 100 main method x powerOf10(3)

600 powerOf10 method 3 1000 100 main method 1000 double x = Recursion.powerOf10(3); 600 powerOf10 method n 3 result 1000 100 main method x 1000

double x = Recursion.powerOf10(3); 100 main method x 1000

Using a stack we to simulate what the JVM is doing during the recursive method each recursive call that is not a base case pushes a 10 onto the stack a recursive call that reaches a base case pushes a 1 onto the stack pop the stack until it is empty, multiplying the values as you go

public static int powerOf10(int n) { Stack<Integer> t = new Stack<Integer>(); // recursive calls while (n > 0) { t.push(10); n--; } // base case: n == 0 t.push(1); // accumulate the result int result = t.pop(); while (!t.isEmpty()) { result *= t.pop(); return result;

Converting a Recursive Method using a stack in the previous example is too complicated for the problem that we were trying to solve but it illustrates the basic idea of using a stack to convert a recursive method to an iterative method more complicated examples require greater sophistication in exactly what is pushed onto the stack see the make-up lab (Part 1) when it becomes available

Implementation with ArrayList ArrayList can be used to efficiently implement a stack the end of the list becomes the top of the stack adding and removing to the end of an ArrayList usually can be performed in O(1) time

public class Stack<E> { private ArrayList<E> stack; public Stack() { this.stack = new ArrayList<E>(); } public void push(E element) { this.stack.add(element); public E pop() { return this.stack.remove(this.stack.size() - 1);

Implementation with Array the ArrayList version of stack hints at how to implement a stack using a plain array however, an array always holds a fixed number of elements you cannot add to the end of the array without creating a new array you cannot remove elements from the array without creating a new array instead of adding and removing from the end of the array, we need to keep track of which element of the array represents the current top of the stack we need a field for this index

import java. util. Arrays; import java. util import java.util.Arrays; import java.util.EmptyStackException; public class IntStack { // the initial capacity of the stack private static final int DEFAULT_CAPACITY = 16; // the array that stores the stack private int[] stack; // the index of the top of the stack (equal to -1 for an empty stack) private int topIndex;

/. Create an empty stack. / public IntStack() { this /** * Create an empty stack. */ public IntStack() { this.stack = new int[IntStack.DEFAULT_CAPACITY]; this.topIndex = -1; }

Implementation with Array IntStack t = new IntStack(); this.topIndex == -1 this.stack 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 index

Implementation with Array pushing a value onto the stack: increment this.topIndex set the value at this.stack[this.topIndex]

Implementation with Array IntStack t = new IntStack(); t.push(7); this.topIndex == 0 this.stack 7 1 2 3 4 5 6 8 9 10 11 12 13 14 15 index

Implementation with Array IntStack t = new IntStack(); t.push(7); t.push(-5); this.topIndex == 1 this.stack 7 -5 1 2 3 4 5 6 8 9 10 11 12 13 14 15 index

Implementation with Array popping a value from the stack: get the value at this.stack[this.topIndex] decrement this.topIndex return the value notice that we do not need to modify the value stored in the array

Implementation with Array IntStack t = new IntStack(); t.push(7); t.push(-5); int value = t.pop(); // value == -5 this.topIndex == 0 this.stack 7 -5 1 2 3 4 5 6 8 9 10 11 12 13 14 15 index

/. Pop and return the top element of the stack /** * Pop and return the top element of the stack. * * @return the top element of the stack * @throws EmptyStackException if the stack is empty */ public int pop() { // is the stack empty? if (this.topIndex == -1) { throw new EmptyStackException(); } // get the element at the top of the stack int element = this.stack[this.topIndex]; // adjust the top of stack index this.topIndex--; // return the element that was on the top of the stack return element;

Implementation with Array // stack state when we can safely do one more push this.topIndex == 14 this.stack 7 -5 6 3 2 1 9 -3 -2 4 5 8 10 11 12 13 14 15 index

/. Push an element onto the top of the stack /** * Push an element onto the top of the stack. * * @param element the element to push onto the stack */ public void push(int element) { // is there capacity for one more element? if (this.topIndex < this.stack.length - 1) { // increment the top of stack index and insert the element this.topIndex++; this.stack[this.topIndex] = element; } else {

Adding Capacity if we run out of capacity in the current array we need to add capacity by doing the following: make a new array with greater capacity how much more capacity? copy the old array into the new array set this.stack to refer to the new array push the element onto the stack

else { // make a new array with double previous capacity int[] newStack = new int[this.stack.length * 2]; // copy the old array into the new array for (int i = 0; i < this.stack.length; i++) { newStack[i] = this.stack[i]; } // refer to the new array and push the element onto the stack this.stack = newStack; this.push(element);

Adding Capacity when working with arrays, it is a common operation to have to create a new larger array when you run out of capacity in the existing array you should use Arrays.copyOf to create and copy an existing array into a new array

else { // make a new array with greater capacity int[] newStack = new int[this.stack.length * 2]; // copy the old array into the new array for (int i = 0; i < this.stack.length; i++) { newStack[i] = this.stack[i]; } // refer to the new array and push the element onto the stack this.stack = newStack; this.push(element); int[] newStack = Arrays.copyOf(this.stack, this.stack.length * 2);