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

University Questions

This document implements a binary search tree in C. It includes functions to insert nodes, search for a node, delete nodes, and display the tree inorder. The main function allows the user to choose between these options and build and manipulate the binary search tree accordingly. Key functions include inserting nodes by recursively comparing the node value and traversing left or right, searching by recursively traversing and comparing values, and deleting nodes by handling three cases for the node to remove.

Uploaded by

lekhaperumal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

University Questions

This document implements a binary search tree in C. It includes functions to insert nodes, search for a node, delete nodes, and display the tree inorder. The main function allows the user to choose between these options and build and manipulate the binary search tree accordingly. Key functions include inserting nodes by recursively comparing the node value and traversing left or right, searching by recursively traversing and comparing values, and deleting nodes by handling three cases for the node to remove.

Uploaded by

lekhaperumal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

EX NO 6 Implementation of Binary Search tree

#include<stdio.h> #include<stdlib.h> typedef struct bst { int data; struct bst *left,*right; }node; void insert(node *,node *); node *search(node *,int,node **); void del(node *,int); void inorder(node *); node findmin(node *); void main() { int choice; char ans='N'; int key; node *new,*root,*tmp,*parent; node *get_node(); root=NULL; printf("\n\t program for binary search tree"); do { printf("\n1.create\n2.search\n3.delete\n4.display"); printf("\n\nenter your choice:"); scanf("%d",&choice); switch(choice) { case 1: do { new=get_node(); printf("\n enter the element"); scanf("%d",&new->data); if(root==NULL)/*tree is not created*/ root=new; else insert(root,new); printf("\n do you want to enter more elements?(y/n)"); ans=getch(); } while(ans=='y'); break; case 2: printf("\n enter the element which you want to search"); scanf("%d",&key); tmp=search(root,key,&parent);

printf("\n parent of node %dis %d",tmp->data,parent->data); break; case 3: printf("\n enter the element you wish to delete"); scanf("%d",&key); del(root,key); break; case 4: if(root==NULL) printf("tree is not created"); else { printf("\n then tree is:"); inorder(root); } break; } } while(choice!=5); } node *get_node() { node *temp; temp=(node *)malloc(sizeof(node)); temp->left=NULL; temp->right=NULL; return temp; } /*this function is for creating a binary search tree*/ void insert(node *root,node *new) { if(new->data<root->data) { if(root->left==NULL) root->left=new; else insert(root->left,new); } if(new->data>root->data) { if(root->right==NULL) root->right=new; else insert(root->right,new); } } /* this function is for searching the node from binary search tree

*/ node *search(node *root,int key,node **parent) { node *temp; temp=root; while(temp!=NULL) { if(temp->data==key) { printf("\n the %d element is present",temp->data); return temp; } *parent=temp; if(temp->data>key) temp=temp->left; else temp=temp->right; } return NULL; } node findmin(node temp) { if(temp == NULL || temp->left == NULL) return temp; return findmin(temp->left);} /* this function is for deleting a node from binary search tree there exists three possible cases for deletion of a node */ void del(node *root,int key) { node *t; if(root == NULL) printf("\nElement not found"); else { if(x < root->element) root->left = delet(x, root->left); else if(x > root->element) root->right = delet(x, t->right); else { if(t->left && t->right) { temp = findmin(t->right); root->element = temp->element; root->right = delet(root->element,root->right); } else if(root->left == NULL) { temp = root; root=root->right; free (temp);

} else { temp = root; root=root->left; free (temp); }} } return root; } } /*this function displays the tree in inorder fashion */ void inorder(node *temp) { if(temp!=NULL) { inorder(temp->left); printf("%d",temp->data); printf(->); inorder(temp->right); } }

You might also like