Beispiel #1
0
        private void Swap(int i, int j)
        {
            HeapNode temp = array[i];

            array[i] = array[j];
            array[j] = temp;
            array[i].SetIndexInHeap(i);
            array[j].SetIndexInHeap(j);
        }
Beispiel #2
0
        public void Push(HeapNode node)
        {
            if (size == array.Length)
            {
                throw new Exception();
            }
            size++;
            int i = size - 1;

            array[i] = node;
            array[i].SetKey(node.GetKey());
            array[i].SetIndexInHeap(i);
            ShiftUp(i);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            float[] keys = new float[10] {
                8, 9, 19, 23, 1, 2, 44, 15, 8, 9
            };
            HeapNode[] nodes = new HeapNode[keys.Length];
            for (int i = 0; i < keys.Length; i++)
            {
                nodes[i] = new HeapNode(keys[i]);
            }
            RAHeap heap = new RAHeap(10);

            for (int i = 0; i < keys.Length; i++)
            {
                heap.Push(nodes[i]);
            }
            Console.WriteLine(heap.ToString());
            heap.Update(nodes[5], 55);
            Console.WriteLine(heap.ToString());
        }
Beispiel #4
0
        public void Delete(HeapNode node)
        {
            int i = node.GetIndexInHeap();

            if (i < 0 || i > size)
            {
                throw new Exception();
            }
            Swap(i, size - 1);
            size--;
            array[size].SetIndexInHeap(-1);
            if (array[i].GetKey() < array[size].GetKey())
            {
                ShiftDown(i);
            }
            else
            {
                ShiftUp(i);
            }
        }
Beispiel #5
0
        public void Update(HeapNode node, float newKeyValue)
        {
            int pos = node.GetIndexInHeap();

            if (pos >= size || pos < 0)
            {
                throw new Exception();
            }
            float oldkey = array[pos].GetKey();

            array[pos].SetKey(newKeyValue);
            if (newKeyValue < oldkey)
            {
                ShiftDown(pos);
            }
            else
            {
                ShiftUp(pos);
            }
        }