a) Write a C program which uses Binary search tree library and implements following two functions:
int sumodd(T) – returnssum of all odd numbers from BST
intsumeven(T) – returnssum of all even numbers from BST
mirror(T) – converts given tree into its mirror image.
Solution:
#include<stdio.h>
#include<malloc.h>
#include"bst.h"
int oddSum(struct node *temp)
{
int sum;
sum =0;
if(temp == NULL)
{
return 0;
}
else if(temp->data%2==1)
{
sum += temp->data;
}
return sum+oddSum(temp->left)+oddSum(temp->right);
}
int evenSum(struct node *temp)
{
int sum;
sum =0;
if(temp == NULL)
{
return 0;
}
else if(temp->data%2==0)
{
sum += temp->data;
}
return sum+evenSum(temp->left)+evenSum(temp->right);
}
struct node* mirror(struct node* root)
{
if (root==NULL)
return;
else
{
struct node* temp;
/* do the subtrees */
mirror(root->left);
mirror(root->right);
/* swap the pointers in this node */
temp = root->left;
root->left = root->right;
root->right = temp;
}
return(root);
}
void main()
{
struct node *root,*miror;
int ch,sum, val;
root=NULL;
while(1)
{
printf("\nBST OPERATIONS ---");
printf("\n1 - Create BST\n");
printf("2 - Odd Sum\n");
printf("3 - Even Sum BST\n");
printf("4 - Mirror of BST\n");
printf("5 - Inorder Traversal\n");
printf("6 - Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
while(1)
{
printf("\nEnter The data");
scanf("%d", &val);
if(val==0)
break;
else
{
root=create(root, val);
}
}
break;
case 2:
sum=oddSum(root);
printf("\nSum of all odd numbers from BST:\t%d\n",sum);
break;
case 3:
sum=evenSum(root);
printf("\nSum of all even numbers from BST:\t%d\n",sum);
break;
case 4:
printf("\nGiven Tree is \n");
inorder(root);
miror=mirror(root);
printf("\nMirror of The Given Tree is \n");
inorder(miror);
break;
case 5:
printf("\nGiven Tree is \n");
inorder(root);
break;
case 6:
exit(0);
default :
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
Library Function ( .h file )
NOTE: save file name as ' btree.h'.
NOTE: save file name as ' btree.h'.
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *left, *right;
};
struct node *create(struct node *root, int x)
{
if(root==NULL)
{
root=(struct node*)malloc(sizeof(struct node));
root->data=x;
root->left=root->right=NULL;
return(root);
}
else if(x<root->data)
{
root->left=create(root->left, x);
}
else
root->right=create(root->right, x);
return(root);
}
struct node *insert(struct node *root, int key) {
// Return a new node if the tree is empty
if (root == NULL)
{
root=(struct node*)malloc(sizeof(struct node));
root->data=key;
root->left=root->right=NULL;
return(root);
}
// Traverse to the right place and insert the node
if (key < root->data)
root->left = insert(root->left, key);
else
root->right = insert(root->right, key);
return root;
}
void preorder(struct node *root)
{
if(root!=NULL)
{
printf(" %d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct node *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf(" %d ", root->data);
}
}
void inorder(struct node *root)
{
if(root!=NULL)
{
inorder(root->left);
printf(" %d ", root->data);
inorder(root->right);
}
}
struct node *searchBST(struct node *root, int data)
{
if(root==NULL)
{
return(NULL);
}
if(root->data==data)
{
return(root);
}
else if(data<root->data)
return(searchBST(root->left,data));
else
return(searchBST(root->right,data));
}
#include<malloc.h>
struct node
{
int data;
struct node *left, *right;
};
struct node *create(struct node *root, int x)
{
if(root==NULL)
{
root=(struct node*)malloc(sizeof(struct node));
root->data=x;
root->left=root->right=NULL;
return(root);
}
else if(x<root->data)
{
root->left=create(root->left, x);
}
else
root->right=create(root->right, x);
return(root);
}
struct node *insert(struct node *root, int key) {
// Return a new node if the tree is empty
if (root == NULL)
{
root=(struct node*)malloc(sizeof(struct node));
root->data=key;
root->left=root->right=NULL;
return(root);
}
// Traverse to the right place and insert the node
if (key < root->data)
root->left = insert(root->left, key);
else
root->right = insert(root->right, key);
return root;
}
void preorder(struct node *root)
{
if(root!=NULL)
{
printf(" %d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct node *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf(" %d ", root->data);
}
}
void inorder(struct node *root)
{
if(root!=NULL)
{
inorder(root->left);
printf(" %d ", root->data);
inorder(root->right);
}
}
struct node *searchBST(struct node *root, int data)
{
if(root==NULL)
{
return(NULL);
}
if(root->data==data)
{
return(root);
}
else if(data<root->data)
return(searchBST(root->left,data));
else
return(searchBST(root->right,data));
}
0 Comments:
Post a Comment