Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

What this function does(i tried to trace it

  1. Oct 28, 2008 #1
    i understand what each line does here

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

    Code (Text):

    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;
    }
     
     
  2. jcsd
  3. Oct 28, 2008 #2

    jtbell

    User Avatar

    Staff: Mentor

    The function name is "what" so I suspect this is a textbook or homework or exam problem. :smile:

    Have you tried drawing a diagram of an example tree, with arrows for pointers, and rearranging the arrows according to the statements? A visual representation of what's happening might make things more clear.
     
  4. Oct 28, 2008 #3
    what kind of tree should i try ??
    how big
    what values
    ?
     
  5. Oct 29, 2008 #4
    what commands in this code are changing the structure of the tree?
    lines like this:
    Code (Text):

    save=save->lc;
     
    only changes our perspective on the tree
    this line only makes us look on a different section of a tree
     
  6. Oct 29, 2008 #5

    KTC

    User Avatar

    Code (Text):
    typedef struct node node;         //node is alias for struct node
    struct node{                              
        int value,count;
        node *lc,*rc;
    };
     
    I would say a binary tree.

    Try some different examples. An empty tree, one with no left node, one with no right node, one with both, etc. See what result you get.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: What this function does(i tried to trace it
  1. What should I learn? (Replies: 1)

Loading...