Download presentation
Presentation is loading. Please wait.
1
Agenda See schedule HW5 (mini-programming project) due Next Wed. 16th Quiz 6 (double quiz- 50 minutes) will be Next Mon. 14th
2
Chapter 3 Data Structures –Stacks & Queues –Array vs. Linked Lists –Resizable arrays –Binary Search leads to Binary Trees –Priority Queues lead to Binary Heaps
3
Stacks & Queues Stacks –FILO (First In – Last Out) –O(1) push –O(1) pop –Ensuring efficient push and pop means that iteration may not be possible.
4
Stacks & Queues Queues –FIFO (First In – First Out) –O(1) push or enqueue –O(1) pop or dequeue –Ensuring efficient push and pop means that iteration may not be possible.
5
Stacks & Queues Queue q; Stack s1, s2; q.push(1) s1.push(2) s2.push(3) q.push(s2.pop()) q.push(4) s2.push(5) s1.push(6) s2.push(q.pop()); s1.push(s2.pop()); s2.pop() s1.push(s2.pop());
6
Arrays vs. Linked Lists Arrays Constant-time access of k th item Binary search can be implemented on sorted arrays O(n) insertion O(n) deletion O(n) merging and resizing Linked List O(n)-time to access k th item on average Binary search can NOT be implemented on sorted linked lists O(1) insertion O(1) deletion O(1) merging
7
Resizable Arrays Given an array with a capacity of M Insert N 1 items What if N 1 > M? Resize array to N 1 (stupid) Resize array to 2*N 1 (smart) Why?
8
Resizable Arrays i InsertionsCopies 10 m 11 i Total 1 2 m 12 m 3 i m 13 m 4 mi mmmm mmmm i mi 145 156 21Sum
9
Resizable Arrays i InsCopies 1 m 11 i Total 1 2 11 i m 13 m 4 mi i i 11 11 10Sum
10
Binary Trees Tries to combine binary search with the advantages of linked lists. ArrayLinked ListBinary Tree Raw InsertO(n)O(1) Raw DeleteO(n)O(1) Find/SearchO(log n)O(n)O(log n)
11
Binary Trees http://www.cs.jhu.edu/~goodrich/dsa/trees/btree.html Insertion = Find + Raw Insert –O(log n) + O(1) = O(log n) Deletion = Find + Raw Delete + Find Replacement –O(log n) + O(1) + O(log n) = O(log n)
12
Binary Heaps Arise from priority queues –Enqueue should be as efficient as possible –Dequeue will always remove the minimum/maximum item.
13
Binary Heap http://ciips.ee.uwa.edu.au/~morris/Year2/PLDS210/heaps.html Binary HeapBinary Tree InsertionO(1)O(log n) Delete/Remove Min or Max O(log n) Find/SearchO(n)O(log n)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.