Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct node{
- int data;
- struct node* rightl;
- struct node* leftl;
- };
- struct node* root=NULL;
- struct node* parent=NULL;
- int insert_data(){
- int item;
- printf("Enter data to be inserted:");
- scanf("%d",&item);
- return item;
- }
- void preorder(struct node* ptr){
- if(ptr!=NULL){
- printf("%d\t",ptr->data);
- preorder(ptr->leftl);
- preorder(ptr->rightl);
- }
- }
- void inorder(struct node* ptr){
- if(ptr!=NULL){
- inorder(ptr->leftl);
- printf("%d\t",ptr->data);
- inorder(ptr->rightl);
- }
- }
- void postorder(struct node* ptr){
- if(ptr!=NULL){
- postorder(ptr->leftl);
- postorder(ptr->rightl);
- printf("%d\t",ptr->data);
- }
- }
- struct node* search(int item){
- struct node* current=root;
- parent=NULL;
- while(current!=NULL){
- if(current->data==item){
- break;
- }
- else{
- parent=current;
- if(item>current->data)
- current=current->rightl;
- else
- current=current->leftl;
- }
- }
- return current;
- }
- void insertion(){
- struct node* current;
- struct node* newnode;
- newnode = (struct node*)malloc(sizeof(struct node));
- int item=newnode->data=insert_data();
- newnode->rightl=NULL;
- newnode->leftl=NULL;
- if(root==NULL)
- root=newnode;
- else{
- current=root;
- parent=NULL;
- while(current!=NULL){
- parent=current;
- if(item>current->data)
- current=current->rightl;
- else if(item<current->data)
- current=current->leftl;
- else{
- printf("Duplicate node");
- return;
- }
- }
- if(item>parent->data)
- parent->rightl=newnode;
- else
- parent->leftl=newnode;
- }
- }
- struct node* successor(struct node* temp){
- if(temp!=NULL){
- while(temp->leftl!=NULL)
- temp=temp->leftl;
- }
- return temp;
- }
- void deletion(int delItem){
- struct node* current;
- if(root==NULL){
- printf("Empty Tree\n");
- return;
- }
- current = search(delItem);
- if(current==NULL)
- printf("\nItem not Found!\n");
- else if(current->leftl==NULL && current->rightl==NULL){
- if(parent==NULL){
- free(current);
- root=NULL;
- }
- else if(parent->leftl==current){
- parent->leftl=NULL;
- free(current);
- }
- else if(parent->rightl==current){
- parent->rightl=NULL;
- free(current);
- }
- }
- else if(current->leftl==NULL && current->rightl!=NULL){
- if(parent==NULL){
- free(current);
- root=current->rightl;
- }
- else if(parent->leftl==current){
- parent->leftl=current->rightl;
- free(current);
- }
- else if(parent->rightl==current){
- parent->rightl=current->rightl;
- free(current);
- }
- }
- else if(current->leftl!=NULL && current->rightl==NULL){
- if(parent==NULL){
- free(current);
- root=current->leftl;
- }
- else if(parent->leftl==current){
- parent->leftl=current->leftl;
- free(current);
- }
- else if(parent->rightl==current){
- parent->rightl=current->leftl;
- free(current);
- }
- }
- else if(current->leftl!=NULL && current->rightl!=NULL){
- struct node* succ;
- succ=successor(current->rightl);
- delItem=succ->data;
- deletion(delItem);
- current->data=delItem;
- }
- }
- int main(){
- int arg;
- while(1){
- printf("\n******************");
- printf("\nEnter your choice:\n1. Insertion\n2. Traversal\n3. Deletion\n4. Search\n0. Exit");
- printf("\n******************\n");
- scanf("%d",&arg);
- switch(arg){
- case 1:
- insertion();
- break;
- case 2:{
- int search_arg;
- printf("\n*****_TRAVERSAL MENU_*****");
- printf("\nEnter your choice:\n1. PreOrder\n2. InOrder\n3. PostOrder");
- printf("\n**************************\n");
- scanf("%d",&search_arg);
- switch(search_arg){
- case 1:
- printf("\nPreOrder: ");
- preorder(root);
- break;
- case 2:
- printf("\nInOrder: ");
- inorder(root);
- break;
- case 3:
- printf("\nPostOrder: ");
- postorder(root);
- break;
- }
- printf("\n");
- break;
- }
- case 3:{
- int item;
- printf("Enter item to be deleted:");
- scanf("%d",&item);
- deletion(item);
- break;
- }
- case 4:{
- int item;
- printf("Enter item to be searched: ");
- scanf("%d",&item);
- struct node* current=search(item);
- if(current==NULL)
- printf("Item not Found");
- else
- printf("Item Found");
- break;
- }
- case 0:
- exit(0);
- default:
- printf("Invalid Argument!");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement