data structures & programming golnar sheikhshab heap€¦ · heap-order property: in a heap t,...

19
Data Structures & Programming Heap Golnar Sheikhshab

Upload: others

Post on 21-Jun-2020

4 views

Category:

Documents


0 download

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

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

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
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

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

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

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

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

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

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

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

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

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

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
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
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
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
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
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

Reading Material 8.3.1 - 8.3.4