this is my program could somebody help plz

#include <iostream.h>
#include <string>

using namespace std;

typedef int ComponentType;

struct CropType
{
        string crop;
        string farmer;
        string color;
};

CropType myCrop;

struct TreeNode
{
        CropType info;
        TreeNode* left;
        TreeNode* right;
};

void Insert ( /* in */ ComponentType item);
void Inorder (TreeNode* root);;

int main()
{
        string command;

        ComponentType item;

        TreeNode* root;
        root = new TreeNode;

   do
   {
    cout << "   Please Enter a command"    << endl;
    cout << "   Insert To Enter new data"    << endl;
    cout << "   Find To Search the Tree"    << endl;
    cout << "   Remove To delete data"    << endl;
    cout << "   Dump1  For Inorder Traversal   " <<endl;
    cout << "   Dump2  For Preorder Traversal   " <<endl;
    cout << "   Dump3  For Postorder Traversal   " <<endl;
    cout << "   Quit To exit"    << endl;

    cin >> command;

         if (command == "Insert")
         {
                cout << "Please Enter a Crop" << endl;
                cin >> myCrop.crop;

                cout << "Please Enter a Farmer" << endl;
                cin >> myCrop.farmer;

                cout << "Please Enter an Order" << endl;
                cin >> myCrop.order;

                cout << "Please Enter a Color" << endl;
                cin >> myCrop.color;

                Insert();
         }

                        Insert (root,info);

      }while (command != "Quit");

      cout << "This is your info" << " " << Inorder(root) << endl;

      return 0;
}


void Insert(TreeNode*& root, CropType &item)
{
  if (root==NULL)
  {
      root = new TreeNode;
      root->left = NULL;
      root->right = NULL;
      root->info = item;
  }
  else if (item < root->info)
  {
                Insert(root->left, item);
  }
  else
  {
                Insert(root->right, item);
  }
}

void Inorder (TreeNode* root)
{
   if(root != NULL)
   {
      Inorder(root->left);
      cout << root->info;
      Inorder(root->right);
   }
}

Recommended Answers

All 5 Replies

1. Initialize an empty tree:

TreeNode* root = 0;

You initialize root with new TreeNode which contains garbage (uninitialized) pointers.
2. Discard a strange call of Insert(). No such function in your source.
3. Think about a right place for Insert (root,info) call.

1. Initialize an empty tree:

TreeNode* root = 0;

You initialize root with new TreeNode which contains garbage (uninitialized) pointers.
2. Discard a strange call of Insert(). No such function in your source.
3. Think about a right place for Insert (root,info) call.

thanx for the help.

i correct the errors but i'm still getting errors with:

Insert (root,info);

root->info = item;

else if (item < root->info)

cout << root->info;

Well post in your code again.

And secondly. I dont see the variable "info" initialized in the code. Just check that out.

Well post in your code again.

And secondly. I dont see the variable "info" initialized in the code. Just check that out.

#include <iostream.h>
#include <string>

using namespace std;

typedef int ComponentType;

struct CropType
{
        string crop;
        string farmer;
        string color;
        string order;
};

CropType myCrop;

struct TreeNode
{
        CropType info;
        TreeNode* left;
        TreeNode* right;
};

void Insert ( /* in */ ComponentType item);
void Inorder (TreeNode* root);;

int main()
{
        string command;

        ComponentType item;

        TreeNode* root = 0;

   do
   {
    cout << "   Please Enter a command"    << endl;
    cout << "   Insert To Enter new data"    << endl;
    cout << "   Find To Search the Tree"    << endl;
    cout << "   Remove To delete data"    << endl;
    cout << "   Dump1  For Inorder Traversal   " <<endl;
    cout << "   Dump2  For Preorder Traversal   " <<endl;
    cout << "   Dump3  For Postorder Traversal   " <<endl;
    cout << "   Quit To exit"    << endl;

    cin >> command;

         if (command == "Insert")
         {
                cout << "Please Enter a Crop" << endl;
                cin >> myCrop.crop;

                cout << "Please Enter a Farmer" << endl;
                cin >> myCrop.farmer;

                cout << "Please Enter an Order" << endl;
                cin >> myCrop.order;

                cout << "Please Enter a Color" << endl;
                cin >> myCrop.color;
         }

                        Insert (root,info);

      }while (command != "Quit");

      cout << "This is your info" << " " << Inorder(root) << endl;

      return 0;
}


void Insert(TreeNode*& root, CropType &item)
{
  if (root==NULL)
  {
      root = new TreeNode;
      root->left = NULL;
      root->right = NULL;
      root->info = item;
  }
  else if (item < root->info)
  {
                Insert(root->left, item);
  }
  else
  {
                Insert(root->right, item);
  }
}

void Inorder (TreeNode* root)
{
   if(root != NULL)
   {
      Inorder(root->left);
      cout << root->info;
      Inorder(root->right);
   }
}

i just want to know how to insert the whole struct(the crop, farmer, color, and order) insert into the binary tree as one node and what does the header of the function look like.

>what does the header of the function look like.
You wrote this header (see Insert function in your code). May be it's not your code? That's the Insert function prototype (use copy/paste high technology approach):

void Insert(TreeNode*& root, CropType &item);

Why you ignore points #2 and #3 in my previous post?
>i just want to know how to insert the whole struct
Evidently: Insert(tree,myCrop); Apropos, why this variable is global?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.