c - return current node from BST -


hello i've run issues cannot seem resolve. have bst traversing through , checking ranks. have method checkrank(link head, targrank) takes in head node , traverses through tree until finds node equal rank targrank. trying have checkrank function return current node found equal rank at. best way achieve because attempts seem return current node head?

typedef struct node* link;  struct node  {     item item;  // data node     link l, r;  // left & right links     int rank; }; 

func call:

link head; checkrank(head, 13); 

func:

link checkrank(link h,int targetrank) {     if (h != null)     {         if (h->rank < targrank)         {             checkrank(h->r, targrank);         }       if (h->rank > tarrank)         {             checkrank(h->l, targtrank);         }          if (h->rank == targrank)         {             return ??;         }     }     else     {         printf("equiv rank not found\n");     } } 

first of all, need return along each path. have considered following:

link check_rank(link h, int target) {   if (h == null) {     printf("equivalent rank not found\n");     return null;   }   if (h->rank < target)     return check_rank(h->r, target);   if (h->rank > target)     return check_rank(h->l, target);   return h; } 

functions have return value , many recursive functions follow pattern of (1) return sentinel stop recursion when appropriate condition met or (2) recurse , return whatever recursive call returns.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -