当我插入元素时,C++ 优先级队列崩溃

标签 c++ crash nodes priority-queue

我通过最小堆组成了一个优先级队列。 这是指向类 Node 的指针的 PriorityQueue

当我尝试通过 vector 创建一个 PriorityQueue 对象时,它效果很好。问题是通过Insert方法单独插入指向Node的指针。它也可以工作并打印优先级队列,但有时它会在执行结束时崩溃!它返回一个错误尽管效果很好

输出:

a 1
b 2
c 3


Process returned -1073741819 (0xC0000005)   execution time : 3.000 s
Press any key to continue.

主要:

int main()
{
    NODE a = new Node('a',1);
    NODE b = new Node('b',2);
    NODE c = new Node('c',3);

    Q.Insert(a);
    Q.Insert(b);
    Q.Insert(c);

    Q.Print();

    return 0;
}

节点代码:

typedef class Node *NODE;

class Node {
private:
    unsigned char Ch;
    int Key;
    NODE L;
    NODE R;
public:
    Node() { L = NULL; R = NULL; };
    Node(int, unsigned char, NODE, NODE);
    Node(unsigned char, int);
    ~Node() { delete L; delete R; };
    NODE Left();
    NODE Right();
    int GetKey();
    unsigned char GetChar();
    void SetKey(int);
    void SetChar(unsigned char);
};

Node::Node(unsigned char c, int k)
{
    Ch = c; Key = k; R = NULL; L = NULL;
}

NODE Node::Left()
{
    return L;
}

NODE Node::Right()
{
    return R;
}

unsigned char Node::GetChar()
{
    return Ch;
}

int Node::GetKey()
{
    return Key;
}

void Node::SetKey(int k)
{
    Key = k;
}

PriorityQueue代码:

class PriorityQueue {
private:
    vector<NODE> A;
    int Heap_Size;
    int Parent(int);
    int Left(int);
    int Right(int);
    void Swap(NODE &, NODE &);
    void MinHeapify(int);
public:
    PriorityQueue();
    PriorityQueue(vector<NODE>);
    ~PriorityQueue() {};
    NODE Minimum();
    NODE ExtractMin();
    void DecreaseKey(int, int);
    void Insert(NODE);
    bool IsEmpty();
    void Print();
};

PriorityQueue::PriorityQueue()
{
    // I need to push back an empty node to use the vector from the index 1.
    // This is important to move in the min-heap trough the indices.
    NODE Default = new Node;
    A.push_back(Default);

    Heap_Size = 0;
}

PriorityQueue::PriorityQueue(vector<NODE> vett)
{
    A = vett; Heap_Size = A.size()-1;

    for (int i=Heap_Size/2; i>=1; i--)
    {
        MinHeapify(i);
    }
}

void PriorityQueue::Swap(NODE &a, NODE &b)
{
    NODE temp = a;
    a = b;
    b = temp;
}

void PriorityQueue::DecreaseKey(int i, int key)
{
    if (key > A[i]->GetKey())
    {
        cout << "How can I decrease the key?" << endl;
        return;
    }

    A[i]->SetKey(key);

    while (i>1 && A[Parent(i)]->GetKey() > A[i]->GetKey())
    {
        Swap(A[i],A[Parent(i)]);
        i = Parent(i);
    }
}

void PriorityQueue::Insert(NODE Nodo)
{
    Heap_Size++;
    A[Heap_Size] = Nodo;
    DecreaseKey(Heap_Size,Nodo->GetKey());
}

void PriorityQueue::Print()
{
    for (int i=1; i<=Heap_Size; i++)
        cout << A[i]->GetChar() << " " << A[i]->GetKey() << endl;
}

非常感谢!!!!

最佳答案

我解决了!问题是:

Heap_Size++;
A[Heap_Size] = Nodo;

A 是一个 vector ,所以我必须以这种方式编辑:

A.push_back(Nodo);

关于当我插入元素时,C++ 优先级队列崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21478161/

相关文章:

c++ - 列出目录 C++ 中的文件

firebase - 在物理设备上测试时,应用崩溃

python - Networkx 可以从不同文件中读取节点和边吗?

c++ - 图的节点类(C++)

c++ - 整数变量如何存储对整数的引用?

c++ - 适用于 Windows 'rm' 的 Git-Bash 如何工作?

c++ - 抛出异常后的 LHS 状态

crash - 如何在 jvm GCTaskThread 中调试 SIGSEGV

c++ - 程序仅在调试器外的 Release模式下崩溃

c - 无法破译我老师的伪代码