implement the add method for minheap public class Heap priv

implement the add() method for min-heap

public class Heap {
     private ArrayList<e> element;
     private comparator <e> comp;

    public int patent(int childIndex) {
          return (childIndex - 1) / 2;
     }

     public void add(E element) {

      code here

     }

}

Solution

public void add(int value) {

            if (heapSize == data.length)

                  throw newHeapException(\"Heap\'s underlying storage is overflow\");

            else {

                  heapSize++;

                  data[heapSize - 1] = value;

                  shiftUp(heapSize - 1);

            }

      }   

private void shiftUp(int nodeIndex) {

            int parentIndex, tmp;

            if (nodeIndex != 0) {

                  parentIndex = getParentIndex(nodeIndex);

                  if (data[parentIndex] >data[nodeIndex]) {

                        tmp =data[parentIndex];

                        data[parentIndex] = data[nodeIndex];

                        data[nodeIndex] = tmp;

shiftUp(parentIndex);

                  }

            }

      }

}

implement the add() method for min-heap public class Heap { private ArrayList<e> element; private comparator <e> comp; public int patent(int childIn
implement the add() method for min-heap public class Heap { private ArrayList<e> element; private comparator <e> comp; public int patent(int childIn

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site