Page 230 - DCAP407_DATA_STRUCTURE
P. 230
Unit 12: Binary Tree Traversals and Operations
struct node *make(int y)
{
struct node *newnode; // Declare newnode as pointer to struct node
newnode=(struct node *) malloc(sizeof(struct node)); //Allocate space in memory
/*Assign object data to newnode and initialize to variable y*/
newnode->data=y;
/*Declare right newnode and left newnode to NULL*/
newnode->right=newnode->left=NULL;
return(newnode);
}
void left(struct node *r,int x) // Define left sub-tree function
{
/*Checks if left sub-trees is not equal to NULL*/
if(r->left!=NULL)
printf("\n Invalid !"); // Prints invalid
else
r->left=make(x); //Initialize left sub-tree
}
void right(struct node *r,int x) //Define right sub-tree
{
/*Checks if right sub-tree is not equal to NULL*/
if(r->right!=NULL)
printf("\n Invalid !"); // Prints invalid
else
r->right=make(x); //Initialize right sub-tree
}
void inorder(struct node *r) //Define inorder traversal function
{
/*Conditional statement, check if r is not equal to NULL*/
if(r!=NULL)
{
/ *Recursively call inorder passing the address of the left sub-tree*/
inorder(r->left);
printf("\t %d", r->data); //Prints the data of the node
/ *Recursively call inorder passing the address of the left sub-tree*/
inorder(r->right);
}
}
LOVELY PROFESSIONAL UNIVERSITY 223