Agenda See schedule HW5 (mini-programming project) due Next Wed. 16th Quiz 6 (double quiz- 50 minutes) will be Next Mon. 14th
Chapter 3 Data Structures –Stacks & Queues –Array vs. Linked Lists –Resizable arrays –Binary Search leads to Binary Trees –Priority Queues lead to Binary Heaps
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.
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.
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());
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
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?
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 Sum
Resizable Arrays i InsCopies 1 m 11 i Total i m 13 m 4 mi i i Sum
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)
Binary Trees 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)
Binary Heaps Arise from priority queues –Enqueue should be as efficient as possible –Dequeue will always remove the minimum/maximum item.
Binary Heap Binary HeapBinary Tree InsertionO(1)O(log n) Delete/Remove Min or Max O(log n) Find/SearchO(n)O(log n)