BINARY SEARCH TREE TAVERSAL

638

 

bca third semester

Binary Search tree taversal

program 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 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. Inorder Traversal\n3. Preorder Traversal\n4. Postorder Traversal\n5. 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:
inorder(root);
getch();
break;
case 3:
preorder(root);
getch();
break;
case 4:
postorder(root);
getch();
break;
}
}while(ch != 5);
}

OUTPUT
1. Creation
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit

Enter Your Choice : 1

Enter Item : 50
1. Creation
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit

Enter Your Choice : 1
Enter Item : 30

1. Creation
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit

Enter Your Choice : 1
Enter Item : 70

1. Creation
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit

Enter Your Choice : 1
Enter Item : 10

1. Creation
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit

Enter Your Choice : 1
Enter Item : 40

1. Creation
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit

Enter Your Choice : 1
Enter Item : 60

1. Creation
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit

Enter Your Choice : 1
Enter Item : 80

1. Creation
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit

Enter Your Choice : 2

10 30 40 50 60 70 80

1. Creation
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit

Enter Your Choice : 3

50 30 10 40 70 60 80

1. Creation
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit

Enter Your Choice : 4

10 40 30 60 80 70 50

 

 

 

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