Heap: Difference between revisions
Line 13: | Line 13: | ||
<font color=darkkhaki>More details [[CLRS]] page 151, page 1177.</font> | <font color=darkkhaki>More details [[CLRS]] page 151, page 1177.</font> | ||
=Canonical Use of a Heap= | =Canonical Use of a Heap= | ||
The primary reason to use a heap is if you notice that your program does repeated minimum (maximum) computations, usually via exhaustive search. | The primary reason to use a heap is if you notice that your program does repeated minimum (maximum) computations, usually via exhaustive search. Also see: {{Internal|Heap Sort|Heap Sort}} | ||
=Supported Operations= | =Supported Operations= |
Revision as of 22:17, 9 October 2021
External
Internal
Overview
This article is about binary heaps. A binary heap data structure is an array where data is placed to form a complete binary tree, plus the index of the last node in the heap. Each element of the array contains a pointer to tree nodes. Each node contains at least a key. The heap works by comparing key and placing the pointer of the associated nodes in the right position in the heap. Duplicate key values are supported.
More details CLRS page 151, page 1177.
Canonical Use of a Heap
The primary reason to use a heap is if you notice that your program does repeated minimum (maximum) computations, usually via exhaustive search. Also see:
Supported Operations
INSERT
Insert a node in the tree. The running time is O(log n) where n is the number of nodes.
Also see:
REMOVE-MIN
Remove the node from the heap with minimum key value. The running time is O(log n) where n is the number of nodes.
Also see:
Min Heap and Max Heap
A heap is constructed in such a way that it maintains a minimum value or a maximum value, but not both. A heap that maintains the minimum value is called a min heap. A heap that maintains the maximum value is called a max heap. If both extracting minimum and maximum are required, use a binary search tree instead.
HEAPIFY
Initialize the heap in linear time O(n). Using INSERT would take O(n log n), but HEAPIFY does it in O(n) when invoked on a batch of n elements.
DELETE
Delete an arbitrary node in the middle of the heap in O(log n) time. This is useful when implementing Dijkstra's Algorithm
Also see: