0% found this document useful (0 votes)
4 views6 pages

Power of Two Max Heap Implementation

Uploaded by

Amisha Shahare
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Power of Two Max Heap Implementation

Uploaded by

Amisha Shahare
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

ArrayList;
import [Link];
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");
[Link] = 1 <« branchExponernt; // 2branchExponent
[Link] = new ArrayListk>();

public void insert(T value) {


[Link] (value);
siftUp(heap. size () - 1);

public T popMax() {
if ([Link]()) {
throw new NoSuchElementException(" Heap is empty");
}
T maxValue = [Link] (0);
last = [Link] ([Link]() - 1);
if (![Link]()) {
[Link] (0, last);
siftDown(0);
}
return maxvalue;

public boolean isEmpty() {


rivate void swap(int i, int j) {
T temp = [Link] (i);
[Link] (i, [Link](j));
heap. set(j, temp);

For debugging and testing


ublic void printHeap() {
[Link] (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);
[Link](10);
[Link] (40);
[Link](20);
[Link] (60) ;
[Link](30) ;
[Link]();
while ([Link]()) {
[Link] . print ([Link]() +" ");

// Testing with Large bran chExponent = 4 (16 children)


[Link]. println("\n\nTesting wide Max Heap (branchExponent = 4):");
PowerofTwoMaxHeap<Integer> wideHeap = new POwerofTwOMaxHeap<>(4);
for (int i: 0; i< 1000; i+) {
[Link] ((int) (Math. random() * 100o0) );
rivate void swap(int i, int j) {
T temp = [Link] (i);
[Link] (i, [Link](j));
heap. set(j, temp);

For debugging and testing


ublic void printHeap() {
[Link] (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);
[Link](10);
[Link] (40);
[Link](20);
[Link] (60) ;
[Link](30) ;
[Link]();
while ([Link]()) {
[Link] . print ([Link]() +" ");

// Testing with Large bran chExponent = 4 (16 children)


[Link]. println("\n\nTesting wide Max Heap (branchExponent = 4):");
PowerofTwoMaxHeap<Integer> wideHeap = new POwerofTwOMaxHeap<>(4);
for (int i: 0; i< 1000; i+) {
[Link] ((int) (Math. random() * 100o0) );
public boolean isEmpty() {
return [Link]();

public int size() {


return [Link]();
}
private void siftUp(int index) {
while (index > 0) {
int parentIndex = (index - 1) / branchingFactor;
if ([Link] (index).compareTo([Link] (parentIndex)) > 0) {
swap(index, parent Index) ;
index = parentIndex;
} else {
break;

private void siftDown(int index)


int size = [Link]();
while (true) {
int maxIndex = index;

for (int i= 1; i <= branchingFactor; i++) {


int childIndex = branchingFactor * index i;
if (childIndex < size && [Link] (childIndex).compareTo([Link] (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 = [Link]();
if (current > prev) {
throw new RuntimeException("Heap property violated!");
prev = Current;

[Link] . 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.

You might also like