C++ 跟踪c+中结构数组中的位置+;
对于这个任务,我使用堆来构建优先级队列,从最低优先级到最高优先级排序。我创建了一个结构,它有两个成员:值和优先级。为了正确定位每个节点的子节点,我需要跟踪其位置。我最初在结构中有第三个元素location,它被设置为heap_size,因为节点被添加到数组的末尾C++ 跟踪c+中结构数组中的位置+;,c++,arrays,C++,Arrays,对于这个任务,我使用堆来构建优先级队列,从最低优先级到最高优先级排序。我创建了一个结构,它有两个成员:值和优先级。为了正确定位每个节点的子节点,我需要跟踪其位置。我最初在结构中有第三个元素location,它被设置为heap_size,因为节点被添加到数组的末尾 heaptype Heap::getleft (heaptype *A, heaptype& node) { heaptype lchild; lchild.loc
heaptype Heap::getleft (heaptype *A, heaptype& node)
{
heaptype lchild;
lchild.location = 2*node.location;
for(int i =0 ; i <heapsize; i++)
{
if(A[i].location == lchild.location)
{
lchild=A[i];
}
return lchild;
}
void Heap::heapifyup(heaptype *A, heaptype &node)
{
heaptype min;
heaptype r = getright(A, node);
heaptype l = getleft(A, node);
if(l.location<=heapsize && l.priority<node.priority)
{
min = l;
}
else
{
min = node;
}
if(r.location<=heapsize && r.priority<l.priority)
{
min = r;
}
if(min.priority!=node.priority)
{
swap(min, node);
heapifyup(A, min);
}
}
void Heap::swap (heaptype& a, heaptype& b)
{
heaptype temp;
temp.id = a.id;
a.id = b.id;
b.id = temp.id;
temp.priority = a.priority;
a.priority = b.priority;
b.priority = temp.priority;
}
heaptype Heap::getleft(heaptype*A,heaptype&node)
{
健康型lchild;
lchild.location=2*node.location;
对于(int i=0;i),如果这是家庭作业,你可能会这么说,因为否则答案可能是“使用”。噢,谢谢,这是作业。我编辑了它。等等……在节点中维护位置字段,或者在不弄乱其值和优先级字段的情况下交换数组中的两个节点有问题吗?