Download presentation
Presentation is loading. Please wait.
Published byElvin Gibbs Modified over 8 years ago
1
Question of the Day How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?
2
Question of the Day How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?
3
CSC 212 – Data Structures
4
Using Stack
5
public interface Stack extends Collection { public E peek() throws EmptyCollectionException; public E pop() throws EmptyCollectionException; public void push(E element); } Any type of data stored within a Stack Generics enable us to avoid rewriting this code Minimum set of exceptions defined by interface Classes could throw more unchecked exceptions Stack Interface
6
Array-based Implementation Array reference in a field Another field tracks top element’s index Stack size also: top Add to next lowest index Remove highest index Algorithm isEmpty() return top == 0 Algorithm pop() if isEmpty() then throw new EmptyCollectionException else top top 1 return stack[top] 012 … ArrayStack stack top 0
7
Array-based Implementation Array reference in a field Another field tracks top element’s index Stack size also: top Add to next lowest index Remove highest index 012 top … ArrayStack stack top 0 Algorithm isEmpty() return top == 0 Algorithm pop() if isEmpty() then throw new EmptyCollectionException else top top 1 return stack[top]
8
top Array-based Implementation Array reference in a field Another field tracks top element’s index Stack size also: top Add to next lowest index Remove highest index ArrayStack stack top X + 1 012 … X Algorithm isEmpty() return top == 0 Algorithm pop() if isEmpty() then throw new EmptyCollectionException else top top 1 return stack[top]
9
Array-based Implementation Array reference in a field Another field tracks top element’s index Stack size also: top Add to next lowest index Remove highest index ArrayStack stack top X 012 … X top Algorithm isEmpty() return top == 0 Algorithm pop() if isEmpty() then throw new EmptyCollectionException else top top 1 return stack[top]
10
Could fill array with data More push() s impossible Throw exception on error Specific to array-based Unchecked exception must be used Algorithm push(elem) if top == stack.length then throw new FullStackException else stack[top] elem top top + 1 fi ArrayStack stack top X 012 … X top
11
Could fill array with data More push() s impossible Throw exception on error Specific to array-based Unchecked exception must be used Algorithm push(elem) if top == stack.length then throw new FullStackException else stack[top] elem top top + 1 fi ArrayStack stack top X + 1 012 … X top
12
Could fill array with data More push() s impossible Throw exception on error Specific to array-based Unchecked exception must be used Algorithm push(elem) if top == stack.length then throw new FullStackException else stack[top] elem top top + 1 fi ArrayStack stack top length 012 … top
13
Oops… My Bad Could fill array with data More push() s impossible Throw exception on error Specific to array-based Unchecked exception must be used Algorithm push(elem) if top == stack.length then throw new FullStackException else stack[top] elem top top + 1 fi ArrayStack stack top length 012 … top
14
Simple Array-based Stack Why It RocksWhy It Sucks Easy to write & read Simple to find bugs Quick running times Methods take O(1) time huge Array must be huge Max. possible elements Problems occur when too many exist at once When full, throws specific exception
15
Better Approach (Maybe?) Implement with array, but allow resizing array Grow as needed rather that stating full (In Java) Requires allocating new, larger array Copy data into new array from current array Reassign field so that it now aliases larger array Arrays.copyOf() does everything in single call
16
Better Approach (Maybe?) Implement with array, but allow resizing array Grow as needed rather that stating full (In Java) Requires allocating new, larger array Copy data into new array from current array Reassign field so that it now aliases larger array Arrays.copyOf() does everything in single call Method call is O (1), but method takes O ( n ) time push() now O ( n ) in worst-case, but how bad is that?
17
Ways to Grow Array Two ways to increase array size Constant value (e.g., 2, 4, 6, 8, 10…) Constant factor (e.g., 2, 4, 8, 16, 32…) Both approaches requires O(n) time… Instantiating & copying array are slow steps average …average (amortized) costs differ, however Difference in how often slow step needed
18
Increase Array Size by c To hold n elements, must grow k = n / c times Copy array each growth, so total copies is: 1 + (c+1) + + (((k-1)*c)+1) + (k*c+1) = 1 + (k*c+1) + (c+1) + (((k-1)*c)+1) + = ((k * c) + 2) + ((k * c) + 2) + = k / 2 * ((k * c) + 2) = O(c*k 2 )= O(c * ( n / c ) 2 ) = O(n 2 * 1 / c ) = O(n 2 ) Averaged over adding n elements = O(n) each!
19
Grow by Constant Factor To hold n elements, need to grow k = log n times Still copy array each time, so will make this many copies: 1 + 2 + 4 + + 2 k-1 + 2 k = (2 k - 1) + 2 k = 2 k +1 - 1 = (2 * 2 k ) - 1 = (2 * 2 log n ) - 1 = O(2n - 1) = O(n) Average cost is O(1) ( O(n) copies ÷ n elements)
20
Your Turn Get into your groups and complete activity
21
For Next Lecture Read 4.1 – 4.3 before Wednesday's class Can we do better than resizable arrays? GENIUS Didn’t some GENIUS find too much memory expensive? How could we grow & shrink memory demands? Is linked list an ADT? Why or why not? Week #7 weekly assignment due Wednesday Programming Assignment #1 due in 2 weeks Start before it gets too late! Planning saves time!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.