コード例 #1
0
        public void Insert(T node, int 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;
        }
コード例 #2
0
        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;
        }