data structures & programming golnar sheikhshab heap€¦ · heap-order property: in a heap t,...
TRANSCRIPT
![Page 1: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/1.jpg)
Data Structures & Programming
Heap
Golnar Sheikhshab
![Page 2: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/2.jpg)
HeapsA binary Tree with two extra properties:
Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal to the key associated with v’s parent.
Complete Binary Tree Property: A heap T with height h is a complete binary tree, that is, levels 0,1,2,...,h−1 of T have the maximum number of nodes possible (namely, level i has 2^i nodes, for 0 ≤ i ≤ h − 1) and the nodes at level h fill this level from left to right.
![Page 3: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/3.jpg)
![Page 4: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/4.jpg)
Note1. We defined a min-heap. One can define a max-heap similarly (Like in STL
priority Queue).
2. The element with minimum key is on top of the heap (at the root) and in each path from root to an external node, keys are ordered non-decreasingly.
3. Do not confuse the data structure heap with the freestore memory heap (Section 14.1.1) used in the run-time environment supporting programming languages like C++.
![Page 5: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/5.jpg)
The Height of a Heap
We like this property because it helps us do both add(e) and remove_min() in O(log n)
![Page 6: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/6.jpg)
Insertion to a Heap (1)
![Page 7: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/7.jpg)
Insertion to a Heap (2)
![Page 8: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/8.jpg)
Removal (1)
![Page 9: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/9.jpg)
Removal (2)
![Page 10: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/10.jpg)
Complexity Aanalysis
![Page 11: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/11.jpg)
Heap Implementation Usually array/vector based
![Page 12: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/12.jpg)
C++ Implementation
![Page 13: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/13.jpg)
C++ Implementation
![Page 14: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/14.jpg)
![Page 15: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/15.jpg)
![Page 16: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/16.jpg)
![Page 17: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/17.jpg)
![Page 18: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/18.jpg)
![Page 19: Data Structures & Programming Golnar Sheikhshab Heap€¦ · Heap-Order Property: In a heap T, for every node v other than the root, the key associated with v is greater than or equal](https://reader036.vdocuments.us/reader036/viewer/2022070811/5f0a1c557e708231d42a107e/html5/thumbnails/19.jpg)
Reading Material 8.3.1 - 8.3.4