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 *left;
- struct Node *right;
- } *root;
- //struct Node *root = NULL;
- /*struct Node *newNode(int item)
- {
- struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
- temp->data = item;
- temp->left = temp->right = NULL;
- return temp;
- }*/
- void insert(struct Node *temp_root, int data )
- {
- if(temp_root == NULL)
- {
- struct Node *p_root = (struct Node*)malloc(sizeof(struct Node));
- p_root->left = NULL;
- p_root->right = NULL;
- p_root->data = data;
- root = p_root;
- return;
- }
- else if(data < temp_root->data && temp_root->left != NULL)
- {
- temp_root = temp_root->left;
- insert(temp_root,data);
- }
- else if(data > temp_root->data && temp_root->right != NULL)
- {
- temp_root = temp_root->right;
- insert(temp_root,data);
- }
- else if(temp_root->left == NULL && data < temp_root->data)
- {
- struct Node *p_root = (struct Node*)malloc(sizeof(struct Node));
- p_root->data = data;
- p_root->right = NULL;
- p_root->left = NULL;
- temp_root->left = p_root;
- return;
- }
- else if(temp_root->right == NULL && data > temp_root->data)
- {
- struct Node *p_root = (struct Node*)malloc(sizeof(struct Node));
- p_root->data = data;
- p_root->right = NULL;
- p_root->left = NULL;
- temp_root->right = p_root;
- return;
- }
- }
- struct Node *find_minimum(struct Node* node)
- {
- struct node* current = node;
- while(node->left != NULL)
- node = node->left;
- return current;
- }
- struct Node* Delete(struct Node *root, int data)
- //void Delete(struct Node *root, int data )
- {
- if(root == NULL)
- return root;
- else if(data < root->data)
- root->left = Delete(root->left,data);
- else if (data > root->data)
- root->right = Delete(root->right,data);
- else
- {
- if(root->left == NULL && root->right == NULL)
- {
- free(root);
- root = NULL;
- }
- else if(root->left == NULL)
- {
- //struct Node *temp = root;
- free(root);
- root = root->right;
- }
- else if(root->right == NULL)
- {
- // struct Node *temp = root;
- free(root);
- root = root->left;
- }
- else
- {
- struct Node *temp = find_minimum(root->right);
- root->data = temp->data;
- root->right= Delete(root->right,temp->data);
- }
- //return root;
- }
- return root;
- }
- void preorder (struct Node *curr)
- {
- if(curr!=NULL)
- {
- printf(" %d ",curr->data);
- preorder(curr->left);
- preorder(curr->right);
- }
- }
- void inorder(struct Node *curr)
- {
- if(curr!=NULL)
- {
- inorder(curr->left);
- printf(" %d ", curr->data);
- inorder(curr->right);
- }
- }
- void postorder(struct Node *curr)
- {
- if(curr!=NULL)
- {
- postorder(curr->left);
- postorder(curr->right);
- printf(" %d ",curr->data);
- }
- }
- int main()
- {
- int choice,value;
- for(;;)
- {
- printf("\n\n\t\t---------------------\n");
- printf("\t\t: Binary Tree :\n");
- printf("\t\t---------------------\n");
- printf("\t\t: 1. Insert :\n");
- printf("\t\t: 2. Delete :\n");
- printf("\t\t: 3. Preorder :\n");
- printf("\t\t: 4. Postorder :\n");
- printf("\t\t: 5. Inorder :\n");
- printf("\t\t: 6. exit :\n");
- printf("\t\t---------------------\n");
- printf("\t\tEnter Choice: ");
- scanf("\t\t%d",&choice);
- switch (choice)
- {
- case 1 :
- printf("\t\tEnter a number: ");
- scanf("%d",&value);
- insert(root, value);
- break;
- case 2 :
- printf("\t\tEnter a number: ");
- scanf("%d",&value);
- Delete(root, value);
- break;
- case 3 :
- printf("\n Preorder:");
- preorder(root);
- break;
- case 4 :
- printf("\n Postorder:");
- postorder(root);
- break;
- case 5 :
- printf("\n Inorder:");
- inorder(root);
- break;
- case 6 :
- return EXIT_SUCCESS;
- default:
- printf("\t\tEnter only 1 to 6");
- break;
- }
- }
- }
Add Comment
Please, Sign In to add comment