Binary Search Tree Operations
- #include<stdio.h>
- #include<stdlib.h>
- #include "tree.h"
- NODE * create(NODE *root);
- NODE * insert(NODE *root,int n);
- NODE *search(NODE * root, int n);
- void display();
- void preorder(NODE * root);
- void inorder(NODE * root);
- void postorder(NODE * root);
- NODE * deletebst(NODE *root, int n);
- int Node_count(NODE *root);
- int Leaf_count(NODE *root);
- NODE *treecopy(NODE *root);
- int comparebst(NODE *root, NODE *root1);
- int sumOdd(NODE *node);
- int sumEven(NODE *node1);
- void mirror(NODE *root);
- int smallest(NODE * root);
- int largest(NODE * root);
- void main()
- {
- NODE *root=NULL, *root1=NULL;
- int n, num, osum, esum, choice;
- printf("\t\t>>Operations of Binary Search Tree<<\n\n");
- printf("\t\t\t>>First Create BST<<\n");
- //CREATE
- root = create(root);
- root1 = create(root1);
- do
- {
- printf("\n\n1.INSERT");
- printf("\n2.SEARCH");
- printf("\n3.DISPLAY");
- printf("\n4.PERODER");
- printf("\n5.INODER");
- printf("\n6.POSTODER");
- printf("\n7.DELETE");
- printf("\n8.COUNT OF TOTAL NODES");
- printf("\n9.COUNT OF LEAF NODES");
- printf("\n10.COPY BST");
- printf("\n11.MIRROR OF BST");
- printf("\n12.SUM OF ODD NUMBERS");
- printf("\n13.SUM OF EVEN NUMBERS");
- printf("\n14.COMPARE BST");
- printf("\n15.SMALLEST NUMBER IN BST");
- printf("\n16.LARGEST NUMBER IN BST");
- printf("\n17.Exit\n");
- printf("\n Enter your Choice:\t");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1: //INSERT
- printf("\n\nEnter Element you want to insert:\t");
- scanf("%d",&n);
- root = insert(root,n);
- display(root);
- break;
- case 2: //SEARCH
- printf("Enter Element to be search:\t");
- scanf("%d", &n);
- search(root, n);
- break;
- case 3: //DISPLAY
- display(root);
- break;
- case 4: //PREORDER
- printf("\nPerorder traversal is:\t");
- preorder(root);
- break;
- case 5: //INORDER
- printf("\nInorder traversal is:\t");
- inorder(root);
- break;
- case 6: //POSTORDER
- printf("\nPostorder traversal is:\t");
- postorder(root);
- break;
- case 7: //DELETE
- printf("\n\nEnter Element you want to delete:\t");
- scanf("%d",&n);
- root = deletebst(root,n);
- display(root);
- break;
- case 8: //TOTAL NODE COUNT
- num=Node_count(root);
- printf("\nTotal Number of Nodes In BST:\t%d",num);
- break;
- case 9: //LEAF NODE
- num=Leaf_count(root);
- printf("\n Total Number of Leaf Nodes In BST:\t%d",num);
- break;
- case 10: //COPY BST
- treecopy(root);
- display(root);
- break;
- case 11: //MIRROR OF BST
- mirror(root);
- display(root);
- break;
- case 12: //SUM OF ODD NUMBERS
- osum=sumOdd(root);
- printf("\n Sum of odd numbers is :\t%d",osum);
- break;
- case 13: //SUM OF EVEN NUMBERS
- esum=sumEven(root);
- printf("\n Sum of even numbers is :\t%d",esum);
- break;
- case 14 : //COMPARE
- if(comparebst(root, root1))
- printf("\n\t>>BOTH TREE ARE EQUAL<<");
- else
- printf("\n\t>>BOTH TREE ARE NOT EQUAL<<");
- break;
- case 15: //SMALLEST ELEMENT IN BST
- printf("\n\n\t >>Smallest element in BST is:\t %d", smallest(root));
- break;
- case 16: //LARGEST ELEMENT IN BST
- printf("\n\t>>Largest element in BST is:\t %d", largest(root));
- break;
- case 17 : //EXIT
- printf("\t\t>>BYE<<");
- break;
- default: printf("\t>>ENTER RIGHT CHOICE<<");
- }
- }while(choice!=17);
- }
//Header File
- typedef struct node
- {
- int info;
- struct node *left, *right;
- }NODE;
- //CREATE
- NODE * create(NODE *root)
- {
- NODE *nn, *temp;
- int i, n, num;
- printf("\nHow many nodes you want to inserted...?\t ");
- scanf("%d",&n );
- for(i=1; i<=n; i++)
- {
- nn=(NODE*)malloc(sizeof(NODE));
- printf("Enter %d Element:\t",n);
- scanf("%d",&num);
- nn->info = num;
- nn->left=nn->right=NULL;
- if(root==NULL)
- root=nn;
- else
- {
- temp=root;
- while(1)
- {
- if(nn->info < temp->info)
- if(temp->left == NULL)
- {
- temp->left=nn;
- break;
- }
- else
- temp=temp->left;
- else
- if(temp->right == NULL)
- {
- temp->right=nn;
- break;
- }
- else
- temp=temp->right;
- }//while
- }//else
- }//for
- return(root);
- }
- //DISPLAY
- void display(NODE *root)
- {
- NODE *temp=root;
- if(temp != NULL)
- {
- printf("\t%d",temp->info);
- display(temp->left);
- display(temp->right);
- }
- }
- //INSERT
- NODE * insert(NODE *root, int n)
- {
- NODE *temp, *nn;
- nn = (NODE*)malloc(sizeof(NODE));
- nn->left=nn->right=NULL;
- nn->info=n;
- if(root==NULL)
- {
- root=nn;
- return root;
- }
- else
- {
- temp=root;
- while(1)
- {
- if(n < temp->info)
- {
- if(temp->left==NULL)
- {
- temp->left=nn;
- break;
- }
- else
- temp=temp->left;
- }
- else
- if(n > temp->info)
- {
- if(temp->right==NULL)
- {
- temp->right=nn;
- break;
- }
- else
- temp=temp->right;
- }//if
- }//while
- }//else
- return(root);
- }
- //PERODER
- void preorder(NODE * root)
- {
- NODE *temp = root;
- if(temp != NULL)
- {
- printf("%d\t", temp->info);
- preorder(temp->left);
- preorder(temp->right);
- }
- }
- //INORDER
- void inorder(NODE * root)
- {
- NODE *temp = root;
- if(temp != NULL)
- {
- inorder(temp->left);
- printf("%d\t", temp->info);
- inorder(temp->right);
- }
- }
- //POSTORDER
- void postorder(NODE * root)
- {
- NODE *temp = root;
- if(temp != NULL)
- {
- postorder(temp->left);
- postorder(temp->right);
- printf("%d\t", temp->info);
- }
- }
- //Delete
- NODE * deletebst(NODE *root, int n)
- {
- NODE * temp = root;
- NODE * prev = NULL;
- while(temp != NULL && temp->info != n)
- {
- prev = temp;
- if(n < temp->info)
- temp = temp->left;
- else
- temp = temp->right;
- }
- if(temp==NULL)
- {
- printf("\t'%d' Not Found In BST\n",n);
- return root;
- }
- if(temp->left == NULL || temp->right == NULL)
- {
- NODE *nn;
- if(temp->left == NULL)
- nn = temp->right;
- else
- nn = temp->left;
- if(prev == NULL)
- return nn;
- if(temp == prev->left)
- prev->left = nn;
- else
- prev->right = nn;
- free(temp);
- }
- else
- {
- NODE * p = NULL;
- NODE * temp1;
- temp1 = temp->right;
- while(temp1->left != NULL)
- {
- p = temp1;
- temp1 = temp1->left;
- }
- if(p != NULL)
- p->left = temp1->right;
- else
- temp->right = temp1->right;
- temp->info = temp1->info;
- free(temp1);
- }
- return root;
- }//fun
- //Search
- NODE *search(NODE * root, int n)
- {
- NODE *temp = root;
- while(temp != NULL)
- {
- if(temp->info==n)
- {
- printf("\t'%d' Is Present In BST", n);
- return temp;
- }
- if(n < temp->info)
- temp=temp->left;
- else
- temp=temp->right;
- }
- printf("\t>>'%d' IS Not Present In BST<<",n);
- }
- //Count Total Nodes
- int Node_count(NODE *root)
- {
- static int count=0;
- NODE *temp=root;
- if(temp != NULL)
- {
- count++;
- Node_count(temp->left);
- Node_count(temp->right);
- }
- return (count);
- }
- //Count Leaf Node
- int Leaf_count(NODE *root)
- {
- static int count=0;
- NODE *temp=root;
- if(temp != NULL)
- {
- if(temp->left==NULL && temp->right==NULL)
- count++;
- Leaf_count(temp->left);
- Leaf_count(temp->right);
- }
- return (count);
- }
- //COPY TREE
- NODE *treecopy(NODE *root)
- {
- NODE *nn;
- if(root != NULL)
- {
- nn=(NODE*)malloc(sizeof(NODE));
- nn->info=root->info;
- nn->left = treecopy(root->left);
- nn->right = treecopy(root->right);
- return (nn);
- }
- else
- return NULL;
- }
- //MIRROR
- void mirror(NODE *root)
- {
- NODE *temp;
- if(root != NULL)
- {
- if(root->left)
- mirror(root->left);
- if(root->right)
- mirror(root->left);
- temp=root->left;
- root->left = root->right;
- root->right = temp;
- }
- }
- //SUM OF ODD NUMBERS
- int sumOdd(NODE *node)
- {
- int osum=0;
- if(node != NULL)
- {
- if((node->info % 2 )!= 0)
- osum += node->info;
- osum += sumOdd(node->left);
- osum += sumOdd(node->right);
- }
- return osum;
- }
- //SUM OF EVEN NUMBERS
- int sumEven(NODE *node1)
- {
- int esum=0;
- if(node1 != NULL)
- {
- if((node1->info % 2 )== 0)
- esum += node1->info;
- esum += sumEven(node1->left);
- esum += sumEven(node1->right);
- }
- return esum;
- }
- //SMALLEST
- int smallest(NODE * root)
- {
- NODE *temp = root;
- while (temp->left != NULL)
- {
- temp = temp->left;
- }
- return(temp->info);
- }
- //LARGEST
- int largest(NODE * root)
- {
- NODE *temp = root;
- while (temp->right != NULL)
- {
- temp = temp->right;
- }
- return(temp->info);
- }
- //COMPARE
- int comparebst(NODE *root1, NODE *root2)
- {
- if(root1==NULL && root2==NULL)
- return 1;
- if(root1 != NULL && root2 != NULL)
- {
- return
- (
- root1->info == root2->info &&
- comparebst(root1->left, root2->left) &&
- comparebst(root1->right, root2->right)
- );
- }
- return 0;
- }
Comments
Post a Comment