EECS 281, Week 3: Monday
STL Containers
Type Containers
vector, deque, list, forward list
(multi)set, (multi)map
unordered_(multi)set, unordered_(multi)map
queue, stack, priority queue
Vector
Deque
Vector vs. Deque?
Array
Maxim Aleksa ([email protected]) 0 < 5 http://maximal.io/eecs281 EECS 281, Week 3: Monday
List
Forward List
Iterator Invalidation
Vector ______
Deque ______
List ______
Sequence Containers Summary
Container insert/remove front insert/remove middle insert/remove back search
Vector
Deque
Array
List
Forward List
Associative Containers Set Multiset Map Multipap
Unordered Associative Containers Unordered set Unordered multiset Unordered map Unordered multipap
Maxim Aleksa ([email protected]) 4 < 5 http://maximal.io/eecs281 EECS 281, Week 3: Monday
Priority Queue
Heap The (binary) heap data structure is an array that represents a nearly complete binary tree.
0 16
1 14 2 10
3 4 5 6 8 7 9 3 7 8 9 2 4 1
Heap property
Max-heap property: ______
Min-heap property: ______
Heap as an array
Number of elements: ______root(): ______parent(i): ______left(i): ______right(i): ______height(): ______
Heap operations buildMaxHeap produce a max-heap from an unordered array maxHeapify(i) correct a single violation of the heap property in a subtree with root at i maxElement return the largest element removeMaxElement remove the largest element increaseKey(i, k) update the key of an element to a larger value k insert(k) add an element with key k
Maxim Aleksa ([email protected]) 5 < 5 http://maximal.io/eecs281 EECS 281, Week 3: Monday maxHeapify
16
4 10
14 7 9 3
2 8 1 buildMaxHeap
4
1 3
2 16 9 10
14 8 7
maxElement removeMaxElement 16
14 10
8 7 9 3
2 4 1 insert 14
8 10
4 7 9 3
2 1
Maxim Aleksa ([email protected]) 6 < 5 http://maximal.io/eecs281 EECS 281, Week 3: Monday
Queue with Stacks Implement NewQueue, a templated class that represents a queue using two stacks. Specifically, implement at least the following member functions:
• size(), which should return the size of the queue
• empty(), which should return true if the queue is empty and false otherwise
• push(), which should insert an element at the “end” of the queue
• pop(), which should remove an element from the “front” of the queue Feel free to use std::stack that has the same methods: size(), empty(), push() and pop().
When finished, take a picture of your code and email it to Maxim. Use additional pages if needed.
Maxim Aleksa ([email protected]) 6 < 5 http://maximal.io/eecs281