import java.util.
ArrayList;
import java.util.NoSuchElementException;
public class PowerofTwoMaxHeap<T extends Comparable<T> > {
private final int branchingFactor;
private final ArrayList<T> heap;
public POwerofTwOMaxHeap(int branchExponent) {
if (branchExponent < | | branchExponent > 10) {
throw new IllegalArgumentException("branchExponent must be between 0 and 10");
this.branchingFactor = 1 <« branchExponernt; // 2branchExponent
this.heap = new ArrayListk>();
public void insert(T value) {
heap.add (value);
siftUp(heap. size () - 1);
public T popMax() {
if (heap.isEmpty()) {
throw new NoSuchElementException(" Heap is empty");
}
T maxValue = heap.get (0);
last = heap.remove (heap.size() - 1);
if (!heap.isEmpty()) {
heap.set (0, last);
siftDown(0);
}
return maxvalue;
public boolean isEmpty() {
rivate void swap(int i, int j) {
T temp = heap.get (i);
heap.set (i, heap.get(j));
heap. set(j, temp);
For debugging and testing
ublic void printHeap() {
System.out.println (heap);
ublic static void main(String[] args) {
I/ Testing with branchExponent = 1 (binary heap )
System. out.
PowerofTWOMaV Testing Binary Max Heap (branchExponent = 1):");
<Integer> binaryHeap = new PowerofTwoMaxHeap<>(1);
binaryHeap.insert(10);
binaryHeap.insert (40);
binaryHeap.insert(20);
binaryHeap.insert (60) ;
binaryHeap.insert(30) ;
binaryHeap.printHeap();
while (lbinaryHeap.isEmpty()) {
System.out . print (binaryHeap.popMax() +" ");
// Testing with Large bran chExponent = 4 (16 children)
System.out. println("\n\nTesting wide Max Heap (branchExponent = 4):");
PowerofTwoMaxHeap<Integer> wideHeap = new POwerofTwOMaxHeap<>(4);
for (int i: 0; i< 1000; i+) {
wideHeap.insert ((int) (Math. random() * 100o0) );
rivate void swap(int i, int j) {
T temp = heap.get (i);
heap.set (i, heap.get(j));
heap. set(j, temp);
For debugging and testing
ublic void printHeap() {
System.out.println (heap);
ublic static void main(String[] args) {
I/ Testing with branchExponent = 1 (binary heap )
System. out.
PowerofTWOMaV Testing Binary Max Heap (branchExponent = 1):");
<Integer> binaryHeap = new PowerofTwoMaxHeap<>(1);
binaryHeap.insert(10);
binaryHeap.insert (40);
binaryHeap.insert(20);
binaryHeap.insert (60) ;
binaryHeap.insert(30) ;
binaryHeap.printHeap();
while (lbinaryHeap.isEmpty()) {
System.out . print (binaryHeap.popMax() +" ");
// Testing with Large bran chExponent = 4 (16 children)
System.out. println("\n\nTesting wide Max Heap (branchExponent = 4):");
PowerofTwoMaxHeap<Integer> wideHeap = new POwerofTwOMaxHeap<>(4);
for (int i: 0; i< 1000; i+) {
wideHeap.insert ((int) (Math. random() * 100o0) );
public boolean isEmpty() {
return heap.isEmpty();
public int size() {
return heap.size();
}
private void siftUp(int index) {
while (index > 0) {
int parentIndex = (index - 1) / branchingFactor;
if (heap.get (index).compareTo(heap.get (parentIndex)) > 0) {
swap(index, parent Index) ;
index = parentIndex;
} else {
break;
private void siftDown(int index)
int size = heap.size();
while (true) {
int maxIndex = index;
for (int i= 1; i <= branchingFactor; i++) {
int childIndex = branchingFactor * index i;
if (childIndex < size && heap.get (childIndex).compareTo(heap.get (maxIndex)) > 0) {
maxIndex = childIndex;
}
1f (maxIndex I= index) {
Swap(index, maxIndex) ;
index = maxIndex;
} else {
break;
int prev = Integer.MAX_ VALUE;
while (!wideHeap. isEmpty () ) {
int current = wideHeap.popMax();
if (current > prev) {
throw new RuntimeException("Heap property violated!");
prev = Current;
System.out . println("Heap passed for large branchingFactor.");
Output:
Testing Binary Max Heap (branchExponent = 1):
[60, 40, 20, 10, 30]
60 40 30 20 10
Testing Wide Max Heap (branchExponent = 4):
Heap passed for large branchingFactor.