C program to implement Tree data structure and its operations

Simple C program to implement Tree data structure having following operations:

  • Insert a value
  • Inorder Display
  • Preorder Display
  • Postorder Display
  • Search An Element
  • Delete An Element

Code:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>


struct node{
int data;
struct node *left;
struct node *right;
};


struct node *root;


struct node *newnode(){
int val;
printf("\nEnter the value to be inserted:");
scanf("%d",&val);
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->left=NULL;
temp->right=NULL;
temp->data=val;
return temp;
}


void insert(struct node *root, struct node *root2) {
if (root2->data <= root->data) {
if (root->left == NULL)
root->left = root2;
else
insert(root->left, root2);
}


if (root2->data > root->data) {
if (root->right == NULL)
root->right = root2;
else
insert(root->right, root2);
}
}




void displayi(struct node *temp) {
if(temp!=NULL) {
displayi(temp->left);
printf("\t%d", temp->data);
displayi(temp->right);
}
}


void displaypre(struct node *temp) {
if(temp != NULL) {
printf("\t%d", temp->data);
displaypre(temp->right);
displaypre(temp->left);
}
}


void displaypost(struct node *temp) {
if(temp != NULL) {
displaypost(temp->left);
displaypost(temp->right);
printf("\t%d", temp->data);
}
}




struct node* search(struct node* root, int key)
{


if (root == NULL || root->data == key)
return root;




if (root->data < key)
return search(root->right, key);




return search(root->left, key);
}








struct node *min(struct node* temp)
{
struct node* temp2 = temp;
while (temp2->left != NULL)
temp2 = temp2->left;


return temp2;
}






struct node* deletee(struct node* root, int key)
{


if (root == NULL) return root;




if (key < root->data)
root->left = deletee(root->left, key);


else if (key > root->data)
root->right = deletee(root->right, key);


else
{


if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}




struct node* temp = min(root->right);
root->data = temp->data;
root->right = deletee(root->right, temp->data);
}
return root;
}








main(){
root=NULL;
int opt,val;
struct node *temp,*answer;
printf("*****Operations on tree******\n");
printf("1. Insert a value");
printf("\t2. Inorder Display");
printf("\t3. Preorder Display");
printf("\t4. Postorder Display");
printf("\t5. Search An Element");
printf("\t6. Delete An Element");
printf("\t7. Exit\n");
do{
printf("\nEnter your choice: ");
scanf("%d",&opt);




switch(opt){
case 1:


temp=newnode();
if(root==NULL){root=temp;}
else{insert(root,temp);}
break;
case 2:
if (root==NULL)
{printf("tree is empty!\n");}
else {
printf("\ninorder:");
displayi(root);
printf("\n");
}
break;
case 3:
if (root==NULL)
{printf("tree is empty!\n");}
else {
printf("\npreorder:");
displaypre(root);
printf("\n");
}
break;
case 4:
if (root==NULL)
{printf("tree is empty!\n");}
else {
printf("\npostorder:");
displaypost(root);
printf("\n");
}
break;




case 5:
printf("Enter the value to be searched: ");
scanf("%d",&val);
struct node *root2=root;
answer=search(root2,val);
if(answer==NULL){printf("Value not found!");}
else {printf("Search found: %d",answer->data);}
break;




case 6:
printf("Enter the value to be deleted: ");
scanf("%d",&val);
struct node *root3=root;
if(root3==NULL){printf("Tree s empty!");}
else {deletee(root3,val);}
break;






case 7:
break;
default:printf("Wrong choice!\n");
break;
}


}while(opt!=7);
printf("End");
}

Comments

Popular posts from this blog

C program to evaluate Prefix Expression using Stack data structure

Servlet Program to Print Today’s Date and Time using refresh header

Java Program to Implement sorting algorithm using TCP on Server application