# Binary tree problem

• 02-27-2003
carrja99
Binary tree problem
I've been trying to code my own binary tree (without looking at any examples), but for some reason my code just refuses to compile! here is my source:

Code:

```#include <iostream.h> struct node         {         node *right;         node *left;         int key;         }; typedef node* NodePtr; void add_node(NodePtr &tree, int value, int switch);         main()         {         int num, num_of_nodes;         NodePtr root, pointer; int called = 0;         cout << "How many numbers do you whish to add to the tree?";         cin >> num_of_nodes;         cout << "Enter the first node:";         cin >> num;         root->key = num;         pointer = root;         for (int i = 0; i < num_of_nodes; i++)                 {                 cout << "Enter a number to add:";                 cin >> num;                 add_node(pointer, num, called);                 pointer = root;                 called = 0;                 }                 return 0;         } void add_node(NodePtr &tree, int value, int switch)         {         if (switch == 0)                 {                 node* tmp = new node;                  tmp->key = value;                  switch = 1;                  }         if (value >= tree->key)                if (tree->right != NULL)                 {                 tree = tree->right;                 add_node(tree, value);                 }             else                 tree->right = tmp;         if (value <= tree->key)                if (tree->left != NULL)                 {                 tree = tree->left;                 add_node(tree, value);                 }             else                 tree->left = tmp;                                 }```
It all seems to check out fine, yet I get the following errors from g++:
Code:

```binary_tree.cpp:11: parse error before `switch' binary_tree.cpp:35: parse error before `switch' binary_tree.cpp: In function `void add_node(...)': binary_tree.cpp:37: parse error before `switch' binary_tree.cpp:40: `tmp' undeclared (first use this function) binary_tree.cpp:40: (Each undeclared identifier is reported only once for each   function it appears in.) binary_tree.cpp:40: `value' undeclared (first use this function) binary_tree.cpp:41: parse error before `=' token binary_tree.cpp: At global scope: binary_tree.cpp:47: `tree' was not declared in this scope binary_tree.cpp:47: ISO C++ forbids declaration of `add_node' with no type binary_tree.cpp:47: `int add_node' redeclared as different kind of symbol binary_tree.cpp:36: previous declaration of `void add_node(...)' binary_tree.cpp:47: initializer list being treated as compound expression binary_tree.cpp:48: parse error before `}' token binary_tree.cpp:56: `tree' was not declared in this scope binary_tree.cpp:56: ISO C++ forbids declaration of `add_node' with no type binary_tree.cpp:56: redefinition of `int add_node' binary_tree.cpp:47: `int add_node' previously defined here binary_tree.cpp:56: initializer list being treated as compound expression binary_tree.cpp:57: parse error before `}' token```
WHY!? :confused:
• 02-27-2003
jdinger
switch is a reserved keyword. That's like naming a variable if or else or return.
• 02-27-2003
carrja99
Quote:

Originally posted by jdinger
switch is a reserved keyword. That's like naming a variable if or else or return.
Silly me. I just wasn't paying attention!
• 02-27-2003
RoD
i'm not sure what tmp should be declared as so u have to fix that. Your add_node function takes 3 params, u only had the first two so i NULL'd the third, declare tmp and it should work i think.

Code:

```#include <iostream.h> struct node         {         node *right;         node *left;         int key;         }; typedef node* NodePtr; void add_node(NodePtr &tree, int value, int switch1);         main()         {         int num, num_of_nodes;         NodePtr root, pointer; int called = 0;         cout << "How many numbers do you whish to add to the tree?";         cin >> num_of_nodes;         cout << "Enter the first node:";         cin >> num;         root->key = num;         pointer = root;         for (int i = 0; i < num_of_nodes; i++)                 {                 cout << "Enter a number to add:";                 cin >> num;                 add_node(pointer, num, called);                 pointer = root;                 called = 0;                 }                 return 0;         } void add_node(NodePtr &tree, int value, int switch1)         {         if (switch1 == 0)                 {                 node* tmp = new node;                  tmp->key = value;                  switch1 = 1;                  }         if (value >= tree->key)                if (tree->right != NULL)                 {                 tree = tree->right;                 add_node(tree, value, NULL);                 }             else                 tree->right = tmp;         if (value <= tree->key)                if (tree->left != NULL)                 {                 tree = tree->left;                 add_node(tree, value,NULL);                 }             else                 tree->left = tmp;                                 }```
• 02-27-2003
carrja99
Ok... I got the following program worked out... works nicely too, IMHO.

Code:

```#include <iostream> using namespace std; struct node         {         node *right;         node *left;         int key;         }; typedef node* NodePtr; void add_node(NodePtr &tree, NodePtr &tmp); void print_inorder(NodePtr p)         {             if (p != NULL)                 {                       print_inorder(p->left);  // print left subtree                                 cout << p->key << endl; // print this node                                 print_inorder(p->right); // print right subtree                     }         }         main()         {         int num, num_of_nodes;         NodePtr root, pointer;         root = new node;         pointer = new node;         cout << "How many numbers do you whish to add to the tree?";         cin >> num_of_nodes;         cout << "Enter the first node:";         cin >> num;         root->key = num;         pointer = root;         for (int i = 0; i < num_of_nodes-1; i++)                 {                 cout << "Enter a number to add:";                 cin >> num;                 node* tmp = new node;                  tmp->key = num;                 add_node(pointer, tmp);                 pointer = root;                 }         cout << "The numbers entered in order is:" << endl;         print_inorder(root);         return 0;         } void add_node(NodePtr &tree, NodePtr &tmp)         {         if (tmp->key >= tree->key)                if (tree->right != NULL)                 {                 tree = tree->right;                 add_node(tree, tmp);                 }             else                 tree->right = tmp;         if (tmp->key <= tree->key)                if (tree->left != NULL)                 {                 tree = tree->left;                 add_node(tree, tmp);                 }             else                 tree->left = tmp;                                 }```
Please telll me if there are some things I can improve on in this!

EDIT: oh.. as for tmp, I decided it only needed to be declared once, so I left it out of the function. The print function prints the numbers out in a sorted fashion, and I've tested a modification using a random number generator. For some odd reason, whenever I use 5 or more nodes, it hangs. Maybe its the way I use random numbers, but it couldbe that I have too many things running. Let me know if anyone has any ideas, and could point out other things I coudl do with this program.