130 likes | 282 Views
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.
E N D
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.
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()); Stacks & Queues
Arrays Constant-time access of kth 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 kth item on average Binary search can NOT be implemented on sorted linked lists O(1) insertion O(1) deletion O(1) merging Arrays vs. Linked Lists
Resizable Arrays • Given an array with a capacity of M • Insert N1 items • What if N1 > M? • Resize array to N1 (stupid) • Resize array to 2*N1 (smart) • Why?
Resizable Arrays Insertions Copies Total 1 0 1 i m i 1 1 2 1 2 3 m m i m m m i 1 3 4 m m m m i 1 4 5 m m m m m i 1 5 6 Sum 21
Resizable Arrays Ins Copies Total 1 1 i m i 1 1 2 1 1 i m m m i 1 3 4 i 1 1 i 1 1 Sum 10
Binary Trees • Tries to combine binary search with the advantages of linked lists.
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)
Binary Heaps • Arise from priority queues • Enqueue should be as efficient as possible • Dequeue will always remove the minimum/maximum item.
Binary Heap http://ciips.ee.uwa.edu.au/~morris/Year2/PLDS210/heaps.html