Download presentation
Presentation is loading. Please wait.
Published byDonát Vincze Modified over 5 years ago
1
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
We use an array because there’s not much faster than finding the kth value in an array DATA ->hash function ->index Hash function used for both STORING AND THEN FINDING LATER
2
Hash Map Example Student objects:
Sobj *stud1 = new Sobj(“Smith”,”Sam”,”Sophomore”,”ELEG”,3.4, ) Sobj *stud2 = new Sobj(“Kell”,”Taylor”,”Freshman”,”CISC”,2.9, ) Sobj *stud3 = new Sobj(“Smith”,”Taylor”,”Freshman”,”ELEG”,3.4, ) (the unique element about each student is their id) Hash function we’ll use is folding with width of 3 Since the original set of data consists of 3 objects, the ideal array size would be 7 ( )%7 = 2 ( )%7 = 5 ( )%7 = 0 Once students are stored in array, now when I want to find info about a particular student, I take their id, fold it, and go to that index in the array (0(1))! E.g., what if I wanted to find Sam Smith’s GPA? ( )%7 = 2, go to index to and get her GPA! 1 2 3 4 5 6 Taylor Smith, Freshman, ELEG, 3.4, Sam Smith, Sophomore, ELEG, 3.4, Taylor Kell, Freshman, CISC, 2.9,
3
So Far: Arrays: find the kth value, traversing quickly!
Linked lists: join, push, pop (stacks) AVL Trees: insert, delete, search in equally efficient time, no extra space HashMap: Finding data quickly!!! (at the cost of extra memory)
4
Binary Heaps What if we’re concerned with finding the most relevant data? A binary heap is a binary tree (2 or fewer subtrees for each node) A heap is structured so that the node with the most relevant data is the root node, the next most relevant as the children of the root, etc. A heap is not a binary search tree A heap does not order its node A heap is a complete tree. Every level but the leaf level is full Leaf level full from left to right Binary Heap – another way to implement a priority queue!
5
Definition of a Binary Heap
A tree is a binary heap if It is a complete tree Every level is full except the bottom level The value in the root is the largest of the tree (Or smallest, if the smallest value is the most relevant and that is how you choose to structure your tree) Every subtree is also a binary heap Equivalently, a complete tree is a binary heap if Node value > child value, for each child of the node Note: This use of the word “heap” is entirely different from the heap that is the allocation area in C++
6
Is this a Binary Heap?
7
Inserting an Item into a Binary Heap
Insert the item in the next position across the bottom of the complete tree: preserves completeness Restore “heap-ness”: while new item is not root and is greater than its parent swap new item with its parent
8
Insert 80? 80 74 80 6 66 80 How many steps? At most, how many steps to insert?
9
Removing an Item We always remove the top (root) node!
Heaps find the largest (or smallest) value in a set of numbers and that number is at the root! Remove the root Leaves a “hole”: Fill the “hole” with the last item (lower right-hand leaf) L Preserve completeness Swap L with largest child, as necessary Restore “heap-ness”
10
Remove? 89 80 66 66 74 66 How many steps? At most, how many steps to remove? Next: how would we implement a heap?
11
Implementing a Heap Yeah, yeah, we could use nodes and pointers, but… Recall: a heap is a complete binary tree If we know the number of nodes ahead of time, a complete binary tree fits nicely in an array: The root is at index 0 Children of 0 are at 1 and 2 Children of 1 are at 3 and 4 Children of 2 are at 5 and 6 Children of 3 are at 7 and 8 Children of 4 are at 9 and 10 Is there a formula for figuring out where children of a node are? Parents? Where would we insert the next node (the child of the node containing 7)? 11 11 8 7 2 5 4 1 3 6 7 8 3 8 1 7 2 2 3 5 5 4 4 5
12
Inserting into a Heap Insert new item at end; set child to curr_size-1
Set parent to (child – 1)/2 while (parent ≥ 0 and arr[parent] < arr[child]) Swap arr[parent] and arr[child] Set child equal to parent // so child is now (child – 1)/2 Set parent to (child – 1) / 2 How do we delete?
13
Deleting from a Heap Set arr[0] to arr[curr_size-1], and shrink curr_size by 1 Set parent to 0 flag = true while (flag) Set leftchild to 2 * parent + 1, and rightchild to leftchild + 1 if leftchild ≥ curr_size, flag is false else: Set maxchild to leftchild If rightchild < curr_size and arr[rightchild] > arr[leftchild] set maxchild to rightchild If arr[parent] ≥ arr[maxchild], Flag is false Swap arr[parent] and arr[maxchild]; set parent to maxchild
14
Performance of Heap A complete tree of height h has:
Less than 2h nodes (why?) At least 2h-1 nodes Thus complete tree of n nodes has height O(log n) Insertion and deletion at most are O(log n), always Heap is useful for priority queues
15
Which of the following is (are) a heap?
1 2 3 4 5 6 7 8 9 42 21 58 12 31 48 64 14 29 A 1 2 3 4 5 6 7 8 9 42 41 35 39 37 24 12 38 36 B 1 2 3 4 5 6 7 8 9 thunder storm mud squirrels grass flowers dandelions petunias spring baby birds C
16
Try: 18’s parent is? Perform a delete 1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 58 45 56 20 33 50 18 19 24 41 18’s parent is? (7-1)/2, or 20 (at location 3) Perform a delete Remove 58 41 goes to position 0 Bubble 41 down 1 2 3 4 5 6 7 8 9 10 11 12 13 14 41 45 56 20 33 50 18 19 24 1 2 3 4 5 6 7 8 9 10 11 12 13 14 56 45 41 20 33 50 18 19 24 1 2 3 4 5 6 7 8 9 10 11 12 13 14 56 45 50 20 33 41 18 19 24
18
Heapsort Heapsort Idea: Works in place: no additional storage
Insert each element into a priority queue Repeatedly remove from priority queue to array Array slots go from 0 to n-1
19
Heapsort Picture
20
Algorithm for In-Place Heapsort
Build heap starting from unsorted array While the heap is not empty Remove the first item from the heap: Swap it with the last item Restore the heap property
21
Heapsort Analysis Insertion cost is log n for heap of size n
This is O(n log n) Removal cost is also log n for heap of size n Total removal cost = O(n log n) Total cost is O(n log n)
22
Heapsort Picture
24
Making Heap: Could: OR: Insert each number in a sequence into the heap
As you insert, bubble each number up OR: Just insert all numbers into array. Check each node at leaf level with its parent. Make a bunch of tiny heaps Then check each node at height of 2 with its parent Make heaps by switching just max child and parent Continue until you get to root Quicker
25
Make a heap (efficiently!):
37, 19,15,16,20,46,44,47,41,42,23,22,40,10,25
26
Example of making a heap:
37, 19,15,16,20,46,44,47,41,42,23,22,40,10,25 37 19 15 16 20 46 44 47 41 42 23 22 40 10 25 Round 1: Level 1 and 2: switch 47 and 16, 42 and 20, 46 stays, and 44 stays
27
Example of making a heap:
37, 19,15,16,20,46,44,47,41,42,23,22,40,10,25 37 19 15 47 42 46 44 16 41 20 23 22 40 10 25 Round 1: Level 1 and 2: switch 47 and 16, 42 and 20, 46 stays, and 44 stays Round 2: Level 2 and 3: switch 47 and 19, then 41 and 19, switch 46 and 15, then 40 and 15
28
Example of making a heap:
37, 19,15,16,20,46,44,47,41,42,23,22,40,10,25 37 47 46 41 42 40 44 16 19 20 23 22 15 10 25 Round 1: Level 1 and 2: switch 47 and 16, 42 and 20, 46 stays, and 44 stays Round 2: Level 2 and 3: switch 47 and 19, then 41 and 19, switch 46 and 15, then 40 and 15 Round 3: Level 1 and 2: switch 47 and 37, then 42 and 37
29
Example of making a heap:
37, 19,15,16,20,46,44,47,41,42,23,22,40,10,25 47 42 46 41 37 40 44 16 19 20 23 22 15 10 25 Round 1: Level 1 and 2: switch 47 and 16, 42 and 20, 46 stays, and 44 stays Round 2: Level 2 and 3: switch 47 and 19, then 41 and 19, switch 46 and 15, then 40 and 15 Round 3: Level 1 and 2: switch 47 and 37, then 42 and 37
30
E.G.: 37, 19,15,16,20,46,44,47,41,42,23,22,40,10,25 H:4 H:3 H:2 H:1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 37 19 15 16 20 46 44 47 41 42 23 22 40 25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 37 19 15 47 42 46 44 16 41 20 23 22 40 25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 37 47 46 41 42 40 44 16 19 20 23 22 15 25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 47 42 46 41 37 40 44 16 19 20 23 22 15 25 Now we have a heap!
32
Example: Selection problem (Kth largest)
If we had a set of unordered numbers, how would we determine the kth largest (or smallest, by reversing the process)? E.g., 11, 7, 1, 3, 8, 4, 9, 2, 6, 10, 5 What if we wanted the 5th smallest (largest) element? How would we do this? How long would it take?
33
Can we do better? Of course. 11, 7, 1, 3, 8, 4, 9, 2, 6, 10, 5 (for this, pretend as if we’re looking for the kth smallest element) 1. build a heap with the array with the smallest value at the top. 2. delete k elements from the heap. Running time? 11 10 5 3 8 2 4 8 1 1 2 3 4 10 11 2 5 6 8 3 8 4 7 11 3 6 8 10 5 7 8 9 11 6 8 10 8 So 5 is the 5th smallest element in the list. Can we do better than this????
34
Better: 11, 7, 1, 3, 8, 4, 9, 2, 6, 10, 5 build a heap (largest elements at top) with just the first k elements. 11 4 2 6 5 8 7 7 2 4 4 8 1 3 7 4 2 2. Compare rest of elements with heap. If the new element is smaller than the root, we insert the new element and remove the root. 3. Otherwise we ignore. Note: we are finding the kth smallest element. To find the kth largest element, we would make a heap with the smallest number as the root, and ascend as we move down. Then new elements would be inserted if they were larger than the root.
35
Analysis: We’re finding the kth smallest in an array of data
K must be smaller than n All n elements must at a minimum be compared to the root, In the worst case, must bubble down log k Worst case analysis: n log k
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.