Need help (inorder-preorder-postorder)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mnf7
    New Member
    • Jun 2014
    • 3

    #1

    Need help (inorder-preorder-postorder)

    My program doesn't work properly. Please help me



    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <conio.h>
    #include <stdio.h>
    #include <string.h>
    using namespace std;
    
    class node{
    	public:
    		node *child_left;
    		node *child_right;
    		char value [11];
    };
    node *reshe = NULL;	
    //#######################################################
    void insert (node *c)
    {
    	node *a;
    	node *b;
    	b = NULL;
    	a = reshe;
    
    	while ( a!=NULL)
    {
    	b = a;
    	if(strcmp(c->value,a->value) < 0)
    {
    	a= a-> child_left; 
    }
    	else
    {
    	a= a-> child_right; 
    }
    }
    	if( b==NULL )
    {
    	reshe=c;
    }
    	else if(strcmp( c->value,b->value) < 0)
    {
    	b -> child_left = c;
    }
    	else
    {
    	b-> child_right = c;
    }
    }
    //#######################################################
    void preorder(node *r){
        if( r!=NULL)
    	{
            cout<<"   "<< r->value;
            preorder(r->child_left);
            preorder(r->child_right);
    	} 
    }
    //#######################################################
    void inorder(node* r){
    	if( r!=NULL)
    	{
    		inorder(r->child_left);
    		cout << r->value << " " ;
    		inorder(r->child_right);
    	}
    }
    //########################################################
    void postorder(node* r){
    	if( r!=NULL)
    	{
    		inorder(r->child_left);
    		inorder(r->child_right);
    		cout << r->value << " " ;
    
    	}
    }
    //#######################################################
    void main()
    {
    	int choice=0 ;
    	while (choice!=3){
    	system("cls");
    	cout<<"\n\n\t 1. Insert a Expression.";
    	cout<<"\n\n\t 2. peymayesh.";
    	cout<<"\n\n\t 3. Exit.";
    	cout<<"\n\n\t Enter your choice : ";
    	cin>>choice;
    
    	switch (choice)
    {
    	case 1:{ system("cls");
    			node *c=new node;
    			cout<<"\n\n\t Enter your Expression Trees: ";
    			cin >> c->value;
    			c->child_right=NULL; 
    			c->child_left=NULL; 
    			insert(c); 
    		   }
    			break;
    	case 2:{ system("cls");
    			cout << "\n\n\t  peymayesh Preorder : ";
    			preorder(reshe);
    			cout << "\n\n\t  peymayesh Inorder : ";
    			inorder(reshe);
    			cout << "\n\n\t  peymayesh Postorder : ";
    			postorder(reshe);
    			getchar();
    			getchar();
    		   }
    			break;
    	case 3: break;
    }
    }
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    There are too many bugs in the code to address here. I suggest you step through the code using your debugger.

    The obvious bugs are:
    1) in the insert function the argument is a node* c. However, c is not used in the search while loop. What is used is a node* a that has never been initialized.

    2)In the inorder, preorder, and postorder functions a recursive call is made if the argument node* is not NULL. However, if it is NULL, the functions just quit without doing anything.

    Comment

    Working...