Created
April 18, 2019 16:04
-
-
Save jatinsharrma/79e67b0d8214a59b25f43f01ec78b4c3 to your computer and use it in GitHub Desktop.
Binary Tree implementation in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
// Initialization ---------------------------------------------------------------------------------- | |
struct node { | |
int data; | |
struct node* left; | |
struct node* right; | |
}; | |
struct node* root = NULL; | |
//prototyping---------------------------------------------------------------------------------------- | |
struct node * insert(struct node *, int); | |
struct node * getnew (int); | |
void search (struct node * , int); | |
int preorder(struct node* ); | |
int postorder(struct node* ); | |
int inorder(struct node* ); | |
struct node * delete(struct node*, int); | |
//--------------------------------------------------------------------------------------------------- | |
int main(){ | |
while(1){ | |
printf("\nChoose\n"); | |
printf(" 1. Insert \n 2. Search \n 3. Preorder Traversal \n 4. Postorder Traversal \n 5. Inorder Traversal \n 6. Delete element \n 0. exit \n"); | |
printf("\nEnter your choice : "); | |
int option; | |
scanf("%d",&option); | |
switch (option) | |
{ | |
case 1 : {printf("\nEnter the data of node : "); | |
int data; | |
scanf("%d", &data); | |
root = insert(root,data); | |
break;} | |
case 2 : {if(root == NULL){ | |
printf("\nTree is empty\n");} | |
else{ | |
printf("\nEnter the data you want to search : "); | |
int data1; | |
scanf("%d",&data1); | |
search(root,data1);} | |
break;} | |
case 3 : {if(root == NULL){ | |
printf("\nTree is empty\n");} | |
else{ | |
preorder(root);} | |
break;} | |
case 4 : {if(root == NULL){ | |
printf("\nTree is empty\n");} | |
else{ | |
postorder(root);} | |
break;} | |
case 5 : {if(root == NULL){ | |
printf("\nTree is empty\n");} | |
else{ | |
inorder(root);} | |
break;} | |
case 6 : {if(root == NULL){ | |
printf("\nTree is empty\n");} | |
else{ | |
printf("\nEnter the element you want to delete : "); | |
int data2; | |
scanf("%d",&data2); | |
if (root -> data == data2){ | |
printf("\nYou can not delete root element\n"); | |
} | |
else{ | |
delete(root,data2);} | |
} | |
break;} | |
case 0 : exit(0); | |
break; | |
default: | |
break; | |
} | |
} | |
return 0; | |
} | |
//--------------------------------------------------------------------------------------------------- | |
struct node* getnew(int data){ | |
struct node* temp; | |
temp = (struct node*)malloc(sizeof(struct node)); | |
temp -> right = NULL; | |
temp -> data = data; | |
temp -> left = NULL; | |
return temp; | |
} | |
//--------------------------------------------------------------------------------------------------- | |
struct node* insert(struct node * root, int data){ | |
if(root == NULL){ | |
root = getnew(data); | |
} | |
else if( data <= root -> data){ | |
root -> left = insert(root -> left, data); | |
} | |
else { | |
root -> right = insert(root -> right ,data); | |
} | |
return root; | |
} | |
//--------------------------------------------------------------------------------------------------- | |
void search(struct node * root , int data){ | |
if (root == NULL) printf("\nNo\n"); | |
else if (root ->data == data) printf("\nYes\n"); | |
else if (data <= root -> data) return search(root -> left, data); | |
else return search (root -> right , data); | |
} | |
//--------------------------------------------------------------------------------------------------- | |
int preorder(struct node* root){ | |
if(root == NULL){ | |
return 1; | |
} | |
else { | |
printf("%d ", root -> data); | |
if (root-> left){ | |
preorder(root -> left); | |
} | |
if (root -> right){ | |
preorder(root -> right); | |
} | |
} | |
return 1; | |
} | |
//--------------------------------------------------------------------------------------------------- | |
int postorder(struct node* root){ | |
if(root == NULL){ | |
return 1; | |
} | |
else { | |
if (root-> left){ | |
postorder(root -> left); | |
} | |
if (root -> right){ | |
postorder(root -> right); | |
} | |
printf("%d ", root -> data); | |
} | |
return 1; | |
} | |
//--------------------------------------------------------------------------------------------------- | |
int inorder(struct node* root){ | |
if(root == NULL){ | |
return 1; | |
} | |
else { | |
if (root-> left){ | |
inorder(root -> left); | |
} | |
printf("%d ", root -> data); | |
if (root -> right){ | |
inorder(root -> right); | |
} | |
} | |
return 1; | |
} | |
//--------------------------------------------------------------------------------------------------- | |
struct node* delete (struct node* root,int data){ | |
if (root == NULL) return NULL; | |
else if (root ->data == data){ | |
if (root -> left){ | |
return root ->left; | |
} | |
else if (root -> right){ | |
return root -> right; | |
} | |
free(root); | |
} | |
else if (data <= root -> data){ | |
struct node * ret, *temp; | |
ret = delete(root -> left, data); | |
if (ret != NULL ){ | |
temp = root -> left; | |
free(temp); | |
root -> left = ret; | |
} | |
} | |
else { | |
struct node * ret, *temp; | |
ret = delete(root -> right, data); | |
if (ret != NULL ){ | |
temp = root -> right; | |
free(temp); | |
root -> right = ret; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment