Prerequisite - Binary Heap
K-ary heaps are a generalization of binary heap(K=2) in which each node have K children instead of 2. Just like binary heap, it follows two properties:
- Nearly complete binary tree, with all levels having maximum number of nodes except the last, which is filled in left to right manner.
- Like Binary Heap, it can be divided into two categories:
- Max k-ary heap (key at root is greater than all descendants and same is recursively true for all nodes).
- Min k-ary heap (key at root is lesser than all descendants and same is recursively true for all nodes)
Examples:
3-ary max heap - root node is maximum
of all nodes
10
/ | \
7 9 8
/ | \ /
4 6 5 7
3-ary min heap -root node is minimum
of all nodes
10
/ | \
12 11 13
/ | \
14 15 18
The height of a complete k-ary tree with n-nodes is given by logkn.
Applications of K-ary Heap:
- K-ary heap when used in the implementation of priority queue allows faster decrease key operation as compared to binary heap ( O(log2n)) for binary heap vs O(logkn) for K-ary heap). Nevertheless, it causes the complexity of extractMin() operation to increase to O(k log kn) as compared to the complexity of O(log2n) when using binary heaps for priority queue. This allows K-ary heap to be more efficient in algorithms where decrease priority operations are more common than extractMin() operation.Example: Dijkstra's algorithm for single source shortest path and Prim's algorithm for minimum spanning tree
- K-ary heap has better memory cache behaviour than a binary heap which allows them to run more quickly in practice, although it has a larger worst case running time of both extractMin() and delete() operation (both being O(k log kn) ).
Implementation:
Assuming 0 based indexing of array, an array represents a K-ary heap such that for any node we consider:
- Parent of the node at index i (except root node) is located at index (i-1)/k
- Children of the node at index i are at indices (k*i)+1 , (k*i)+2 .... (k*i)+k
- The last non-leaf node of a heap of size n is located at index (n-2)/k
buildHeap() : Builds a heap from an input array.
This function runs a loop starting from the last non-leaf node all the way upto the root node, calling a function restoreDown(also known as maHeapify) for each index that restores the passed index at the correct position of the heap by shifting the node down in the K-ary heap building it in a bottom up manner.
Why do we start the loop from the last non-leaf node ?
Because all the nodes after that are leaf nodes which will trivially satisfy the heap property as they don't have any children and hence, are already roots of a K-ary max heap.
restoreDown() (or maxHeapify) : Used to maintain heap property.
It runs a loop where it finds the maximum of all the node's children, compares it with its own value and swaps if the max(value of all children) > (value at node). It repeats this step until the node is restored into its original position in the heap.
extractMax() : Extracting the root node.
A k-ary max heap stores the largest element in its root. It returns the root node, copies last node to the first, calls restore down on the first node thus maintaining the heap property.
insert() : Inserting a node into the heap
This can be achieved by inserting the node at the last position and calling restoreUp() on the given index to restore the node at its proper position in the heap. restoreUp() iteratively compares a given node with its parent, since in a max heap the parent is always greater than or equal to its children nodes, the node is swapped with its parent only when its key is greater than the parent.
Combining the above, following is the implementation of K-ary heap.
CPP
// C++ program to demonstrate all operations of
// k-ary Heap
#include<bits/stdc++.h>
using namespace std;
// function to heapify (or restore the max- heap
// property). This is used to build a k-ary heap
// and in extractMin()
// att[] -- Array that stores heap
// len -- Size of array
// index -- index of element to be restored
// (or heapified)
void restoreDown(int arr[], int len, int index,
int k)
{
// child array to store indexes of all
// the children of given node
int child[k+1];
while (1)
{
// child[i]=-1 if the node is a leaf
// children (no children)
for (int i=1; i<=k; i++)
child[i] = ((k*index + i) < len) ?
(k*index + i) : -1;
// max_child stores the maximum child and
// max_child_index holds its index
int max_child = -1, max_child_index ;
// loop to find the maximum of all
// the children of a given node
for (int i=1; i<=k; i++)
{
if (child[i] != -1 &&
arr[child[i]] > max_child)
{
max_child_index = child[i];
max_child = arr[child[i]];
}
}
// leaf node
if (max_child == -1)
break;
// swap only if the key of max_child_index
// is greater than the key of node
if (arr[index] < arr[max_child_index])
swap(arr[index], arr[max_child_index]);
index = max_child_index;
}
}
// Restores a given node up in the heap. This is used
// in decreaseKey() and insert()
void restoreUp(int arr[], int index, int k)
{
// parent stores the index of the parent variable
// of the node
int parent = (index-1)/k;
// Loop should only run till root node in case the
// element inserted is the maximum restore up will
// send it to the root node
while (parent>=0)
{
if (arr[index] > arr[parent])
{
swap(arr[index], arr[parent]);
index = parent;
parent = (index -1)/k;
}
// node has been restored at the correct position
else
break;
}
}
// Function to build a heap of arr[0..n-1] and value of k.
void buildHeap(int arr[], int n, int k)
{
// Heapify all internal nodes starting from last
// non-leaf node all the way upto the root node
// and calling restore down on each
for (int i= (n-1)/k; i>=0; i--)
restoreDown(arr, n, i, k);
}
// Function to insert a value in a heap. Parameters are
// the array, size of heap, value k and the element to
// be inserted
void insert(int arr[], int* n, int k, int elem)
{
// Put the new element in the last position
arr[*n] = elem;
// Increase heap size by 1
*n = *n+1;
// Call restoreUp on the last index
restoreUp(arr, *n-1, k);
}
// Function that returns the key of root node of
// the heap and then restores the heap property
// of the remaining nodes
int extractMax(int arr[], int* n, int k)
{
// Stores the key of root node to be returned
int max = arr[0];
// Copy the last node's key to the root node
arr[0] = arr[*n-1];
// Decrease heap size by 1
*n = *n-1;
// Call restoreDown on the root node to restore
// it to the correct position in the heap
restoreDown(arr, *n, 0, k);
return max;
}
// Driver program
int main()
{
const int capacity = 100;
int arr[capacity] = {4, 5, 6, 7, 8, 9, 10};
int n = 7;
int k = 3;
buildHeap(arr, n, k);
printf("Built Heap : \n");
for (int i=0; i<n; i++)
printf("%d ", arr[i]);
int element = 3;
insert(arr, &n, k, element);
printf("\n\nHeap after insertion of %d: \n",
element);
for (int i=0; i<n; i++)
printf("%d ", arr[i]);
printf("\n\nExtracted max is %d",
extractMax(arr, &n, k));
printf("\n\nHeap after extract max: \n");
for (int i=0; i<n; i++)
printf("%d ", arr[i]);
return 0;
}
Java
public class KaryHeap {
public static void main(String[] args) {
final int capacity = 100;
int[] arr = new int[capacity];
arr[0] = 4;
arr[1] = 5;
arr[2] = 6;
arr[3] = 7;
arr[4] = 8;
arr[5] = 9;
arr[6] = 10;
int n = 7;
int k = 3;
buildHeap(arr, n, k);
System.out.println("Built Heap: ");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
int element = 3;
insert(arr, n, k, element);
n++;
System.out.println("\n\nHeap after insertion of " + element + ": ");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
System.out.println("\n\nExtracted max is " + extractMax(arr, n, k));
n--;
System.out.println("\n\nHeap after extract max: ");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
public static void buildHeap(int[] arr, int n, int k) {
for (int i = (n - 1) / k; i >= 0; i--)
restoreDown(arr, n, i, k);
}
public static void insert(int[] arr, int n, int k, int elem) {
arr[n - 1] = elem;
restoreUp(arr, n - 1, k);
}
public static int extractMax(int[] arr, int n, int k) {
int max = arr[0];
arr[0] = arr[n - 1];
restoreDown(arr, n - 1, 0, k);
return max;
}
public static void restoreDown(int[] arr, int len, int index, int k) {
int[] child = new int[k + 1];
while (true) {
for (int i = 1; i <= k; i++)
child[i] = (k * index + i) < len ? (k * index + i) : -1;
int maxChild = -1, maxChildIndex = 0;
for (int i = 1; i <= k; i++) {
if (child[i] != -1 && arr[child[i]] > maxChild) {
maxChildIndex = child[i];
maxChild = arr[child[i]];
}
}
if (maxChild == -1)
break;
if (arr[index] < arr[maxChildIndex])
swap(arr, index, maxChildIndex);
index = maxChildIndex;
}
}
public static void restoreUp(int[] arr, int index, int k) {
int parent = (index - 1) / k;
while (parent >= 0) {
if (arr[index] > arr[parent]) {
swap(arr, index, parent);
index = parent;
parent = (index - 1) / k;
} else
break;
}
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
Python
def restore_down(arr, length, index, k):
child = [0] * (k + 1)
while True:
for i in range(1, k + 1):
child[i] = k * index + i if k * index + i < length else -1
max_child, max_child_index = -1, 0
for i in range(1, k + 1):
if child[i] != -1 and arr[child[i]] > max_child:
max_child_index = child[i]
max_child = arr[child[i]]
if max_child == -1:
break
if arr[index] < arr[max_child_index]:
arr[index], arr[max_child_index] = arr[max_child_index], arr[index]
index = max_child_index
def restore_up(arr, index, k):
parent = (index - 1) // k
while parent >= 0:
if arr[index] > arr[parent]:
arr[index], arr[parent] = arr[parent], arr[index]
index = parent
parent = (index - 1) // k
else:
break
def build_heap(arr, n, k):
for i in range((n - 1) // k, -1, -1):
restore_down(arr, n, i, k)
def insert(arr, n, k, elem):
arr.append(elem)
n += 1
restore_up(arr, n - 1, k)
def extract_max(arr, n, k):
max_elem = arr[0]
arr[0] = arr[n - 1]
n -= 1
restore_down(arr, n, 0, k)
return max_elem
arr = [4, 5, 6, 7, 8, 9, 10]
n = 7
k = 3
build_heap(arr, n, k)
print("Built Heap:")
print(arr[:n])
elem = 3
insert(arr, n, k, elem)
print("\nHeap after insertion of", elem, ":")
print(arr[:n])
max_elem = extract_max(arr, n, k)
print("\nExtracted max is", max_elem)
print("Heap after extract max:")
print(arr[:n])
C#
using System;
class KaryHeap
{
static void restoreDown(int[] arr, int len, int index, int k)
{
int[] child = new int[k + 1];
while (true)
{
for (int i = 1; i <= k; i++)
child[i] = (k * index + i) < len ? (k * index + i) : -1;
int maxChild = -1, maxChildIndex = 0;
for (int i = 1; i <= k; i++)
{
if (child[i] != -1 && arr[child[i]] > maxChild)
{
maxChildIndex = child[i];
maxChild = arr[child[i]];
}
}
if (maxChild == -1)
break;
if (arr[index] < arr[maxChildIndex])
swap(ref arr[index], ref arr[maxChildIndex]);
index = maxChildIndex;
}
}
static void restoreUp(int[] arr, int index, int k)
{
int parent = (index - 1) / k;
while (parent >= 0)
{
if (arr[index] > arr[parent])
{
swap(ref arr[index], ref arr[parent]);
index = parent;
parent = (index - 1) / k;
}
else
break;
}
}
static void buildHeap(int[] arr, int n, int k)
{
for (int i = (n - 1) / k; i >= 0; i--)
restoreDown(arr, n, i, k);
}
static void insert(int[] arr, ref int n, int k, int elem)
{
arr[n] = elem;
n++;
restoreUp(arr, n - 1, k);
}
static int extractMax(int[] arr, ref int n, int k)
{
int max = arr[0];
arr[0] = arr[n - 1];
n--;
restoreDown(arr, n, 0, k);
return max;
}
static void Main(string[] args)
{
const int capacity = 100;
int[] arr = new int[capacity] { 4, 5, 6, 7, 8, 9, 10 };
int n = 7;
int k = 3;
buildHeap(arr, n, k);
Console.WriteLine("Built Heap : ");
for (int i = 0; i < n; i++)
Console.Write("{0} ", arr[i]);
int element = 3;
insert(arr, ref n, k, element);
Console.WriteLine("\n\nHeap after insertion of {0}: ", element);
for (int i = 0; i < n; i++)
Console.Write("{0} ", arr[i]);
Console.WriteLine("\n\nExtracted max is {0}", extractMax(arr, ref n, k));
Console.WriteLine("\n\nHeap after extract max: ");
for (int i = 0; i < n; i++)
Console.Write("{0} ", arr[i]);
Console.ReadLine();
}
static void swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
}
JavaScript
// Function to restore the max-heap property by moving the element down the heap
function restoreDown(arr, len, index, k) {
// child array to store indexes of all the children of the given node
let child = Array(k + 1).fill(-1);
while (true) {
// Initialize child array with indexes of the children
for (let i = 1; i <= k; i++) {
child[i] = k * index + i < len ? k * index + i : -1;
}
let maxChild = -1;
let maxChildIndex;
// Find the maximum child among all the children
for (let i = 1; i <= k; i++) {
if (child[i] !== -1 && arr[child[i]] > maxChild) {
maxChildIndex = child[i];
maxChild = arr[child[i]];
}
}
// If the node is a leaf node, break the loop
if (maxChild === -1) {
break;
}
// Swap only if the key of maxChild is greater than the key of the node
if (arr[index] < arr[maxChildIndex]) {
[arr[index], arr[maxChildIndex]] = [arr[maxChildIndex], arr[index]];
}
// Move to the next level of the heap
index = maxChildIndex;
}
}
// Function to restore a given node up in the heap
function restoreUp(arr, index, k) {
let parent = Math.floor((index - 1) / k);
// Move up the heap until the heap property is restored
while (parent >= 0) {
if (arr[index] > arr[parent]) {
// Swap if the key of the current node is greater than the key of its parent
[arr[index], arr[parent]] = [arr[parent], arr[index]];
index = parent;
parent = Math.floor((index - 1) / k);
} else {
// Node has been restored at the correct position
break;
}
}
}
// Function to build a heap from an array
function buildHeap(arr, n, k) {
// Heapify all internal nodes starting from the last non-leaf node up to the root node
for (let i = Math.floor((n - 1) / k); i >= 0; i--) {
restoreDown(arr, n, i, k);
}
}
// Function to insert a value into the heap
function insert(arr, n, k, elem) {
// Put the new element in the last position
arr[n[0]] = elem;
// Increase heap size by 1
n[0]++;
// Restore the heap property by moving the element up
restoreUp(arr, n[0] - 1, k);
}
// Function to extract the maximum value from the heap
function extractMax(arr, n, k) {
// Store the key of the root node to be returned
let max = arr[0];
// Copy the last node's key to the root node
arr[0] = arr[n[0] - 1];
// Decrease heap size by 1
n[0]--;
// Restore the heap property by moving the root node down
restoreDown(arr, n[0], 0, k);
return max;
}
// Driver program
const capacity = 100;
let arr = [4, 5, 6, 7, 8, 9, 10];
let n = [7]; // Using an array to simulate pass by reference for 'n'
const k = 3;
// Build the heap from the given array
buildHeap(arr, n[0], k);
console.log("Built Heap : ");
console.log(arr.slice(0, n[0]).join(" "));
const element = 3;
// Insert a new element into the heap
insert(arr, n, k, element);
console.log(`\nHeap after insertion of ${element}:`);
console.log(arr.slice(0, n[0]).join(" "));
// Extract the maximum value from the heap
console.log(`\nExtracted max is ${extractMax(arr, n, k)}`);
console.log("\nHeap after extracting max:");
console.log(arr.slice(0, n[0]).join(" "));
OutputBuilt Heap :
10 9 6 7 8 4 5
Heap after insertion of 3:
10 9 6 7 8 4 5 3
Extracted max is 10
Heap after extract max:
9 8 6 7 3 4 5
Time Complexity Analysis
- For a k-ary heap, with n nodes the maximum height of the given heap will be logkn. So restoreUp() run for maximum of logkn times (as at every iteration the node is shifted one level up is case of restoreUp() or one level down in case of restoreDown).
- restoreDown() calls itself recursively for k children. So time complexity of this functions is O(k logkn).
- Insert and decreaseKey() operations call restoreUp() once. So complexity is O(logkn).
- Since extractMax() calls restoreDown() once, its complexity O(k logkn)
- Time complexity of build heap is O(n) (Analysis is similar to binary heap)
Auxiliary Space: O(n). This is because we need to store all the elements of the heap in an array.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem