Skip to main content

Write a C Program that uses functions to perform the following operations on a Binary Tree Creation, Insertion, Deletion

 

#include<stdio.h>

 

#include<conio.h>

#include<stdio.h>

struct tree {

                int info;

                struct tree *left;

                struct tree *right;

};

struct tree *insert(struct tree *,int);

void inorder(struct tree *);

void postorder(struct tree *);

void preorder(struct tree *);

struct tree *delet(struct tree *,int);

struct tree *search(struct tree *);

int main(void) {

                struct tree *root;

                int choice, item,item_no;

                root = NULL;

 

                /* rear  = NULL;*/

                do {

                                do {

                                                printf("\n \t 1. Insert in Binary Tree  ");

                                                printf("\n\t 2. Delete from Binary Tree ");

                                                printf("\n\t 3. Inorder traversal of Binary tree");

                                                printf("\n\t 4. Postorder traversal of Binary tree");

                                                printf("\n\t 5. Preorder traversal of Binary tree");

                                                printf("\n\t 6. Search and replace ");

                                                printf("\n\t 7. Exit ");

                                                printf("\n\t Enter choice : ");

                                                scanf(" %d",&choice);

                                                if(choice<1 || choice>7)

                                                      printf("\n Invalid choice - try again");

                                }

                                while (choice<1 || choice>7);

                                switch(choice) {

                                                case 1:

                                                                   printf("\n Enter new element: ");

                                                scanf("%d", &item);

                                                root= insert(root,item);

                                                printf("\n root is %d",root->info);

                                                printf("\n Inorder traversal of binary tree is : ");

                                                inorder(root);

                                                break;

                                                case 2:

                                                                  printf("\n Enter the element to be deleted : ");

                                                scanf(" %d",&item_no);

                                                root=delet(root,item_no);

                                                inorder(root);

                                                break;

                                                case 3:

                                                                  printf("\n Inorder traversal of binary tree is : ");

                                                inorder(root);

                                                break;

                                                case 4:

                                                                  printf("\n Postorder traversal of binary tree is : ");

                                                postorder(root);

                                                break;

                                                case 5:

                                                                  printf("\n Preorder traversal of binary tree is : ");

                                                preorder(root);

                                                break;

                                                case 6:

                                                                  printf("\n Search and replace operation in binary tree ");

                                                root=search(root);

                                                break;

                                                default:

                                                                   printf("\n End of program ");

                                }

                                /* end of switch */

                }

                while(choice !=7);

             

                return(0);

}

struct tree *insert(struct tree *root, int x) {

                if(!root) {

                                root=(struct tree*)malloc(sizeof(struct tree));

                                root->info = x;

                                root->left = NULL;

                                root->right = NULL;

                                return(root);

                }

                if(root->info > x)

                     root->left = insert(root->left,x); else {

                                if(root->info < x)

                                                root->right = insert(root->right,x);

                }

                return(root);

}

void inorder(struct tree *root) {

                if(root != NULL) {

                                inorder(root->left);

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

                                inorder(root->right);

                }

                return;

}

void postorder(struct tree *root) {

                if(root != NULL) {

                                postorder(root->left);

                                postorder(root->right);

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

                }

                return;

}

void preorder(struct tree *root) {

                if(root != NULL) {

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

                                preorder(root->left);

                                preorder(root->right);

                }

                return;

}

/* FUNCTION TO DELETE A NODE FROM A BINARY TREE */

struct tree *delet(struct tree *ptr,int x) {

                struct tree *p1,*p2;

                if(!ptr) {

                                printf("\n Node not found ");

                                return(ptr);

                } else {

                                if(ptr->info < x) {

                                                ptr->right = delet(ptr->right,x);

                                                /*return(ptr);*/

                                } else if (ptr->info >x) {

                                                ptr->left=delet(ptr->left,x);

                                                return ptr;

                                } else

                                /* no. 2 else */ {

                                                if(ptr->info == x)

                                                /* no. 2 if */ {

                                                                if(ptr->left == ptr->right)

                                                                /*i.e., a leaf node*/ {

                                                                                free(ptr);

                                                                                return(NULL);

                                                                } else if(ptr->left==NULL)

                                                                /* a right subtree */ {

                                                                                p1=ptr->right;

                                                                                free(ptr);

                                                                                return p1;

                                                                } else if(ptr->right==NULL)

                                                                /* a left subtree */ {

                                                                                p1=ptr->left;

                                                                                free(ptr);

                                                                                return p1;

                                                                } else {

                                                                                p1=ptr->right;

                                                                                p2=ptr->right;

                                                                                while(p1->left != NULL)

                                                                                                    p1=p1->left;

                                                                                p1->left=ptr->left;

                                                                                free(ptr);

                                                                                return p2;

                                                                }

                                                }

                                                /*end of no. 2 if */

                                }

                                /* end of no. 2 else */

                                /* check which path to search for a given no. */

                }

                return(ptr);

}

/* function to search and replace an element in the binary tree */

struct tree *search(struct tree *root) {

                int no,i,ino;

                struct tree *ptr;

                ptr=root;

                printf("\n Enter the element to be searched :");

                scanf(" %d",&no);

                fflush(stdin);

                while(ptr) {

                                if(no>ptr->info)

                                     ptr=ptr->right; else if(no<ptr->info)

                                     ptr=ptr->left; else

                                     break;

                }

                if(ptr) {

                                printf("\n Element %d which was searched is found and is = %d",no,ptr->info);

                                printf("\n Do you want replace it, press 1 for yes : ");

                                scanf(" %d",&i);

                                if(i==1) {

                                                printf("\n Enter new element :");

                                                scanf(" %d",&ino);

                                                ptr->info=ino;

                                } else

                                    printf("\n\t It's okay");

                } else

                   printf("\n Element %d does not exist in the binary tree",no);

                return(root);

}

 


Comments

Popular posts from this blog

Windows 11 Pro Activation Key

 Windows 11 Pro key: W269N-WFGWX-YVC9B-4J6C9-T83GX Windows 11 Pro N key: MH37W-N47XK-V7XM9-C7227-GCQG9 Windows 11 Pro Workstations key: NRG8B-VKK3Q-CXVCJ-9G2XF-6Q84J Windows 11 Pro Workstations N key: 9FNHH-K3HBT-3W4TD-6383H-6XYWF Windows 11 Pro Education key: 6TP4R-GNPTD-KYYHQ-7B7DP-J447Y Windows 11 Home key: TX9XD-98N7V-6WMQ6-BX7FG-H8Q99 Windows 11 Home N key: 3KHY7-WNT83-DGQKR-F7HPR-844BM Windows 11 Home Home Single Language key: 7HNRX-D7KGG-3K4RQ-4WPJ4-YTDFH Windows 11 Home Country Specific: PVMJN-6DFY6-9CCP6-7BKTT-D3WVR Windows 11 Education key: NW6C2-QMPVW-D7KKK-3GKT6-VCFB2 Windows 11 Education N: 2WH4N-8QGBV-H22JP-CT43Q-MDWWJ Windows 11 Enterprise key: NPPR9-FWDCX-D2C8J-H872K-2YT43 Windows 11 Enterprise N key: DPH2V-TTNVB-4X9Q3-TJR4H-KHJW4 Windows 11 Enterprise G: YYVX9-NTFWV-6MDM3-9PT4T-4M68B Windows 11 Enterprise G N: 44RPN-FTY23-9VTTB-MP9BX-T84FV Windows 11 Enterprise LTSC 2019 key: M7XTQ-FN8P6-TTKYV-9D4CC-J462D Windows 11 Enterprise N LTSC 2019 key: 92NFX-8DJQP-P6BBQ-THF9C-7

Create Private folder in windows

 @ECHO OFF if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK if NOT EXIST Private goto MDPrivate :CONFIRM echo Are you sure to lock this folder? (Y/N) set/p "cho=>" if %cho%==Y goto LOCK if %cho%==y goto LOCK if %cho%==n goto END if %cho%==N goto END echo Invalid choice. goto CONFIRM :LOCK ren Private "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" echo Folder locked goto End :UNLOCK echo Enter password to Unlock Your Secure Folder set/p "pass=>" if NOT %pass%== 121 goto FAIL attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Private echo Folder Unlocked successfully goto End :FAIL echo Invalid password goto end :MDPrivate md Private echo Private created successfully goto End :End RAW Paste Data @ECHO OFF if EXIST "Control Panel.{21