



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A c program implementing a binary tree data structure and providing various operations such as insertion, pre-order, in-order, and post-order traversal, minimum, maximum, and mean calculation. The user can interactively choose the desired operation.
Typology: Exercises
1 / 5
This page cannot be seen from the preview
Don't miss anything!




#include
#include #include struct tree { int num; struct tree *right, *left; }; void input (void); void insert (struct tree **, struct tree *); void pre_order (struct tree *); void in_order (struct tree *); void post_order (struct tree *); void minimum (void); void maximum (void); void search (struct tree *,int ); void delete_key (struct tree ,int ); void mean (struct tree); void mode (void);
struct tree *root; int sum=0,count=0; void main () { struct tree *current; int choice=0,ch; root=NULL; while (choice!=5) { clrscr(); cout<<"\n\t\t\t\n\nEnter one of the following options:\n\n\t\t- To Insert Data press:\t\t\t\t1\n\t\t- To display the whole data press:\t\t2\n\t\t- To find minimum:\t\t\t\t3\n\t\t- To find maximum:\t\t\t\t4\n\t\t- To find mean:\t\t\t\t\t5\n\t\t- To exit press:\t\t\t\t\t6\n\nEnter Choice:\t"; cin>>choice; switch (choice) { case 1: current=new tree; current->left=NULL; current->right=NULL; cout<<"\n\n\nEnter the input number:\t"; cin>>current->num; mean(current); insert(&root,current); break;
case 2: clrscr(); cout<<"\n\nChoose one of the following for display:\n\n\t\t For Pre order press:\t\t1\n\t\t For In order press:\t\t2\n\t\t For Post order press:\t\t3\n\n"; cin>>ch; switch (ch)
case 1: if (root==NULL) { cout<<"\n\nThere is no data in the tree\n"; getch(); } else { pre_order(root); getch(); } break;
case 2: if (root==NULL) { cout<<"\n\nThere is no data in the tree\n"; getch(); } else { in_order(root); getch(); } break;
case 3: if (root==NULL) { cout<<"\n\nThere is no data in the tree\n"; getch(); } else { post_order(root); getch(); } break; } break;
case 3: if (root==NULL) { cout<<"\n\nThere is no data in the tree\n"; getch(); } else { minimum(); getch(); } break;
case 4: if (root==NULL) {
void pre_order (struct tree *a) { if (a!=NULL) { cout<<"\n\tThe data:\t"<num<<"\n\n"; pre_order(a->left); pre_order(a->right); } }
void in_order (struct tree *b) {
if (b!=NULL) { in_order(b->left); cout<<"\n\tThe data:\t"<num<<"\n\n"; in_order(b->right); } }
void post_order (struct tree *c) { if (c!=NULL) { post_order(c->left); post_order(c->right); cout<<"\n\tThe data:\t"<num<<"\n\n"; } }
void minimum (void) { struct tree *p; p=root; while (p->left!=NULL) { p=p->left; } cout<<"\n\nThe minimum value in the tree is:\t"<num;
}
void maximum (void) { struct tree *p; p=root; while (p->right!=NULL) { p=p->right; } cout<<"\n\nThe maximum value in the tree is:\t"<num; }
/*void search (struct tree *p,int ser) { if(p==NULL) { cout<<"\nThere is no data in the list"; } if (p->num==ser) { delete_key(p,ser); } if (p->num < ser) { p=p->right; search(p,ser); } if (p->num > ser) { p=p->left; search(p,ser); } } */
void mean (struct tree p) { sum=sum+current->num; count++; } / delete_key(struct tree *p, int ser) {
}*/