



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
The implementation of a binary tree in c language with various operations such as preorder, inorder, postorder traversal, minimum, maximum, median, insert, delete and search. The user can interactively choose the operation and enter the data to be processed.
Typology: Exercises
1 / 7
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 median (void); void push (int,int,int); int pop (int,int);
struct tree *root; int sum=0,count=0,input,top=-1; void main () { struct tree *current; int choice=0,ch; root=NULL; while (choice!=7) { 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 find median:\t\t\t\t\t6\n\t\t- To exit press:\t\t\t\t\t7\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; sum=sum+current->num; count++; input=current->num; 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:
else if ((rot)->num > current->num) { insert(&(rot)->left,current); } } 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 median (void) { int queue[100],len,stack[100],stop=-1,popout,x; if(top==-1) /For the first entry/ { top=0; queue[0]=input; } if (queue[top]>input) /For entry having less number of pages as compared to the highest number of pages in the queue/ { while(queue[top]>input && top>-1) { popout=pop(&queue[top],&top); /Moves higher number of pages than the current entry to stack such that elements in stack settels in ascending order, from top to bottom/ push(&stack[stop],&stop,popout); top--; } top=top+1; queue[top]=input; /Enters curent entry at its appropriate position/
while(stop>-1) { popout=pop(&stack[stop],&top);stop--; /Pushes back the values from stack into the queue such that they automaticaly settels in ascending order in the queue/ push(&queue[top],&top,popout); } }
if(queue[top] //sum=sum+current->num; count++; //} /* delete_key(struct tree *p, int ser) {
}*/