I am trying to construct a Max Heap and as each new value is inserted the value gets shifted up or down into its correct position, I have yet to implement a shift down function so as of right now I'm using a test that should only require the program to shift up. The test data is entered in the following order:
[16, 10, 14, 9, 7, 1, 4, 2, 8, 3]
I'm using the following code in the main class to insert the values in the heap:
package com.company;
public class Main {
public static void main(String[] args) {
BinaryHeap bh = new BinaryHeap();
bh.insert(16);
bh.insert(10);
bh.insert(14);
bh.insert(9);
bh.insert(7);
bh.insert(1);
bh.insert(4);
bh.insert(2);
bh.insert(8);
bh.insert(3);
bh.printHeap();
}
}
And this next bit of code is where the insertion and the shifting takes place:
package com.company;
public class BinaryHeap {
private int[] Heap;
private int size;
private int maxsize;
public BinaryHeap(){
this.maxsize = 10;
this.size = 0;
Heap = new int[this.maxsize + 1];
}
public int Parent(int i){
return (i)/2;
}
public int LeftChild(int i){
return (2*i);
}
public int RightChild(int i){
return ((2*1)+1);
}
public void insert(int value) {
if(size <= Heap.length) {
size++;
Heap[size] = value;
siftUp(size);
}
}
private void siftUp(int i) {
int parentIndex;
int tmp;
if (i != 0) {
parentIndex = Parent(i);
if (Heap[parentIndex] < Heap[i]) {
tmp = Heap[parentIndex];
Heap[parentIndex] = Heap[i];
Heap[i] = tmp;
siftUp(parentIndex);
}
}
}
public void printHeap()
{
for (int i = 1; i < maxsize; i++) {
System.out.print(" PARENT : " + Heap[Parent(i)]
+ " LEFT CHILD : " + Heap[LeftChild(i)]
+ " RIGHT CHILD :" + Heap[RightChild(i)]);
System.out.println();
}
}
}
The shifting function is siftUp() which I think is the issue. When the program is run with these outputs:
PARENT : 16 LEFT CHILD : 9 RIGHT CHILD :10
PARENT : 14 LEFT CHILD : 8 RIGHT CHILD :10
PARENT : 14 LEFT CHILD : 1 RIGHT CHILD :10
PARENT : 9 LEFT CHILD : 0 RIGHT CHILD :10
PARENT : 9 LEFT CHILD : 3 RIGHT CHILD :10
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 12 out of bounds for length 11
at com.company.BinaryHeap.printHeap(BinaryHeap.java:61)
at com.company.Main.main(Main.java:20)
But this isn't correct because 1) the index out of bounds exception at the end which is coming from the printHeap() function and 2)The Children of each parent node are not in the right place, because the root node should be 16 with 14 and 10 as children, also when it prints out the values for the heap, it prints off a 0 however 0 is never inserted into the heap. I've tried doing some of my own debugging but with not much success so any help with this is welcome.