0% found this document useful (0 votes)
23 views

Binary Search Tree

This document contains code for implementing a binary search tree (BST) in C programming language. It includes functions to insert nodes, perform inorder, preorder and postorder traversals of the tree, search for a node, and a main function with a menu to test the BST operations. The functions work recursively to traverse the tree and search for nodes based on comparing node values. The main function allows building a sample BST by inserting values and then demonstrates the traversal and search operations through the menu.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Binary Search Tree

This document contains code for implementing a binary search tree (BST) in C programming language. It includes functions to insert nodes, perform inorder, preorder and postorder traversals of the tree, search for a node, and a main function with a menu to test the BST operations. The functions work recursively to traverse the tree and search for nodes based on comparing node values. The main function allows building a sample BST by inserting values and then demonstrates the traversal and search operations through the menu.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 7

Binary Search tree


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
struct node
{
int info;
struct node*left;
struct node*right;
};
typedef struct node BST;
BST *LOC, *PAR;
void search(BST *root, int item)
{
BST *save,*ptr;
if (root == NULL)
{
LOC = NULL;
PAR=NULL;
}
if (item == root -> info)
{
LOC = root;
PAR = NULL;
return;
}
if (item < root->info)
{
save = root;
ptr = root->left;
}
else
{
save = root;
ptr = root -> right;
}
while( ptr != NULL)
{
if (ptr -> info == item)
{
LOC = ptr;
PAR = save;
return;
}
if(item < ptr->info)
{
save = ptr;
ptr = ptr->left;
}
else
{
save = ptr;
ptr = ptr->right;
}
}
LOC = NULL;
PAR = save;
return;

getch();
}
struct node*insert(struct node*r, int x)
{
if (r == NULL)
{
r = (struct node*)malloc(sizeof(struct node));
r->info = x;
r->left = r->right = NULL;
return r;
}
else if (x < r->info)
r->left = insert(r->left, x);
else if (x > r->info)
r->right = insert(r->right, x);
return r;
getch();
}
void inorder(struct node* r)
{
//
printf("Inorder Traversal: ");
if(r!=NULL)
{
inorder(r->left);
printf("%d ", r->info);
printf("\n");
inorder(r->right);
}
}
void preorder(struct node* r)
{
//printf("Preorder Traversal: ");
if(r!=NULL)
{
printf("%d ", r->info);
printf("\n");
preorder(r->left);
preorder(r->right);
}
}
void postorder(struct node* r)
{
//printf("Postorder Traversal: ");
if(r!=NULL)
{
postorder(r->left);
postorder(r->right);
printf("\n");
printf("%d ", r->info);
}
}
int main()
{
struct node* root = NULL;
int x, c = 1, z;
int element;
char ch;
printf("\nEnter an element in Binary Search tree\n: ");
scanf("%d", &x);
root = insert(root, x);
printf("\nDo you want to enter another element :y or n");

printf(" \t\t\t\t\t(y=YES & n=NO)\t");


scanf(" %c",&ch);
while (ch == 'y')
{
printf("\nEnter an element:");
scanf("%d", &x);
root = insert(root,x);
printf("\nPress y or n to insert another element: y or n: ");
scanf(" %c", &ch);
getch();
}
while(1)
{
printf("\t\t\t\t-------------------------------------\n");
printf("\t\t\t\t\t
Menu
\n");
printf("\t\t\t\t-------------------------------------\n");
printf("\n\t\t\t\t 1 Insert an element
");
printf("\n\t\t\t\t 2 In-Order traversal
");
printf("\n\t\t\t\t 3 Pre-Order traversal
");
printf("\n\t\t\t\t 4 Post-Order traversal
");
printf("\n\t\t\t\t 5 Search for an element
");
printf("\n\t\t\t\t 6 Exit
");
printf("\nEnter your choice: ");
scanf("%d", &c);
switch(c)
{
case 1:
printf("\nEnter the item:");
scanf("%d", &z);
root = insert(root,z);
break;
case 2:
printf(" In-Order Traversal\n ");
printf(" -------------------\n");
inorder(root);
getch();
break;
case 3:
printf(" Pre-Order Traversal\n ");
printf(" -------------------\n");
preorder(root);
getch();
break;
case 4:
printf(" Post-Order Traversal\n ");
printf(" -------------------\n");
postorder(root);
getch();
break;
case 5:
printf("\nEnter element to be searched: ");
scanf("%d", &element);
search(root, element);
if(LOC != NULL)
printf("\n%d Found in Binary Search Tree !!\n",element);
else
printf("\nIt is not present in Binary Search Tree\n");
break;
case 6:
printf("\nExiting...");
//return;
default:
printf("Enter a valid choice: ");

}
}
return 0;

You might also like