Binary search tree

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *left;
struct node *right;
};


void inorder(struct node *root)
{
if(root)
{
inorder(root->left);
printf("   %d",root->data);
inorder(root->right);
}
}

int main()
{
int n , i;
struct node *p , *q , *root;
printf("\n\t\t****Crazy Coders****techapurba****");
printf("\n\nEnter the number of nodes to be inserted : ");
scanf("%d",&n);

printf("\n  Enter the numbers to be inserted : ");

for(i=0;i<n;i++)
{
p = (struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);

p->left = NULL;
p->right = NULL;

if(i == 0)
{
root = p; // root is pointing to the root node
}
else
{
q = root;   // q is being used to traverse the tree
while(1)
{
if(p->data > q->data)
{
if(q->right == NULL)
{
q->right = p;
break;
}
else
q = q->right;
}
else
{
if(q->left == NULL)
{
q->left = p;
break;
}
else
q = q->left;
}
}

}

}

printf("\nBinary Search Tree nodes in Inorder Traversal: ");
inorder(root);
printf("\n");

return 0;
}



OUTPUT :-



For Other Programs Visit The WebSite:-   https://www.techapurba.com/

Follow Me On Social Media :-

For tech related videos visit my other website :- https://apurbatechinfo.blogspot.com/

Post a Comment

0 Comments