# A simple Tree class problem.

• 08-14-2007
msp
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; }```
• 08-14-2007
jwenting
without knowing what you expect it to do, no.
• 08-14-2007
Bench82
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.
• 08-14-2007
msp
Quote:

Originally Posted by jwenting
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!
• 08-19-2007
msp
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; }```