Search code examples
javainsertheapsortmax-heap

Inserting item into a Max Heap


I am not sure on how to insert an item into my max heap and then trickle up so the max heap property holds. I have thrown an exception if the heapArray is full therefore not being able to insert an item.

I am not using JCF classes or the priority Queue for this program. I have also thrown in my deleteMax method that deletes the max value of the heap and restores the heap so the max heap property holds.

public  class MaxIntHeap {
//my global var's
private int[] heapArray; // default size of 20 in constructor
private int lastOne; //equal to size of the array(heap)
private int size;
private int k;

public void heapInsert(int v) throws HeapException {
  if(lastOne == heapArray.length - 1 ){
  throw new HeapException("The value " + v + " cannot be inserted because the heap is FULL!");
  }
  else{ //This is where i am lost as to how i should insert
     lastOne++; //size of lastOne is increased.
}

This is my removeMax method.

public int  removeMax ()throws HeapException  { 
  if(size == 0){
     throw new HeapException("The Heap is empty, therefore the Max value of the heap cannot be       
     removed.");
  }
  else{
     int max = heapArray[0];
     heapArray[0] = heapArray[lastOne];
     lastOne--;
     k = 0;
     while(k > 0){
        int j = k / 2;
        if(heapArray[k] < heapArray[j]){
           swap(heapArray[k], heapArray[j]); //swap method...
           k = j;

        }
        else 
           break;
     }
     return max;
   }

  }

Any help would be greatly appreciated. Thank you!


Solution

  • Your class design looks ok. In a heap:

    leftChild = 2*parent +1
    rightChild = 2*parent + 2
    parent = (childindex-1)/2
    

    For a maxheap,

    1. Insert the element at last. Then compare it with its parent.

    2. If parent is greater than this latest insertion, you are good.

    3. Else swap parent and this child

    4. Repeat until you reach the root.

      MaxHeapImpl:

      public class MaxHeap {
      
      public int[] myHeap = new int[20];
      public int begin = 0;
      public int current = 0;
      
      public int getParent(int index){
          return (index - 1)/2;
      }
      
      public int getLeftChild(int index){
          return 2*index+1;
      }
      
      public int getRighChild(int index){
          return 2*index+2;
      }
      
      public void insert(int data) {
      
          myHeap[current] = data;
      
          int i = current;
          int tmp;
          int parent;
          while(i > 0){
              parent = getParent(i);
      
              System.out.println(" I value"+i+" parent"+parent+" data"+data);
              if(myHeap[parent] < myHeap[i]){
                  tmp = myHeap[parent];
                  myHeap[parent] = myHeap[i];
                  myHeap[i] = tmp;
              } else{
                  break;
              }
      
              i = parent;
      
          }
          current++;
      }
      

      }

    TestClass:

        public class MaxHeapTest {
    
        @Test
        public void test() {
            MaxHeap myHeap = new MaxHeap();
            
            myHeap.insert(40);
            myHeap.insert(20);
            myHeap.insert(10);
            myHeap.insert(25);
            myHeap.insert(30);
            myHeap.insert(100);
            
            for(int i = 0; i < myHeap.current;i++){
                System.out.println(" "+myHeap.myHeap[i]);
            }
        }
    
    }