0% found this document useful (0 votes)
10 views9 pages

Ds Ex10 Binary Search Tree

1

Uploaded by

edevipriya01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

Ds Ex10 Binary Search Tree

1

Uploaded by

edevipriya01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
struct node *left;
int data;
struct node *right;
};
struct node *tree;
struct node *insert(struct node *,int);
void preorder(struct node *);
void inorder(struct node*);
void postorder(struct node*);
struct node *del_element(struct node*,int );
struct node *smallest_node(struct node *);
int search (int);
void main()
{
int ch,val,c,n,i;
struct node *ptr;
clrscr();
printf("\n\tBINARY SEACH TREE");
do
{
printf("\nMENU\n1.Insert \n2.Traveral \n3.Search \n4.Delete \n5.Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the no.of elements to insert:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the value:");
scanf("%d",&val);
tree=insert(tree,val);
}

break;
case 2:
printf("\nDisplaying the Tree\n");
printf("\nPreOrder Traversal\n");
preorder(tree);
printf("\nInOrer Traversal\n");
inorder(tree);
printf("\nPostOrder Traversal\n");
postorder(tree);
break;
case 3:
printf("\n Enter the element to search:");
scanf("%d",&val);
if(search(val)==1)
printf("\n Element is present in the tree");
else
printf("\nElement is not present in the tree");
break;
case 4:
printf("\nEnter the element to delete:");
scanf("%d",&val);
tree=del_element(tree,val);
break;
case 5:
printf("\nThank you");
break;
default:
printf("Wrong Input");
break;
}
}while(ch!=5);
getch();
}
struct node *insert(struct node *tree,int val)
{
struct node *ptr,*nodeptr,*parentptr;
ptr=(struct node*)malloc(sizeof(struct node));
ptr->data=val;
ptr->left=NULL;
ptr->right=NULL;
if(tree==NULL)
{
tree=ptr;
tree->left=NULL;
tree-
>right=NULL;
}
else
{
parentptr=NULL;
nodeptr=tree;
while(nodeptr!=NULL)
{
parentptr=nodeptr;
if(val<nodeptr->data)
nodeptr=nodeptr->left;
else
nodeptr=nodeptr->right;
}
if(val<parentptr->data)
parentptr->left=ptr;
else
parentptr->right=ptr;

}
return tree;
}
void preorder(struct node *tree)
{
if(tree!=NULL)
{
printf("%d\t",tree->data);
preorder(tree->left);
preorder(tree->right);
}
}
void inorder(struct node *tree)
{
if(tree!=NULL)
{
inorder(tree->left);
printf("%d\t",tree->data);
inorder(tree->right);
}
}
void postorder(struct node *tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
printf("%d\t",tree->data);
}
}

struct node *del_element(struct node *tree, int key)


{
struct node *temp;
if (tree == NULL)
{
return tree;
}
if (key < tree->data)
{
tree->left = del_element(tree->left, key);
}
else if (key > tree->data)
{
tree->right = del_element(tree->right, key);
}
else
{
if (tree->left == NULL)
{
struct node *temp = tree->right;
free(tree);
return temp;
}
else if (tree->right == NULL)
{
struct node *temp = tree->left;
free(tree);
return temp;
}
temp = smallest_node(tree->right);
tree->data = tree->data;
tree->right = del_element(tree->right, temp->data);
}
printf("\nDeleted Successfully");
return tree;
}

struct node *smallest_node(struct node *tree)


{
struct node *curr = tree;
while (curr != NULL && curr->left != NULL)
{
curr = curr->left;
}
return curr;
}
int search(int key)
{
struct node *temp = tree;
while (temp != NULL)
{
if (key == temp->data)
{
return 1;
}
else if (key > temp->data)
{
temp = temp->right;
}
else
{
temp = temp->left;
}
}
return 0;
}
OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy