我有一個procudere刪除我的樹堆,並上線p=merge(l, merge(m, rs));
我有錯誤error: non-const lvalue reference to type 'nodeptr' (aka 'node *') cannot bind to a temporary of type 'nodeptr'
因此,這裏是執行的刪除和合並錯誤刪除樹堆
nodeptr treap::merge(nodeptr &l, nodeptr &r){
nodeptr result;
if (!l){
result=r;
}
else if(!r){
result=l;
}
else if(l->cnt > r->cnt){
l->right=merge(l->right, r);
result=l;
}
else{
r->left=merge(l, r->left);
result=r;
}
return result;
}
void treap::deletes(nodeptr &p, int x){
nodeptr l, r, m, rs;
split(p, x-1, l, r);
split(r, x, m, rs);
if (m){
if (!m->left){
m=m->right;
}
else{
m=m->left;
}
}
p=merge(l, merge(m, rs));
}
,我該如何實現樹堆的數據結構。
typedef struct node *nodeptr;
struct node{
int x;
long y;
node* left ;
node* right ;
int cnt;
node(int key=0, long prior=0): x(key), y(prior), left(NULL), right(NULL), cnt(0) {}
};
class treap{
public:
int cnt(nodeptr &p);
bool find(nodeptr &p, int x);
void update_cnt(nodeptr &p);
void split(nodeptr &p, int x, nodeptr &l, nodeptr &r);
void insert(nodeptr &p, nodeptr &q);
nodeptr merge(nodeptr &l, nodeptr &r);
void deletes(nodeptr &p, int x);
};
你能說我,爲什麼會有這個錯誤?我認爲一切都很好。對不起,如果問題是不好意思的話。提前致謝。
嗯,錯誤很明顯:你不能將非const引用綁定到臨時對象。該聲明的問題究竟在哪裏? – PlasmaHH
'treap :: merge'函數返回'nodeptr'類型的臨時對象。這個對象不能傳遞給'merge'函數,因爲它需要l值引用'nodeptr&'。 – Constructor