binary heap - scientific computing and imaging institutemiriah/cs2420/lectures/l19-binary-heap.pdf-a...
TRANSCRIPT
![Page 1: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/1.jpg)
cs2420 | Introduction to Algorithms and Data Structures | Spring 2015BINARY HEAP
1
![Page 2: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/2.jpg)
administrivia…
2
![Page 3: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/3.jpg)
3
-assignment 10 is due on Thursday
-midterm grades out tomorrow
![Page 4: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/4.jpg)
last time…
4
![Page 5: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/5.jpg)
5
-a hash table is a general storage data structure
-insertion, deletion, and look-up are all O(c)
-like a stack, but not limited to top item
Hash tables
• A hash table is a general storage data structure
• Insertion/deletion/lookup all constant time
• Like a stack, but not limited to top item
!
Access Insertion Deletion Notes
Hash Table Constant Constant Constant Magic?
![Page 6: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/6.jpg)
6
-underlying data structure is just an array
-requires that all data types inserted have a hash function
-map the hash value to a valid index of the array using %
-use hash value to instantly look-up the index of any item-insertion, deletion, and search: O(1)
-assuming the hash function is O(1)!
![Page 7: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/7.jpg)
linear probing
7
![Page 8: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/8.jpg)
8
-remember: it is NOT required that two non-equal object have different hash values
-because of this, it is possible for two different objects to has to the same index
-this is called a collision
12, 15, 17, 46, 89, 90insert:
array:
index: 0 1 2 3 4 5 6 7 8 9
12 15 1746 8990
, 92
collision! where can we put 92?
![Page 9: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/9.jpg)
clustering-if an item’s natural spot is taken, it goes in the next open spot, making a cluster for that hash
-clustering happens because once there is a collision, there is a high probability that there will be more -this means that any item that hashes into the cluster will require several attempts to resolve the collision
-feedback loop:-the bigger the clusters are, the more likely they are to be hit -when a cluster gets hit, it gets bigger
9
![Page 10: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/10.jpg)
quadratic probing
10
![Page 11: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/11.jpg)
11
-quadratic probing attempts to deal with the clustering problem
-if hash(item) = H, and the cell at H is occupied:-try H+12
-then H+22
-then H+32 -and so on… -wrap around to beginning of array if necessary
![Page 12: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/12.jpg)
separate chaining
12
![Page 13: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/13.jpg)
13
-why not make each spot in the array capable of holding more than one item?
-use an array of linked lists -hash function selects index into array -called separate chaining
-for insertion, append the item to the end of the list-insertion is O(1) if we have what?
-searching is a linear scan through the list-fast if the list is short
![Page 14: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/14.jpg)
a bit more on hash functions…
14
![Page 15: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/15.jpg)
15
-ints have an obvious hash value
-what about Strings? Books? Shapes?…
-we must not overlook the requirement of a good hash functions
array:
index: 0 1 2 3 4 5 6 7 8 9
12 15 1746 8990
![Page 16: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/16.jpg)
remember…-hash functions take any item as input and produce an integer as output
-given the same input the function always returns the same output
-two different inputs MAY have the same hash value
16
![Page 17: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/17.jpg)
thinking about chars and Strings
-ASCII defines an encoding for characters-‘a’ = 97 -‘b’ = 98 -… -‘z’ = 122 -‘2’ = 50 -…
-the char type is actually just a small integer-8 bits instead of the usual 32
17
![Page 18: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/18.jpg)
18
![Page 19: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/19.jpg)
19
-a String is essentially an array of char
-Java hides these details
-how can we use this to create a hash function for Strings?
String s = “hello”;
108104 101 108 111
![Page 20: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/20.jpg)
review-O(1) for all major operations
-assuming λ is managed
-linear probing-has clustering problems
-quadratic probing-has lesser clustering problems -requires λ < 0.5, and prime table size
-separate chaining-probably the easiest to implement, as well as the best performing
20
![Page 21: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/21.jpg)
21
what is the load factor λ for the following hash table?A) 4 B) 6 C) 0.4 D) 0.5 E) 0.6
104 34 19 111 5298
![Page 22: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/22.jpg)
22
using linear probing, in what index will item 93 be added?A) 1 B) 5 C) 6 D) 7
array:
index: 0 1 2 3 4 5 6 7 8 9
891849 349 58
![Page 23: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/23.jpg)
23
using quadratic probing, in what index will item 22 be added?A) 1 B) 5 C) 6 D) 7
array:
index: 0 1 2 3 4 5 6 7 8 9
891849 349 58
![Page 24: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/24.jpg)
recap-i heart hash tables
-collection structure with O(1) for major operations
-but!…-hash function must minimize collisions
-should evenly distribute values across all possible integers
-collisions must be carefully dealt with -hash function runtime must be fast
-no ordering -how do we find the smallest item in a hash table? -in a BST?
24
![Page 25: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/25.jpg)
priority queues
25
![Page 26: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/26.jpg)
26
-a priority queue is a data structure in which access is limited to the minimum item in the set-add -findMin -deleteMin
-add location is unspecified, so long as the the above is always enforced
-what are our options for implementing this?
![Page 27: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/27.jpg)
27
-option 1: a linked list-add: O(1) -findMin: O(N) -deleteMin: O(N) (including finding)
-option 2: a sorted linked list-add: O(N) -findMin: O(1) -deleteMin: O(1)
-option 3: a self-balancing BST-add: O(logN) -findMin: O(logN) -deleteMin: O(logN)
![Page 28: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/28.jpg)
complete trees
28
![Page 29: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/29.jpg)
29
-a complete binary tree has its levels completely filled, with the possible exception of the bottom level
-bottom level is filled from left to right
-each level has twice as many nodes as the previous level
![Page 30: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/30.jpg)
complete trees as an array-if we are guaranteed that tree is complete, we can implement it as an array instead of a linked structure
-the root goes at index 0, its left child at index 1, its right child at index 2
-for any node at index i, it two children are at index (i*2) + 1 and (i*2) + 2
30
![Page 31: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/31.jpg)
31
-for example, d’s children start at (3*2) + 1
-how can we compute the index of any node’s parent?
a
b c
d e f g
h i j
index: 0 1 2 3 4 5 6 7 8 9
gfa ecb d h i j10
![Page 32: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/32.jpg)
32
-luckily, integer division automatically truncates
-any node’s parent is at index (i-1) / 2
a
b c
d e f g
h i j
index: 0 1 2 3 4 5 6 7 8 9
gfa ecb d h i j10
![Page 33: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/33.jpg)
complete trees as an array-keep track of a currentSize variable
-holds the total number of nodes in the tree -the very last leaf of the bottom level will be at index currentSize - 1
-when computing the index of a child node, if that index is >= currentSize, then the child does not exist
33
![Page 34: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/34.jpg)
traversal helper methodsint leftChildIndex(int i) { return (i*2) + 1;
}
int rightChildIndex(int i) { return (i*2) + 2;
}
int parentIndex(int i) { return (i-1) / 2;
}
![Page 35: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/35.jpg)
binary heap
35
![Page 36: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/36.jpg)
36
-a binary heap is a binary tree with two special properties
-structure: it is a complete tree -order: the data in any node is less than or equal to the data of its children
-this is also called a min-heap
-a max-heap would have the opposite property
![Page 37: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/37.jpg)
37
-order of children does not matter, only that they are greater than their parent
3
22 6
31 43 17 92
100 56 45
![Page 38: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/38.jpg)
38
is this a min-heap?A) yes B) no 1
14 12
31 29 17 4
![Page 39: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/39.jpg)
39
is this a min-heap?A) yes B) no 1
14 12
31 29 17 90
42 23
![Page 40: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/40.jpg)
adding to a heap
40
![Page 41: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/41.jpg)
41
-we must be careful to maintain the two properties when adding to a heap
-structure and order
-deal with the structure property first… where can the new item go to maintain a complete tree?
-then, percolate the item upward until the order property is restored
-swap upwards until > parent
![Page 42: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/42.jpg)
42
3
22 6
31 43 17 92
100 56 45
adding 14
put it at the end of the tree
![Page 43: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/43.jpg)
43
3
22 6
31 43 17 92
100 56 45
adding 14
put it at the end of the tree
14
![Page 44: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/44.jpg)
44
3
22 6
31 43 17 92
100 56 45
adding 14
put it at the end of the tree
percolate up the tree to fix the order
14
![Page 45: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/45.jpg)
45
3
22 6
31 43 17 92
100 56 45
adding 14
put it at the end of the tree
percolate up the tree to fix the order
14
![Page 46: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/46.jpg)
46
3
22 6
31 14 17 92
100 56 45
adding 14
put it at the end of the tree
percolate up the tree to fix the order
43
![Page 47: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/47.jpg)
47
3
22 6
31 14 17 92
100 56 45
adding 14
put it at the end of the tree
percolate up the tree to fix the order
43
![Page 48: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/48.jpg)
48
3
14 6
31 22 17 92
100 56 45
adding 14
put it at the end of the tree
percolate up the tree to fix the order
43
![Page 49: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/49.jpg)
49
3
14 6
31 22 17 92
100 56 45
adding 14
put it at the end of the tree
percolate up the tree to fix the order
43
![Page 50: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/50.jpg)
50
3
14 6
31 22 17 92
100 56 45
adding 14
put it at the end of the tree
percolate up the tree to fix the order
43
![Page 51: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/51.jpg)
cost of add-percolate up until smaller than all nodes below it…
-how many nodes are there on each level (in terms of N)?
-about half on the lowest level -about 3/4 in the lowest two levels
51
![Page 52: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/52.jpg)
52
-if the new item is the smallest in the set, cost is O(logN)
-must percolate up every level to the root -complete trees have logN levels
-is this the worst, average, or best case?
-it has been shown that on average, 2.6 comparisons are needed for any N
-thus, add terminates early, and average cost is O(1)
![Page 53: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/53.jpg)
remove
53
![Page 54: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/54.jpg)
54
3
14 16
31 22 17 92
100 56 45
let’s remove the smallest item
Take out 3
43
![Page 55: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/55.jpg)
55
14
31 22 17 92
100 56 45
let’s remove the smallest item
Take out 3
43
16
![Page 56: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/56.jpg)
56
14
31 22 17 92
100 56 45
let’s remove the smallest item
Take out 3
fill with last item on last level. why?
43
16
![Page 57: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/57.jpg)
57
14
31 22 17 92
100 56 45
let’s remove the smallest item
Take out 3
fill with last item on last level. why?
43
16
![Page 58: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/58.jpg)
58
14
31 22 17 92
100 56 45
let’s remove the smallest item
Take out 3
fill with last item on last level. why?
43
16
![Page 59: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/59.jpg)
59
14
31 22 17 92
100 56 45
let’s remove the smallest item
Take out 3
fill with last item on last level. why?
percolate down
43
16
![Page 60: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/60.jpg)
60
14
31 22 17 92
100 56 45
let’s remove the smallest item
Take out 3
fill with last item on last level. why?
percolate down
43
16
![Page 61: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/61.jpg)
61
43
31 22 17 92
100 56 45
let’s remove the smallest item
Take out 3
fill with last item on last level. why?
percolate down
14
16
![Page 62: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/62.jpg)
62
43
31 22 17 92
100 56 45
let’s remove the smallest item
Take out 3
fill with last item on last level. why?
percolate down
14
16
![Page 63: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/63.jpg)
63
22
31 43 17 92
100 56 45
let’s remove the smallest item
Take out 3
fill with last item on last level. why?
percolate down
14
16
![Page 64: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/64.jpg)
64
22
31 43 17 92
100 56 45
let’s remove the smallest item
Take out 3
fill with last item on last level. why?
percolate down
14
16
![Page 65: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/65.jpg)
65
22
31 43 17 92
100 56 45
let’s remove the smallest item
Take out 3
fill with last item on last level. why?
percolate down
14
16
![Page 66: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/66.jpg)
cost of remove-worst case is O(logN)
-percolating down to the bottom level
-average case is also O(logN)-rarely terminates more than 1-2 levels from the bottom… why?
66
![Page 67: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/67.jpg)
recap
67
![Page 68: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/68.jpg)
68
-priority queues can be implemented any number of ways
-a binary heap’s main use is for implementing priority queues
-remember, the basic priority queue operations are:-add -findMin -deleteMin
![Page 69: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/69.jpg)
69
-the average cases for a PQ implemented with a binary heap:
-add -O(1): percolate up (average of 2.6 compares)
-findMin - O(1): just return the root
-deleteMin - O(logN): percolate down (rarely terminates before near the bottom of the tree)
![Page 70: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/70.jpg)
next time…
70
![Page 71: BINARY HEAP - Scientific Computing and Imaging Institutemiriah/cs2420/lectures/L19-binary-heap.pdf-a binary heap is a binary tree with two special properties-structure: it is a complete](https://reader033.vdocument.in/reader033/viewer/2022053018/5f1ebedd1d2d7229b467f31d/html5/thumbnails/71.jpg)
71
-reading -chapter 21 in book
-homework-assignment 10 due Thursday