Dsa Heap
Dsa Heap
Shiksha Online
Updated on Oct 17, 2023 14:23 IST
The use of heaps in algorithms can help you manage your priority list and focus on
the most important tasks. Read this article to learn how to use a heap in algorithms.
Heap is a unique linear tree-based data structure in which the tree is always fully
constructed. Max heaps and Min heaps are two sorts of heaps. The max-heap will
have a root node valued higher than its sub-tree, while the min-heap will have a root
node valued lower than its sub-tree.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Pros: Heap Data Structure
A particular kind of tree data structure is called a heap. A binary tree whereby every
tree element has a minimum of two offspring is known as a heap. The source node
located at the peak of this tree would indicate the smallest kid, and the nodes
situated at the bottom would be the eldest iterations of relatives represented in the
tree.
A genetic ancestry tree is a typical illustration of a binary tree in which each child has
precisely two parents. In most cases, heaps are either min-heap or max-heap trees,
which will be discussed in the following section.
Heap tasks:
Insertion: time-consuming procedure of adding a new element to a current heap (log N).
Deletion: arranging the heap and providing the unit with time complexity O af ter removing
the element with the greatest priority or at the top of the heap (log N).
There are a few characteristics that all heaps have, irrespective of whether they are
min-heap trees or max-heap trees. Because each sibling in max-heap trees has a
bigger value than its early years’ educators and each father in min-heap trees has a
lower value than its kid’s nodes, there is an inferred hierarchy between each parent
node and its children.
Ordering
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
A heap tree’s usefulness depends on the nodes’ arrangement. All heap tree
conditions must be met for each subset in a heap tree. Every parent node in a min-
heap tree is lower than its offspring. As an outcome, a binary tree is created, with
the root node being the simplest model and the branches (the elements at the
bottom of the tree) being the greatest values. Every parent node in a max-heap tree
is bigger than its siblings. Consequently, a binary tree is created, with the root node
being the biggest element and the leaves being the shortest.
Structure
Heap trees have the same structure as a typical binary tree. The result is a tree with
a maximum of two offspring for each parent node. The unique structure of min-
heap trees makes it possible to reach the substring in O(1) time quickly. In a max-
heap tree, finding the biggest member is also O (1).
Min Heap: A node’s value should be lower than its of f spring in the minimum heap. T he
minimal value is kept in the root node.
The root node of binary trees, such as min-heap trees and max-heap trees, is
known as the key. The lowest value in the entire tree must be in the key of a min-
heap tree. Additionally, the offspring of every preferred parent must be more
significant than the parent node. The greatest value in the tree must be in the key of
a max-heap tree. The offspring of each parent node must also be shorter than the
neighbour nodes.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
In a max-heap tree, the youngest sibling would have the highest value, and each age
would have lesser numbers than the children it produced, with the eldest relatives
having a lower number than any subsequent generations. This is similar to the
ancestor tree example from before.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Example:
Assume that R is the root element’s name and that the root’s left and right subtrees
are already heaps. The following graphic represents this circumstance:
Node R’s two subtrees are heaps. R must now be lowered to its suitable position
inside the stack.
R ought to be swapped out for the more valuable kid. R could still be smaller than
one or both of its (new) children, but the outcome will be a heap. In this instance, we
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
keep “pushing down” R until it surpasses its offspring or becomes a leaf node, in
which case we stop. The exclusive method of siftdown is used to carry out this
operation.
According to this method, which presupposes that the subtrees are already heaps, a
full algorithm may be produced by visiting the nodes in a sequence in which the
cluster children are examined before the node. Working from the high index of the
collection to the low index is one quick technique to do this. The construction
procedure can begin with the first internal node in the array’s centre, as the build
process does not need to visit the leaf nodes.
T he process is turned into the following:
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
The following formula calculates the summation of total distances:
It is known that the closed-form result of the total on the right is about 2. Hence the
worst-case execution time of this method is (n) time. In comparison, creating the
heap one member at a time would, in the worst scenario, cost nlogn. Moreover, it is
quicker than the (nlogn) average-case and (n2) worst-case times needed to
construct the BST.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Eliminating an item from the heap or altering its priority:
The cost of removing the highest element is (logn) in both the median and worst
scenarios since the heap is logn levels deep.
Must Read – 8 Most Important Data Structures Every Programmer Must Know
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Storage capacity: Since the heap data structure stores components in a f ull binary tree
structure, it uses lower memory than alternative data structures like clustering or arrays.
Heapsort algorithm: T he heap data structure is the f oundation f or the ef f icient sorting
algorithm known as the heap-sort, which has a worst-case computation time of O. (n
log n).
Effective priority queue: A priority queue is f requently implemented using the heap
data structure, where the primary concern component is always at the highest level of
the heap. T he heap is an ef f ective data structure f or building priority queues because it
of f ers constant-time access to the element with the greatest priority.
Not great for querying: While the heap data structure of f ers rapid access to the
highest element, it is not suitable f or looking f or a particular element in a heap. Looking
f or an item in a heap involves navigating the entire tree, which has a temporal complexity
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
of O(n) (n).
Not a reliable data structure: T he heap data structure is not secure, which implies that
when the heap is built or updated, the comparative position of equal components
cannot be retained.
Algorithms for task scheduling that prioritize or plan tasks according to deadlines
employ the heap data structure. The heap data structure is beneficial for work
scheduling applications because it enables quick access to the task with the
greatest priority.
Graph Algorit hm
Many graph algorithms, including Dijkstra’s, Prim’s, and Kruskal’s algorithms, use the
heap data structure. The heap data structure may be used to construct priority
queues efficiently for these methods.
Memory Management
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Memory management systems employ the heap data structure to allocate and
deallocate memory dynamically. The memory blocks are kept in a heap. The heap
data structure manages them effectively and distributes them to applications as
required.
Priorit y Queues
Priority queues are frequently implemented using the heap data structure. Here,
entries are piled up and sorted according to priority. This makes it an effective data
structure for handling events and activities that require prioritizing since it enables
constant access to the element with the greatest priority.
Heapsort Algorit hm
The heap data structure is the foundation for the efficient sorting algorithm known
as the heapsort, which has a worst-case computation time of O. (n log n). Many
applications, such as database mining and quantitative simulation, employ the
heapsort method.
Conclusion
We have covered several heap data structure applications in this post. In practice,
heaps are employed when retrieving and managing components according to their
priority effectively is necessary. Heaps are effective since they can obtain, add, and
delete elements quicker than a linear search in O(log n) time instead of O(n) time.
Moreover, heaps may be easily implemented and used for various algorithms and
data structures.
FAQs
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
What are the types of heaps?
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.