How to add nodes in a Binary tree.

Firstly, we know how to define binary tree and than we declare the structure of a binary tree. In the binary tree a data and his left and right value are present and his left and right value are continusily increasing according to our need.So we declare left and right pointer value in the structure of binary tree.here is program ———————

=>How to add nodes in a binary tree.

typedef struct tree
{
int data;
struct tree *lft;
struct tree *rt;
}treenode;

void addNode(treenode** root,int data);

int main()
{
int option,data;
treenode *root=NULL;

while(1)
{
menu();
printf("enter the choice");
scanf("%d",&option);
switch(option)

{
case 0:
return 0;
case 1:
printf("enter the value\n");
scanf("%d",&data);
addNode(&root,data);
break;

default:printf("not found");

}
}
return 0;
}

void addNode(treenode** root,int data)
{

if((*root)==NULL)
{
*root=(treenode*)malloc(sizeof(treenode));
(*root)->data=data;
(*root)->lft=NULL;
(*root)->rt=NULL;
return;
}
if(data<=(*root)->data)
addNode(&(*root)->lft,data);
else
addNode(&(*root)->rt,data);
}

Leave a Reply