Exemple #1
0
        /// <summary>
        /// Inserts an object into the heap
        /// </summary>
        /// <param name="node">The node to be inserted</param>
        /// <param name="priority">The priority of the node</param>
        public void Insert(T node, float priority)
        {
            if (node == null)
            {
                return;
            }

            if (HeapContainsNode(node))
            {
                return;
            }

            if (size + 1 == heap.Length)
            {
                DoubleArray();
            }

            PriorityNode <T> n = new PriorityNode <T>(node, priority);

            int hole = ++size;

            heap[0] = n;

            for (; n.priority < heap[hole / 2].priority && hole > 1; hole /= 2)
            {
                heap[hole] = heap[hole / 2];
            }

            heap[hole] = n;
        }
Exemple #2
0
        /// <summary>
        /// Doubles the current heap
        /// </summary>
        private void DoubleArray()
        {
            PriorityNode <T>[] temp = new PriorityNode <T> [heap.Length * 2];
            for (int i = 1; i < heap.Length; i++)
            {
                temp[i] = heap[i];
            }

            heap = temp;
        }