SORTING USING BINARY SEARCH TREE

464

 

SORTING USING BINARY SEARCH 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;

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);
}
}

void sort_a(struct node *temp)
{
if(temp != NULL)
{
sort_a(temp -> left);
printf(” %d “,temp->data);
sort_a(temp -> right);
}
}

void main()
{
int ch,item;
root = NULL;
do
{
clrscr();
printf(“1. Creation \n2. Sorting \n3. 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:
sort_a(root);
getch();
break;
}
}while(ch != 3);
}

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
10 30 40 50 60 70 80

 

if you need a Downloadable link once message us on-page you can get at a time.

m.me/bcanotesnepal

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/

LEAVE A REPLY

Please enter your comment!
Please enter your name here