Exemple #1
0
        public void Insert(BinaryHeapElement e)
        {
            if (Has(e))
            {
                long position = (0x7FFFFFFF & e.binary_heap_index);
                Debug.Assert(position < next_position);
                if (position > 0 && e.LessThanForHeap(heap[(position - 1) / 2]))
                {
                    HeapifyUp(position);
                }
                else
                {
                    HeapifyDown(position);
                }
            }
            else
            {
                if (next_position == max_size)
                {
                    throw new BinaryHeapException("The Binary Heap has exceeded the available space.");
                }
                e.binary_heap_index = 0x80000000 | next_position;
                heap[next_position] = e;

                HeapifyUp(next_position++);
            }
        }
Exemple #2
0
 public BinaryHeapElement Pop()
 {
     if(next_position == 0) throw new BinaryHeapException("The Binary Heap is empty.");
     BinaryHeapElement min = heap[0];
     Debug.Assert((0x7FFFFFFF &heap[0].binary_heap_index) == 0);
     heap[0] = heap[--next_position];
     HeapifyDown(0);
     min.binary_heap_index = 0;
     return min;
 }
Exemple #3
0
        public void Delete(BinaryHeapElement e)
        {
            if (!Has(e))
            {
                throw new BinaryHeapException("The element could not be deleted because it is not in the heap.");
            }
            long position = (0x7FFFFFFF & e.binary_heap_index);

            e.binary_heap_index = 0;
            if(--next_position != position)
            {
                heap[position] = heap[next_position];
                heap[position].binary_heap_index = 0x80000000 | position;
                if(position > 0 && heap[position].LessThanForHeap(heap[(position - 1)/2]))
                    HeapifyUp(position);
                else
                    HeapifyDown(position);
            }
        }
Exemple #4
0
 public bool Has(BinaryHeapElement e)
 {
     return (e.binary_heap_index & 0x80000000) != 0;
 }
Exemple #5
0
 public override bool LessThanForHeap(BinaryHeapElement e)
 {
     return(value < ((MyInt)e).value);
 }
Exemple #6
0
 public abstract bool LessThanForHeap(BinaryHeapElement e);