What this function does(i tried to trace it

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Function Trace
AI Thread Summary
The function "what" is designed to manipulate a binary tree, specifically to remove a node and restructure the tree accordingly. It begins by checking if the tree is null or if the node has no children, in which case it frees the node and returns null. If the node has children, it traverses the tree to find the rightmost node of the left subtree or the leftmost node of the right subtree, depending on which child exists. The function then reassigns pointers to maintain the tree structure after the removal of the specified node. To better understand the function, creating a visual diagram of various tree structures, including different configurations of nodes, is recommended. This approach helps clarify how the function modifies the tree and which commands directly affect its structure. Testing with various tree examples, such as empty trees or trees with different child configurations, can provide further insights into the function's behavior.
transgalactic
Messages
1,386
Reaction score
0
i understand what each line does here

but i can't 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;
}
 
Technology news on Phys.org
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.
 
what kind of tree should i try ??
how big
what values
?
 
what commands in this code are changing the structure of the tree?
lines like this:
Code:
save=save->lc;
only changes our perspective on the tree
this line only makes us look on a different section of a tree
 
transgalactic said:
what kind of tree should i try ??
how big
what values
?

Code:
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top