Thread: A simple Tree class problem.

  1. #1
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31

    Unhappy A simple Tree class problem.

    Can someone help me finding that why this code is not working as expected?
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    struct Node
    {
            int item;
            Node *left;
            Node *right;
    };
    
    class Tree
    {
            Node *root;
    public:
            Tree()
            {
                    root = new Node;
                    root->left = 0;
                    root->right = 0;
                    root->item = 0;
            }
    
            void insert(int i) { ins(i, root); }
            void inorder() { in(root); }
    
    private:
            void ins(int i, Node *r)
            {
                    if (!r)
                    {
                            r = new Node;
                            r->left = r->right = 0;
                            r->item = i;
                    }
                    else
                    {
                            if (r->item > i) ins(i, r->left);
                            else if (r->item < i) ins(i, r->right);
                    }
            }
            
            void in(Node *r)
            {
                    if (r)
                    {
                            in(r->left);
                            cout << r->item << ' ';
                            in(r->right);
                    }
            }
    };
    
    int main()
    {
            Tree t;
            for (int n=1; n<=10; ++n)
            {
                    t.insert(rand() % 100);
            }
            t.inorder();
            return 0;
    }

  2. #2
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    without knowing what you expect it to do, no.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    You haven't modified any of your pointers to point to anything, you're just copying them around. Try passing them by reference, and see if that helps, ie
    Code:
            void ins(int i, Node* & r)
    Also, its a bit pointless creating a 'zero' node in the tree's constructor, since binary trees are recursive, all you're doing is adding an extra unnecessary jump. if the tree is empty, its better to set your root as a null pointer.
    Last edited by Bench82; 08-14-2007 at 06:56 AM.

  4. #4
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    Quote Originally Posted by jwenting View Post
    without knowing what you expect it to do, no.
    Well I just want it to insert items in tree and then print the inserted items.
    Quote Originally Posted by Bench82
    Also, its a bit pointless creating a 'zero' node in the tree's constructor
    Oops! I did not realize that. Thanks.

    Yeah I hope that reference to pointer may work. I'll test it and post.
    Thanks guys!

  5. #5
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    It works now! Thanks everyone!
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    struct Node
    {
            int item;
            Node *left;
            Node *right;
    };
    
    class Tree
    {
            Node *root;
    public:
            Tree()
            {
                    root = 0;
            }
    
            void insert(int i) { ins(i, &root); }
            void inorder() { in(root); }
    
    private:
            void ins(int i, Node **p)
            {
                    Node *r = *p;
                    if (!r)
                    {
                            r = new Node;
                            r->left = r->right = 0;
                            r->item = i;
                            *p = r;
                    }
                    else
                    {
                            if (r->item > i) ins(i, &r->left);
                            else if (r->item < i) ins(i, &r->right);
                    }
            }
            
            void in(Node *r)
            {
                    if (r)
                    {
                            in(r->left);
                            cout << r->item << ' ';
                            in(r->right);
                    }
            }
    };
    
    int main()
    {
            Tree t;
            for (int n=1; n<=100; ++n)
            {
                    t.insert(n);
            }
            t.inorder();
            return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. Binary Search Tree - object instantiation problem
    By patricio2626 in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2006, 02:11 AM
  3. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 06:24 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM