Heap 🌺
Binary Tree
Types of binary tree
1. Full Binary Tree
A full Binary tree is a special type of binary tree in which every parent node/internal node has either two or no children.
To learn more, please visit full binary tree.
2. Perfect Binary Tree
A perfect binary tree is a type of binary tree in which** every internal node has exactly two child nodes and all the leaf nodes are at the same level.**
Perfect Binary Tree
To learn more, please visit perfect binary tree.
3. Complete Binary Tree
A complete binary tree is just like a full binary tree, but with two major differences
- Every level must be completely filled
- All the leaf elements must lean towards the left.
- The last leaf element might not have a right sibling i.e. a complete binary tree doesn’t have to be a full binary tree.
Complete Binary Tree
To learn more, please visit complete binary tree.
4. Degenerate or Pathological Tree
A degenerate or pathological tree is the tree having a single child either left or right .
5. Skewed Binary Tree
A skewed binary tree is a pathological/degenerate tree in which the tree is either dominated by the left nodes or the right nodes. Thus, there are two types of skewed binary tree: left-skewed binary tree and right-skewed binary tree.
Skewed Binary Tree
6. Balanced Binary Tree
It is a type of binary tree in which the difference between the height of the left and the right subtree for each node is either 0 or 1.
Balanced Binary Tree
To learn more, please visit balanced binary tree.
Complete binary tree
Max heap / Min heap
Heap data structure is a complete binary tree that satisfies the heap property, where any given node is
- always greater than its child node/s and the key of the root node is the largest among all other nodes. This property is also called max heap property.
- always smaller than the child node/s and the key of the root node is the smallest among all other nodes. This property is also called min heap property.
Max-heap↓
Min-heap↓
This type of data structure is also called a binary heap.
Heap operation
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.
Let the input array be
Create a complete binary tree from the array
Start from the first index of non-leaf node whose index is given by n/2 - 1.
leaf node
Set current element
i
aslargest
.The index of left child is given by
2i + 1
and the right child is given by2i + 2
.
IfleftChild
is greater thancurrentElement
(i.e. element atith
index), setleftChildIndex
as largest.
IfrightChild
is greater than element inlargest
, setrightChildIndex
aslargest
.Swap largest with currentElement
Repeat steps 3-7 until the subtrees are also heapified.
Algorithm
1 | Heapify(array, size, i) |
To create a Max-Heap:
1 | MaxHeap(array, size) |
For Min-Heap, both leftChild
and rightChild
must be larger than the parent for all nodes.
Insert Element into Heap
Algorithm for insertion in Max Heap
1 | If there is no node, |
Insert the new element at the end of the tree.
Heapify the tree.
For Min Heap, the above algorithm is modified so that parentNode
is always smaller than newNode
.
Delete Element from Heap
Algorithm for deletion in Max Heap
1 | If nodeToBeDeleted is the leafNode |
Select the element to be deleted.
Swap it with the last element.
Remove the last element.
Heapify the tree.
For Min Heap, above algorithm is modified so that both childNodes
are greater smaller than currentNode
.
Java
1 | // Max-Heap data structure in Java |
MaxHeapify VS. BuildMaxHeap
Basically, heapify
is an algorithm used to re-arrange the heap if the root node violates the heap property (child subtrees must be heaps!). It’s a vital part of building a heap, inserting or deleting a top node from the heap.
Heapify is:
- after we popped the top node of the heap and we moved the last node to the top then we rearrange the tree from top-to-bottom so it is a heap again (we heapify)
- heapify time complexity O(log n)
Heapify is not:
creating a heap from an array which is a bottom-up operation with a time complexity of O(n)
even though heapify
is actively used for building a heap, we cannot say that building a heap is heapify
. It’s just an essential part of the process.