Thread: what this function does..(i tried to trace it)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    what this function does..(i tried to trace it)

    i understand what each line does here

    but i cant see what this function does in general??

    Code:
    typedef struct node node;         //node is alias for struct node
    struct node{                               
        int value,count;
        node *lc,*rc;
    };
    
    node *what(node *tree){
        node *save,*temp;
        int flag;
        if(!tree)return NULL;          //if root is null the program stops and return null
        if(!tree->lc && !tree->rc){ 
               free(tree);
               return NULL;
        }                                         //if the sons of root is null then we free the root and 
                                                   //return null
    
    
        save=temp=tree;                // save and temp point to the same place as the root
     
        if(save->lc) {flag=1;            //if (*save).lc differs null 
    
                       save=save->lc;    //save points to the same place as its left son
     
                       while(save->rc){ //while saves right son differs null
                           flag=0;        
                           temp=save;
                           save=save->rc;  //save point to the same place where save right son
                       }
        }
        else{ flag=0;               //if (*save).lc equals null 
               save=save->rc;    //save points to the same place as its right son
               while(save->lc){  //while saves left son differs null
                   flag=0;
                   temp=save;   
                   save=save->rc; //save points to the same place as its right son
               }
    
        }
        if(!flag)
              temp->rc=save->lc;
         else
               temp->lc=save->rc;
         tree->value=save->value;
         return tree;
    }
    Last edited by transgalactic2; 10-28-2008 at 04:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM