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

BST

This document defines functions for operations on a binary search tree (BST) data structure implemented with C pointers and structs. It includes functions to insert nodes, search for nodes, find the minimum/maximum nodes, traverse the tree using preorder, inorder and postorder traversal, and delete nodes from the tree. The main function provides a menu to test these BST functions and recursively calls them to manipulate and traverse a dynamically allocated tree rooted at the global pointer variable root.

Uploaded by

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

BST

This document defines functions for operations on a binary search tree (BST) data structure implemented with C pointers and structs. It includes functions to insert nodes, search for nodes, find the minimum/maximum nodes, traverse the tree using preorder, inorder and postorder traversal, and delete nodes from the tree. The main function provides a menu to test these BST functions and recursively calls them to manipulate and traverse a dynamically allocated tree rooted at the global pointer variable root.

Uploaded by

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

#include<stdio.

h>

#include<stdlib.h>

struct bstnode

int data;

struct bstnode *right;

struct bstnode *left;

}*root=NULL;

typedef struct bstnode node;

int search(node *root,int sec);

node *create();

void insert(node *root,node *temp);

node *findmin(node *root);

void preorder(node *root);

void findmax(node *root);

void inorder(node *root);

node *delet(node *root,int ele);

void postorder(node *root);

int search(node *root,int sec);

int main()

while(1)

int ch,de,sec,flag;

node *temp,*min,*del;
printf("\n1.insert\n2.preorder\n3.inorder\n4.postorder\n5.findmin\n6.findmax\n7.search\n8.delete\n9
.exit\n");

printf("\n\tEnter your choice:");

scanf("%d",&ch);

switch(ch)

case 1:

temp=create();

if(root==NULL)

root=temp;

else

insert(root,temp);

break;

case 2:

preorder(root);

break;

case 3:

inorder(root);

break;

case 4:

postorder(root);

break;

case 5:

min=findmin(root);

printf("Minimum element = %d",min->data);

break;

case 6:

findmax(root);

break;
case 7:

printf("\n\tEnter the element to search:");

scanf("%d",&sec);

flag=search(root,sec);

if(flag==1)

printf("Element is found");

else

printf("Element is not found");

break;

case 8:

printf("\n\tEnter the element to delete:");

scanf("%d",&de);

del=delet(root,de);

printf("\n\tThe deleted element is %d",de);

break;

case 9:

exit(0);

return 0;

node *create()

node *temp;

int num;

temp=(node *)malloc(sizeof(node));

printf("Enter the data");

scanf("%d",&num);
temp->data=num;

temp->right=NULL;

temp->left=NULL;

return temp;

void insert(node *root,node *temp)

if(temp->data<root->data)

if(root->left!=NULL)

insert(root->left,temp);

else

root->left=temp;

if(temp->data>=root->data)

if(root->right!=NULL)

insert(root->right,temp);

else

root->right=temp;

node *findmin(node *root)


{

if(root==NULL)

printf("no tree");

else if(root->left!=NULL)

findmin(root->left);

else

return root;

void findmax(node *root)

if(root==NULL)

printf("no tree");

else if(root->right!=NULL)

findmax(root->right);

else

printf("\n\tThe maximum element is %d",root->data);

void preorder(node *root)

if(root!=NULL)

printf("%d ",root->data);

preorder(root->left);

preorder(root->right);

}
void inorder(node *root)

if(root!=NULL)

inorder(root->left);

printf("%d ",root->data);

inorder(root->right);

void postorder(node *root)

if(root!=NULL)

postorder(root->left);

postorder(root->right);

printf("%d ",root->data);

node *delet(node *root,int ele)

if(ele<root->data)

root->left=delet(root->left,ele);

else if(ele>root->data)

root->right=delet(root->right,ele);

else

if(root->left==NULL&&root->right==NULL)
{

free(root);

return NULL;

else if(root->left==NULL||root->right==NULL)

node *temp;

if(root->left==NULL)

temp=root->right;

else

temp=root->left;

return temp;

else

node *temp=findmin(root->right);

root->data=temp->data;

root->right=delet(root->right,temp->data);

return root;

int search(node *root,int sec)

if(root==NULL)

return 0;

else if(root->data==sec)

return 1;
else if(root->data>sec)

search(root->left,sec);

else if(root->data<sec)

search(root->right,sec);

else

return 0;

You might also like