|
From: chromatonus 7. <chr...@ho...> - 2002-02-13 21:29:13
|
Hi, I don't know why my screen is not displaying anything after i supposedly
input a tree and executed the display commands. Would you please examine my
program and see where it's wrong? Thank you!
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
struct node
{
int data;
node *left, *right;
};
//PROTOTYPES
int menu();
//postcondition: creates and displays a menu, returning the value of
choice
void insert_node(node *&, node *);
void pre( node* );
void in( node* );
void post( node* );
int menu()
{
int choice;
cout<< "TREE EDITOR MENU OPTIONS" << "\n";
cout<< "1. Insert elements in Tree" << "\n";
cout<< "2. Display Tree in Prefix" << "\n";
cout<< "3. Display Tree in Infix" << "\n";
cout<< "4. Display Tree in Postfix" << "\n";
cout<< "5. Exit";
gotoxy(1,8);
cout<< "choice? ";
cin>> choice;
return(choice);
}
// Main Program
int main()
{
node *root = NULL;
node *stem = new node;
int choice, info;
choice = menu();
while( choice != 5 )
{
switch( choice )
{
case 1: gotoxy(1,10);
cout<< "input? ";
cin>> info;
stem->data = info;
insert_node(root, stem);
break;
case 2: gotoxy(1,10);
pre( root );
break;
case 3: gotoxy(1,10);
in( root );
break;
case 4: gotoxy(1,10);
post( root );
break;
}
gotoxy(1,8);
cout<< "choice? ";
cin>> choice;
}
}
void insert_node(node *&root, node *stem)
{
if(root == NULL)
{
stem = root;
}
else
{
if(stem->data < root->data)
insert_node(root->left, stem);
else
insert_node(root->right, stem);
}
}
void pre(node *root)
{
if( root != NULL )
{
cout<< root->data <<" ";
pre( root->left );
pre( root->right );
}
}
void in(node *root)
{
if( root != NULL )
{
in(root->left);
cout<< root->data <<" ";
in(root->right);
}
}
void post(node *root)
{
if(root != NULL)
{
post(root->left);
post(root->right);
cout<< root->data <<" ";
}
}
_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com
|