Answer the question
In order to leave comments, you need to log in
The function doesn't work. What's the best thing to do?
int left_child(int i){
return 2*i+1;
}
int right_child(int i){
return 2*i+2;
}
typedef struct{
int quantity;
int *val;
} binary_tree;
void shift_down(binary_tree *s,int pos){
int leftchild=left_child(pos);
int rightchild=right_child(pos);
if (right_child(pos) < s->quantity){
if (s->val[leftchild] > s->val[rightchild] && s->val[rightchild] > s->val[pos])
{ swap(&s->val[rightchild],&s->val[pos]);
return shift_down(s,rightchild);}
else if (s->val[leftchild] > s->val[pos]){
swap(&s->val[leftchild],&s->val[pos]);
return shift_down(s,leftchild);}
}
else if (leftchild < s->quantity){
if (s->val[leftchild] > s->val[pos]) {swap(&s->val[leftchild],&s->val[pos]);return shift_down(s,leftchild);}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question