Exemple #1
0
        private void percolateDown(int index)
        {
            int            largerChild;
            BinaryHeapNode top = array[index];

            while (index < currentSize / 2)
            {
                int leftChild  = 2 * index + 1;
                int rightChild = leftChild + 1;
                if (rightChild < currentSize && array[leftChild].getValue() > array[rightChild].getValue())
                {
                    largerChild = rightChild;
                }
                else
                {
                    largerChild = leftChild;
                }
                if (top.getValue() <= array[largerChild].getValue())
                {
                    break;
                }
                array[index] = array[largerChild];
                index        = largerChild;
            }
            array[index] = top;
        }
Exemple #2
0
        public int remove()
        {
            BinaryHeapNode root = array[0];

            array[0] = array[--currentSize];
            percolateDown(0);
            return(root.getValue());
        }
Exemple #3
0
 public bool add(int value)
 {
     if (currentSize == maxSize)
     {
         return(false);
     }
     array[currentSize] = new BinaryHeapNode(value);
     percolateUp(currentSize++);
     return(true);
 }
Exemple #4
0
        public void percolateUp(int index)
        {
            int            parent = (index - 1) / 2;
            BinaryHeapNode bottom = array[index];

            while (index > 0 && array[parent].getValue() > bottom.getValue())
            {
                array[index] = array[parent];
                index        = parent;
                parent       = (parent - 1) / 2;
            }
            array[index] = bottom;
        }
Exemple #5
0
 public void addFreely(int value)
 {
     array[currentSize] = new BinaryHeapNode(value);
     currentSize++;
 }