|
From: Ioannis V. <no...@ya...> - 2002-02-14 02:41:27
|
Except of what Jason said, try to make the recursive functions
non-recursive using loops. Recursion has both large time and space cost
with the last placing limits at the capacity of your tree since
dos/console only allows 64 kb of storage in the stack. Always (try to)
make the recursive functions non-recursive by using loop (e.g. for) and
placing the condition of the recursion ending as the condition of the
loop ending.
e.g.
int addall(int n)
{
if(n<0)
return 0;
int count=0;
count+=n;
if(--n)
count+=addall(n);
return count;
}
becones the time/space efficient:
int addall(int n)
{
int count=0;
for(int i=1; i<=n; i++)
count+=i;
return count;
}
Doing this you will not be able to think again in recursive mode (which
is a good thing!) contrary to what smart-ass-but-know-nothing think. :)
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.d2g.com
* Alternative URL: http://run.to/noicys
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of chromatonus 712
> Sent: Wednesday, February 13, 2002 11:29 PM
> To: dev...@li...
> Subject: [Dev-C++] Binary Tree display
>
>
> 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
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|