SEARCH ELEMENT FOR TREE
bca third semester
Data structure
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int data;
struct node *right, *left;
};
struct node *nw, *root, *temp;
int key,flag=0;
void creation(struct node *root, struct node *nw)
{
if(nw -> data < root-> data)
{
if(root -> left == NULL)
root -> left = nw;
else
creation(root->left, nw);
}
if(nw -> data > root -> data)
{
if(root -> right == NULL)
root -> right = nw;
else
creation(root -> right , nw);
}
}
int search(struct node *temp,int key)
{
if(temp != NULL)
{
if(key == temp-> data)
printf(“Searching Sucessfull”);
else if(key < temp->data)
search(temp->left,key);
else if(key > temp -> data)
search(temp->right,key);
}
else
printf(“Not Found”);
return (key);
}
void inorder(struct node *temp)
{
if(temp != NULL)
{
inorder(temp -> left);
printf(” %d “,temp->data);
inorder(temp -> right);
}
}
void preorder(struct node *temp)
{
if(temp != NULL)
{
printf(” %d “,temp->data);
preorder(temp -> left);
preorder(temp -> right);
}
}
void postorder(struct node *temp)
{
if(temp != NULL)
{
postorder(temp -> left);
postorder(temp -> right);
printf(” %d “,temp->data);
}
}
void main()
{
int ch,item;
root = NULL;
do
{
clrscr();
printf(“1. Creation \n2. Searching\n3. Inorder\n4. Preorder
\n5. Postorder\n6. Exit\n\nEnter Your Choice : “);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
{
printf(“Enter Item : “);
scanf(“%d”,&item);
nw= (struct node *)malloc(sizeof(struct node));
nw -> data = item;
nw -> left = NULL;
nw -> right = NULL;
if(root == NULL)
root = nw;
else
creation(root,nw);
}
break;
case 2:
clrscr();
printf(“Enter The Number You want to Search: “);
scanf(“%d”,&key);
search(root,key);
getch();
break;
case 3:
inorder(root);
getch();
break;
case 4:
preorder(root);
getch();
break;
case 5:
postorder(root);
getch();
break;
}
}while(ch != 6);
getch();
}
OUTPUT
1. Creation
2. Searching
3. Inorder
4. Preorder
5. Postorder
6. Exit
Enter Your Choice : 1
Enter Element : 50
1. Creation
2. Searching
3. Inorder
4. Preorder
5. Postorder
6. Exit
Enter Your Choice : 1
Enter Element : 30
1. Creation
2. Searching
3. Inorder
4. Preorder
5. Postorder
6. Exit
Enter Your Choice : 1
Enter Element : 70
1. Creation
2. Searching
3. Inorder
4. Preorder
5. Postorder
6. Exit
Enter Your Choice : 1
Enter Element : 10
1. Creation
2. Searching
3. Inorder
4. Preorder
5. Postorder
6. Exit
Enter Your Choice : 1
Enter Element : 40
1. Creation
2. Searching
3. Inorder
4. Preorder
5. Postorder
6. Exit
Enter Your Choice : 1
Enter Element : 60
1. Creation
2. Searching
3. Inorder
4. Preorder
5. Postorder
6. Exit
Enter Your Choice : 1
Enter Element : 80
1. Creation
2. Searching
3. Inorder
4. Preorder
5. Postorder
6. Exit
Enter Your Choice : 2
Enter The Number You want to Search: 60
Searching Sucessfull
1. Creation
2. Searching
3. Inorder
4. Preorder
5. Postorder
6. Exit
Enter Your Choice : 3
10 30 40 50 60 70 80
1. Creation
2. Searching
3. Inorder
4. Preorder
5. Postorder
6. Exit
Enter Your Choice : 4
50 30 10 40 70 60 80
1. Creation
2. Searching
3. Inorder
4. Preorder
5. Postorder
6. Exit
Enter Your Choice : 5
10 40 30 60 80 70 50
- install our bcanotesnepal application from the play store: BCA Notes NepalDo does not forget to like our Facebook page: www.facebook.com/bcanotesnepal
Facebook Groups https://www.facebook.com/groups/bcanotesnepal
Join In our Facebook Group https://www.facebook.com/groups/1200219363518714/
if you need a Downloadable link once message us on-page you can get at a time.
This post is posted for not further third-party purposes.
Copyright By:- Bcanotesneapl
Not allowed to upload these notes to other sites.
Like our Facebook Page https://www.facebook.com/bcanotesnepal/
if you want to download this PDF comment on us we will provide you a downloadable link.
if you like please share this post with your friend who studies in BCA
if you have any query please comment below,
if you like our sites. you can visit again for the latest updates notes of different BCA subjects of Presentation and different topics with assignment questions and solutions.
Don’t forget to like our page and post also share with your friend and having your group.
if you have any questions you can drop or share them with me using the comment box I will try to answer your question.
Like our Facebook Page https://www.facebook.com/bcanotesnepal/