Hi,
Am attempting to get this little populate function to work. Currently it populates the first node on either side of the root, but beyond that it will crash.
Code:
// this is the struct
struct nodeType
{
int info;
nodeType *llink;
nodeType *rlink;
};
//this is the constructor for instantiating the tree
cBinaryTree::cBinaryTree(void)
{
root = new nodeType;
root->info = 0;
root->llink = NULL;
root->rlink = NULL;
}
//this is the for loop in main that sends a random number and the
//root to the populate() function. theTree is the instantiated class.
for (int i = 0; i < 10; i++)
{
randomNumber = TOPRAND*rand()/(RAND_MAX + 1);
theTree->populate(randomNumber, theTree->root);
}
//this is populate
void cBinaryTree::populate(int num, nodeType *p) //p is root
{
temp = p;
bool isPlaced = false; //marker for placement
while (isPlaced != true)
{
if (num <= temp->info )
{
if (temp->llink == 0)
{
nodeType * newNode;
newNode = new nodeType;
temp->llink = newNode;
newNode->info = num;
cout<<temp->llink->info;
isPlaced = true;
}
else temp = temp->llink;
}
else
{
if (temp->rlink == 0)
{
nodeType * newNode;
newNode = new nodeType;
temp->rlink = newNode;
newNode->info = num;
cout<<temp->rlink->info;
isPlaced = true;
}
else temp = temp->rlink;
}
}
}
As i mentioned, this code can populate the right and left links of the root - but beyond that I get a crash... I have tried various versions of the above code, but i *think* my issue has to do with the linking of the various nodes.
Wow, that statement seems obvious, but... there you have it...
Any and ALL help would be appreciated. I'm certain it's probably something stupid - but I am at a loss...