Heap A heap is a specialized treebased data
Heap • A heap is a specialized tree-based data structure (Binary Tree). • The tree is completely filled on all levels except possibly the lowest, which is filled from the left up to a point. 1
Heap • A heap is a specialized tree-based data structure (Binary Tree). • The tree is completely filled on all levels except possibly the lowest, which is filled from the left up to a point. • Min-Heap − Where the value of the root node is less than or equal to either of its children. • Max-Heap − Where the value of the root node is greater than or equal to either of its children. Min of Min-Heap: O(1) Max of Max-Heap: O(1) 2
Heap • A heap is a specialized tree-based data structure (Binary Tree). The tree is completely filled on all levels except possibly the lowest, which is filled from the left up to a point. • Storing Heap in an array is more efficient than other structure! Why? • An array A that represents a heap is an object with two attributes: • A. length, which (as usual) gives the number of elements in the array. • A. heap-size, which represents how many elements in the heap are stored within array A. 3
Heap • Min-Heap − Where the value of the root node is less than or equal to either of its children. • Max-Heap − Where the value of the root node is greater than or equal to either of its children. 4
Heap Insertion • A new data is inserted to the bottom level of heap, left as possible. • The heap must be Complete. • Therefore, we need to Heapify the heap. 5
Heapify 16 1 4 1 0 8 7 9 3 2 4 1 16 4 1 0 1 4 7 9 3 2 8 1 16 1 4 1 0 4 7 9 3 2 8 1 6
Max Heap Construction Step 1 − Create a new node at the end of heap. Step 2 − Assign new value to the node. Step 3 − Compare the value of this child node with its parent. Step 4 − If value of parent is less than child, then swap them. Step 5 − Repeat step 3 & 4 until Heap property holds. . 7
Max Heap Deletion Algorithm Step 1 − Remove root node. Step 2 − Move the last element of last level to root. Step 3 − Compare the value of this child node with its parent. Step 4 − If value of parent is less than child, then swap them. Step 5 − Repeat step 3 & 4 until Heap property holds. 8
Min-Max Heap 7 min 70 30 45 9 50 max 40 30 10 20 12 15 j min max 10
Insert to Min-Max Heap 7 min 70 30 45 9 50 max 40 30 10 20 12 15 j min max
Min-Max Heap After Inserting Key 5 5 min 70 30 45 9 50 max 40 30 7 20 12 15 10 min max
Min-Max Heap After Inserting Key 80 7 min 70 30 45 9 50 max 80 30 10 20 12 15 40 min max
Deletion of the Min Element 12 70 9 50 max 40 30 45 min 30 10 20 15 min max
Operations • • • Build Insertion Find Minimum Find Maximum Find Minimum
Deaps • • A deap is a double-ended heap that supports the double-ended priority operations of insert, delet-min, and delete-max. Similar to min-max heap but deap is faster on these operations by a constant factor, and the algorithms are simpler.
Deaps A deap is a complete binary tree that is either empty or satisfies the following properties: (1) The root contains no element (2) The left subtree is a min heap. (3) The right subtree is a max heap. (4) If the right subtree is not empty, then let i be any node in the left subtree. Let j be the corresponding node in the right subtree. If such a j does not exist, then let j be the node in the right subtree that corresponds to the parent of i. The key in node i is less than or equal to that of j.
Insertion Into A Deap 5 45 10 15 8 19 9 i 25 30 20 40 j
Deap Structure After Insertion of 4 4 45 5 15 8 10 9 25 30 20 40 19
Deap Structure After Insertion of 30 5 45 10 15 8 19 9 30 30 20 40 25
- Slides: 20