Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Write a menu-driven program to display the number of nodes, height of the tree, maximum and minimum values, leaf and non-leaf nodes, sum of all nodes of the tree and odd & even values.*/
- #include <stdio.h>
- #include <stdlib.h>
- struct tree{
- int data;
- struct tree *left,*right;
- }*root = NULL;
- int totalCount, evenCount, oddCount;
- void insert(int value){
- struct tree *newNode = (struct tree*) malloc(sizeof(struct tree));
- newNode->data = value; newNode->right = newNode->left = NULL;
- if(!root) root = newNode;
- else{
- struct tree *help = root, *previous = NULL;
- while(help){
- previous = help;
- if(value > help->data) help = help ->right;
- else help = help->left;
- }
- if(value > previous->data) previous->right = newNode;
- else previous->left = newNode;
- }
- }
- void count(struct tree* help){
- if(help){
- count(help->right);
- totalCount++;
- count(help->left);
- }
- }
- int height(struct tree *help){
- int leftHeight, rightHeight;
- if(!help) return 0;
- leftHeight = height(help->left);
- rightHeight = height(help->right);
- if(leftHeight > rightHeight) return leftHeight+1;
- else return rightHeight+1;
- }
- int largest(struct tree* help){
- if(!help) return 0;
- else if (!help-> right) return help->data;
- else return largest(help->right);
- }
- int smallest(struct tree* help){
- if(!help) return 0;
- else if (!help-> left) return help->data;
- else return smallest(help->left);
- }
- int leaf(struct tree *help){
- if(help){
- if(!help->right && !help->left){
- printf("%d ", help-> data);
- return 1;
- }
- else return leaf(help->left) + leaf(help->right);
- }
- else return 0;
- }
- int nonleaf(struct tree *help){
- if(help){
- if(help->right || help->left){
- printf("%d ", help-> data);
- return 1 + nonleaf(help->left) + nonleaf(help->right);
- }
- }
- else return 0;
- }
- int sum(struct tree* help){
- if(help)
- return help->data + sum(help->right) + sum(help->left);
- else return 0;
- }
- void odd(struct tree *help){
- if(help){
- odd(help->right);
- if(help->data % 2 == 1 || help->data % 2 == -1){
- oddCount++;
- printf("%d ", help->data);
- }
- odd(help->left);
- }
- }
- void even(struct tree *help){
- if(help){
- even(help->right);
- if(help->data % 2 == 0){
- evenCount++;
- printf("%d ", help->data);
- }
- even(help->left);
- }
- }
- int main(){
- int n;
- up: printf("\nEnter nodes, input -1 to stop.\n");
- while(1){
- printf("\nInput: ");
- scanf("%d",&n);
- if(n==-1) break;
- insert(n);
- }
- printf("\n1.Total 2.Height 3.Max 4.Min 5.Leaf 6.Non-Leaf 7.Sum 8.Odd 9.Even 10.Exit 11.Insert\n");
- while(1){
- printf("\n\nSelect an option: ");
- scanf("%d",&n);
- switch(n){
- case 1: totalCount = 0; count(root); printf("\nThe number of nodes: %d.", totalCount);break;
- case 2: printf("\nThe height of the tree is %d.", height(root)-1); break;
- case 3: printf("The maximum value is %d.", largest(root)); break;
- case 4: printf("The minimum value is %d.", smallest(root)); break;
- case 5: printf("\n%d leaf node(s) found: ", leaf(root)); break;
- case 6: printf("\n%d non-leaf node(s) found.", nonleaf(root)); break;
- case 7: printf("\n The sum of all the elements in the tree is %d.",sum(root)); break;
- case 8: odd(root); printf("\n%d odd node(s) found.", oddCount); break;//
- case 9: even(root); printf("\n%d even node(s) found.", evenCount); break;
- case 10:return 0;
- case 11: goto up;
- default: printf("\nEnter a number in between 1 and 12.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement