Heap Sort
Heap Sort
A complete binary tree is a special binary tree in which every level, except possibly the last, is
filled. All the nodes are as far left as possible
Heap data structure is a complete binary tree that satisfies the heap property. It is also called
as a binary heap. Generally, Heaps can be of two types:
Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys
present at all of it’s children. The same property must be recursively true for all sub-trees in
that Binary Tree.
Min-Heap: In a Min-Heap the key present at the root node must be minimum among the keys
present at all of it’s children. The same property must be recursively true for all sub-trees in
that Binary Tree.
Heap Property is the property of a node in which (for max heap) key of each node is always
greater than its child node/s and the key of the root node is the largest among all other
nodes;(for min heap) key of each node is always smaller than the child node/s and the key of
the root node is the smallest among all other nodes.
Heap
• For Input → 35 33 42 10 14 19 27 44 26 31
• 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.
52
7,10,15,45,50, 55,70
• Node : kth position array
• Left child: 2*k (if it starts from 0 : 2*k+1)
• Right child: 2*k+1 (: 2*k+2)
55 50 45 15 10 7
• Heap Operations
• Some of the important operations performed
on a heap are described below along with their
algorithms.
• Heapify
• Heapify is the process of creating a heap data
structure from a binary tree. It is used to create
a Min-Heap or a Max-Heap.
• Insert the new element at the end of the tree.