C
C
Chemodan2282020-02-22 00:10:58
C++ / C#
Chemodan228, 2020-02-22 00:10:58

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);}
    }
}

Tell me what can be done with the function?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question