Binary Tree Implementation in C with Various Operations, Exercises of Data Structures and Algorithms

The implementation of a binary tree in c language with various operations such as insertion, pre-order, in-order, post-order, minimum, maximum, and mean. The user can interactively choose the operation and input the data to be processed.

Typology: Exercises

2011/2012

Uploaded on 07/30/2012

dhanvantari
dhanvantari ๐Ÿ‡ฎ๐Ÿ‡ณ

2

(2)

45 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#include<stdio.h>
# include<conio.h>
#include<iostream.h>
#include<string.h>
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;
void main ()
{
struct tree *current;
int choice=0,ch;
root=NULL;
while (choice!=6)
{
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; sum=sum+current->num; count++;
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;
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Binary Tree Implementation in C with Various Operations and more Exercises Data Structures and Algorithms in PDF only on Docsity!

#include

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; void main () { struct tree *current; int choice=0,ch; root=NULL; while (choice!=6) { 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; sum=sum+current->num; count++; 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 median (void) { int queue[100],len,top=-1,stack[100],stop=-1,input,popout; char choice='y'; clrscr();

while(choice=='y') { cout<<"\nEnter the number of pages you want to print:\t"; /The person with least number of pages will be given the highest priority / cin>>input; 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]98 && in>*st)