It's probably really obvious, then again I don't know much about trees in c++. When I try to insert an integer into the tree it doesn't travel down the tree and insert the integer. I'll post the function and then the whole enchillada after. Also how can I create a second pointer to follow down the tree. Thanks for any help.

``````void node::insert(node* ptr)
{
node *temp;
int inf;
node *p, *q;
p = q = ptr;
cout<<"Enter a number to insert: ";
cin>>inf;
if (inf != -999)  {
temp = new node;
temp->info = inf;
temp->left = temp->right = NULL;
}
while( inf != p->info && q != NULL) {
p = q;
if (info< p->info)
q = p->left;
else
q = p->right;
if (inf == p->info)
cout<<"\nNumber "<<inf<<" is duplicate."<<endl;
else if (inf < p->info)
p->left = temp;
else
p->right = temp;
}
}``````

whole program follows:

``````#include <iostream>
using namespace std;

class node
{
public:
int info;
node* left;
node* right;
int maketree();
void intrav(node*);
void pretrav(node*);
void postrav(node*);
int countnode(node*);
void child(node*);
void insert(node*);
void deletetree(node*);
};

node* root = NULL;

int node::maketree()
{
node* temp;
int inf;
node *ptr, *prev;
cout<<"Enter in a number: ";
cin>>inf;
if (inf != -999)  {
temp = new node;
temp->info = inf;
temp->left = temp->right = NULL;

if (root == NULL)
root = temp;
else {
ptr = root;
while (ptr != NULL)
{
prev = ptr;
if(inf == ptr->info) {
cout<<"Duplicate Value - Invalid"<<endl;
break;
}
if (inf < ptr->info)
ptr = ptr->left;
else
ptr = ptr->right;
}
if (inf < prev->info)
prev->left = temp;
else
prev->right = temp;
}
}
return inf;
}

void node::intrav(node* ptr)
{
if (ptr != NULL)  {
intrav(ptr->left);
cout<<ptr->info<<" ";
intrav(ptr->right);
}
}

void node::pretrav(node* ptr)
{
if (ptr != NULL)  {
cout<<ptr->info<<" ";
pretrav(ptr->left);
pretrav(ptr->right);
}
}

void node::postrav(node* ptr)
{
if (ptr != NULL)  {
postrav(ptr->left);
postrav(ptr->right);
cout<<ptr->info<<" ";
}
}

int node::countnode(node* ptr)
{
if (ptr == NULL)
return 0;
else  {
int count = 1;
count += countnode(ptr->left);
count += countnode(ptr->right);
return count;
}
}

void node::child(node* ptr)
{
if(ptr == NULL)
return;
else  {
if((ptr->left != NULL)&&(ptr->right != NULL))
cout<<"Node with number "<<ptr->info<<" has two children"<<endl;
else if (((ptr->left == NULL)&&(ptr->right != NULL))
|| ((ptr->left != NULL)&&(ptr->right == NULL)))  {
cout<<"Node with number "<<ptr->info<<" has one child"<<endl;
}
child(ptr->left);
child(ptr->right);
}
}

void node::insert(node* ptr)
{
node *temp;
int inf;
node *p, *q;
p = q = ptr;
cout<<"Enter a number to insert: ";
cin>>inf;
if (inf != -999)  {
temp = new node;
temp->info = inf;
temp->left = temp->right = NULL;
}
while( inf != p->info && q != NULL) {
p = q;
if (info< p->info)
q = p->left;
else
q = p->right;
if (inf == p->info)
cout<<"\nNumber "<<inf<<" is duplicate."<<endl;
else if (inf < p->info)
p->left = temp;
else
p->right = temp;
}
}

void node::deletetree(node* ptr)
{
if ( ptr != NULL) {
deletetree(ptr->left);
deletetree(ptr->right);
delete ptr;
cout<<"Deleted Node"<<endl;
}
}
int main()
{
int num;
node tree;
node* rot;  // Root Alias
cout<<"Enter -999 to end input. "<<endl;

while(tree.maketree() != -999)  {
rot = root;
}

cout<<"Inorder Traversal "<<endl;
tree.intrav(rot);
cout<<"\nPreorder Traversal "<<endl;
tree.pretrav(rot);
cout<<"\nPostorder Traversal "<<endl;
tree.postrav(rot);
num = tree.countnode(rot);
cout<<"\nThere are "<<num<<" nodes in this tree."<<endl;
tree.child(rot);

tree.insert(rot);
tree.postrav(rot);

//    tree.deletetree(rot);
//    tree.intrav(rot);

system("pause");
return 0;
}``````

Use a debugger. Set a breakpoint at line 122. Run. Initialize your tree with 1, 2 and 3. Step few times, enter 4 when prompted. Step, step, step up to line 129. Step again. Get surprised. Ask yourself how it is possible to get where it gets you. Solve the problem. Appreciate a debugger.

Hi!

I don't know if you have already solved his issue, but like "nezachem" (a small hint :) )there is a typing-error in the line!

Daniel

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.