i have a procudere deletes for my treap, and on the line p=merge(l, merge(m, rs));
i have error error: non-const lvalue reference to type 'nodeptr' (aka 'node *') cannot bind to a temporary of type 'nodeptr'
So here is implementation of deletes and merge
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));
}
And how i implement treap as a data structure.
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);
};
Can you say me, why is there this error? I think that all is fine. Sorry, if the question is nooby. Thanks in advance.
You have hit the annoyance of references over pointers. When you say p=merge(...)
the compiler is trying to assign the temporary nodeptr
returned by merge
to the reference. But then the temp is out of scope and p
no longer holds a valid reference. Sadly, sometimes you need to use pointers.