# Thread: Binary sort number arranging tree

1. ## Binary sort number arranging tree

I am having trouble with this. I believe I have found where the problem is with the random couts in the functions. When a node is added it keeps getting added to root node. It won't become a new node. What am I doing wrong? Thanks for any help offered.

Code:
```#include <iostream>

using namespace std;

class Node
{
public:
Node(int v);
~Node();

int value;

Node* left;
Node *right;
};

Node::Node(int v)
{
value = v;
left = NULL;
right = NULL;
}

Node::~Node() {}

class BinaryTree
{
public:
BinaryTree();
~BinaryTree();

void display();

void clear();

private:
Node* root;

void removeNode(Node* n);
void displayValue(Node* n);
void insertNode(Node* n, int v);
};

BinaryTree::BinaryTree()
{
root = NULL;
}

BinaryTree::~BinaryTree()
{
removeNode(root);
}

void BinaryTree::removeNode(Node* n)
{
if (n)
{
removeNode(n->left);
removeNode(n->right);
delete n;
}
}

void BinaryTree::clear()
{
removeNode(root);
}

void BinaryTree::display()
{
cout << "Values: ";
displayValue(root);
cout << endl;
}

void BinaryTree::displayValue(Node* n)
{
if (n)
{
if (n->left)
displayValue(n->left);
if (n->right)
displayValue(n->right);

cout << n->value << ", ";
}
}

{
if (root)
cout << "root is not null" <<endl;
else
cout << "root is null" << endl;

insertNode(root, i);
}

void BinaryTree::insertNode(Node* n, int v)
{
if (n)
{
if (v < n->value)
insertNode(n->left, v);
else
insertNode(n->right, v);

cout << "branch added" << endl;
}
else
{
n = new Node(v);
cout << "leaf added" << endl;
}
}

int main()
{
BinaryTree Tree;

int response = 0;
int value;

while (response != 4)
{
cout << "1. Add A Number." << endl;
cout << "2. Display Values." << endl;
cout << "3. Clear Tree." << endl;
cout << "4. Exit." << endl;
cin >> response;

if (response == 1)
{
cout << "Value to add: ";
cin >> value;
}
else if (response == 2)
{
Tree.display();
}
else if (response == 3)
{
Tree.clear();
}

cout << " ----------------------------- " << endl;

}
cin.get();
return 0;
}``` 2. insertion seems to be OK
problem with displaying
Code:
```void BinaryTree::displayValue(Node* n)
{
if (n)
{
if (n->left)
displayValue(n->left);
if (n->right)
displayValue(n->right);

cout << n->value << ", ";
}
}```
Code:
`        cout << n->value << ", ";`
should be outputted before right node 3. I don't think displaying is the probem, inserting is.

When root is passed in "insertNode(Node* n, int v);" as insertNode(root, i) the new keyword works only for the prototype "Node* n" but root isn't being set as Node *n. I am sorry for my ignorance with pointers and passing them in functions. I am new with them.

Code:
```void BinaryTree::add(int i)
{
if (root)
cout << "root is not null" <<endl;
else
cout << "root is null" << endl;

insertNode(root, i);
}

void BinaryTree::insertNode(Node* n, int v)
{
if (n)
{
if (v < n->value)
insertNode(n->left, v);
else
insertNode(n->right, v);

cout << "branch added" << endl;
}
else
{
n = new Node(v);
cout << "leaf added" << endl;
}
}``` 4. void BinaryTree::insertNode(Node*& n, int v)

the *& was what the problem was. I am still unsure of what it exactly means.

Also I am not sure if I am deleting all the nodes correctly. Would I also make the remove function be void removeNode(Node*& n); ?

Here is the updated code, it seems to work but this is not what I am doing with trees. I am planning to use binary trees for a way more complex problem, this is just practice. Thanks for all the help provided so far.

Code:
```#include <iostream>

using namespace std;

class Node
{
public:
Node(int v);
~Node();

int value;

Node* left;
Node* right;
};

Node::Node(int v)
{
value = v;
left = NULL;
right = NULL;
}

Node::~Node() {}

class BinaryTree
{
public:
BinaryTree();
~BinaryTree();

void display();

void clear();

private:
Node* root;

void removeNode(Node* n);
void displayValue(Node* n);
void insertNode(Node*& n, int v);
};

BinaryTree::BinaryTree()
{
root = NULL;
}

BinaryTree::~BinaryTree()
{
removeNode(root);
}

void BinaryTree::removeNode(Node* n)
{
if (n)
{
removeNode(n->left);
removeNode(n->right);
delete n;
n = NULL;
}
}

void BinaryTree::clear()
{
removeNode(root);
}

void BinaryTree::display()
{
cout << "Values: ";
displayValue(root);
cout << endl;
}

void BinaryTree::displayValue(Node* n)
{
if (n)
{
if (n->left)
displayValue(n->left);

cout << n->value << ", ";

if (n->right)
displayValue(n->right);
}
}

{
insertNode(root, i);
}

void BinaryTree::insertNode(Node*& n, int v)
{
if (n)
{
if (v < n->value)
insertNode(n->left, v);
else
insertNode(n->right, v);

}
else
{
n = new Node(v);
}
}

int main()
{
BinaryTree Tree;

int response = 0;
int value;

while (response != 4)
{
cout << "1. Add A Number." << endl;
cout << "2. Display Values." << endl;
cout << "3. Clear Tree." << endl;
cout << "4. Exit." << endl;
cin >> response;

if (response == 1)
{
cout << "Value to add: ";
cin >> value;
}
else if (response == 2)
{
Tree.display();
}
else if (response == 3)
{
Tree.clear();
}

cout << " ----------------------------- " << endl;
}
cin.get();
return 0;
}``` Popular pages Recent additions 