堆pqueue中的快速合并功能 - >指针?

时间:2013-04-09 11:56:32

标签: c++

我正在尝试在优先级队列的堆实现中执行快速合并功能。我加倍了他的一个“堆”数组(字符串),开始。我是否正确地认为“string first = one-> heap [count]”会给我一个名为“first”的字符串,它是数组“heap”中int count的字符串(heappqueue类中的私有成员数组) ?我得到了一些奇怪的结果......

HeapPQueue *HeapPQueue::merge(HeapPQueue *one, HeapPQueue *two) {
    one->expandCapacity();// Double heappqueue one's heap capacity
    int size2 = two->logSize;
    int count = 1;
        for (int i = size2 + 1; i <= size2 * 2; i++) {// concatinate heaps by copying heappqueue one's heap again
            string first = two->heap[count];
            one->heap[i] = first;
            count++;
            one->logSize++;// increase heapqueue one's logSize as it copies
    }
    int index = one->logSize;
    one->reverseHeapify(index);// call to private member function on heappqueue one

    return one;
}

void HeapPQueue::reverseHeapify(int index) {
    if (index == 1) return; // base case
    string this1 = heap[index];
    string node1 = heap[index / 2];
    if (node1 > this1) {
        heap[index / 2] = this1;
        heap[index] = node1;
    }

    reverseHeapify(index - 1);

}

0 个答案:

没有答案