What this function does(i tried to trace it

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Function Trace
Click For Summary

Discussion Overview

The discussion revolves around understanding the functionality of a specific C function named "what," which operates on a binary tree structure. Participants explore the implications of the code, its effects on tree structure, and seek clarity on how to visualize the function's behavior through examples.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant expresses understanding of the individual lines of code but is unclear about the overall purpose of the function.
  • Another participant suggests creating a visual representation of a tree to better understand how the function manipulates it.
  • A question is raised about what type of tree to use for testing the function, including considerations of size and values.
  • Concerns are discussed regarding which commands in the code actually alter the tree structure, with one participant noting that some lines merely change the perspective on the tree rather than its structure.
  • A suggestion is made to test various tree configurations, including empty trees and trees with different node arrangements.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the function's overall purpose, and multiple viewpoints regarding the testing approach and understanding of the code's effects are present.

Contextual Notes

Participants mention the need for visual aids and examples to clarify the function's behavior, indicating that the understanding of the code may depend on specific tree configurations and representations.

Who May Find This Useful

This discussion may be useful for individuals studying data structures, particularly binary trees, and those seeking to understand function manipulation within C programming.

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
5K
Replies
2
Views
2K
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
1K