Thread: Problem for Binary Tree code , Help

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    2

    Problem for Binary Tree code , Help

    Code:
    The error seems like the root from the class Tree can't read in the class Tree_node
    and the root simulation follows , 
    follows are the error message:
    Tree_node in class Tree does not name a type 
    as follows : shows the root can't use 
    
    
    
    
    ////Binary Search Tree
    //
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    
    
    class Tree{
          Tree_node *root;
    
    
    };
    class Tree_node:public Tree{
    
    
    public:
            Tree_node(){root=NULL;};
            ~Tree_node(){delete [] root;};
            void insert(string name);
            void print_Name(Tree_node *tree);
    
    
    
    
    
    
    private:
            string name;
            Tree_node *left;
            Tree_node *right;
    };
    
    
    void Tree_node::insert(string new_name)
    {
            Tree_node* temp = new Tree_node;
            temp->name = new_name;
            temp->left = NULL;
            temp->right = NULL;
            Tree_node* parents;
    
    
            if (root==NULL) {
    
    
                    root  = temp;
            }
            else
            {
                    Tree_node* curnt;
                    curnt = root;
                    while(curnt)
                    {
                            parents = curnt;
                            if (temp->name>curnt->name)
                                    curnt = curnt->right;
                            else
                                    curnt = curnt->left;
    
    
                    }
                    if (temp->name>parents->name)
                            parents->right = temp;
                    else
                            parents->left=temp;
    
    
    
    
            }
    
    
    }
    
    
    void Tree_node::print_Name(Tree_node *tree)
    {
            if (tree==NULL) return;
    
    
            if (tree!=NULL)
            {
                    if(tree->left)
                    {
                    print_Name(tree->left);
                }
    
    
            else {
                    print_Name(tree->right);
    
    
                 }
    
    
    
    
            cout<<"  " << tree->name<<" ";
            }
    }
    
    
    
    
    int main(){
            Tree_node t;
            string tmp;
            char menu;
            while(true)
            {
                    cout<<"Followings are the Menu for Sort the Name\n";
                    cout<<"---------------------------------------------"<<endl;
                    cout<<"a. Insert the names."<<endl;
                    cout<<"b. Print out the names in Alphabetically order."<<endl;
                    cout<<endl<<endl;
                    cout<<"Enter the choice:   "<<endl;
    
    
                    switch(menu)
                    {
                    case 1:
                            cout<<"Enter name to be inserted:";
                            cin>>tmp;
                            t.insert(tmp);
                            break;
                    case 2:
                            cout<<"Alphabetically Order"<<endl;
                            t.print_Name(tmp);
                         break;
                    }
            }
    }
    Last edited by cuihu52; 01-03-2013 at 02:48 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    what about some explanation, errors you get etc?

    //yeah, the edit helped
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    2
    Quote Originally Posted by std10093 View Post
    what about some explanation, errors you get etc?

    //yeah, the edit helped

    Any thoughts of that ?
    I put the insert method inside of the Tree class

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Tree will have to be implemented further down, on a line past Tree_node. And since a Tree is implemented in terms of Tree_node I think you are meant to use Tree in the main() function. Tree_node is not meant to be the interface of your data structure.

    I do not think inheritance is necessary.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Lets start with fixing line 29. It has the wrong version of delete.
    delete[] matches new[] and delete matches new.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Hmm, looking at this in more detail, its a lot worse than that.
    You don't seem to understand polymorphism. You currently have it that every Tree_node is a Tree, but what you need to have is either Tree_node and Tree are totally separate classes, or it could be the case that Tree contains Tree_node.
    Then think about which methods belong in which class.

    You're not going to get anything at all working until you get that right.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary tree problem
    By moddinati in forum C++ Programming
    Replies: 8
    Last Post: 06-19-2008, 05:05 PM
  2. Binary Tree Problem
    By noodle24 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2007, 02:20 PM
  3. binary tree problem
    By spank in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:27 AM
  4. Binary tree problem
    By carrja99 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 09:36 PM
  5. where can I find source code for a binary tree?
    By binary_man in forum C++ Programming
    Replies: 5
    Last Post: 01-10-2003, 09:53 AM