I am loading a set of integers int a binary which is no problem, its when Im trying to increase their frequencies I am running into some difficulties. As it stands the program crashs if any of the numbers repeat themselves, any suggestions?
void BST::insert(int d)
{
tree_node* t = new tree_node;
tree_node* parent;
t->data = d;
t->freq = 1;
t->left = NULL;
t->right = NULL;
parent = NULL;
bool found = false;
// is this a new tree?
if(isEmpty())
root = t;
else
{
found = search(d);
if(found==true)
{
t->freq=t->freq + 1;
}
else
{
//insertions are as leaf nodes
tree_node* curr;
curr = root;
while(curr)
{
// Find the Node's parent
parent = curr;
if(t->data > curr->data)
curr = curr->right;
else curr = curr->left;
}
if(t->data < parent->data)
parent->left = t;
else
parent->right = t;
}
}
}
bool BST::search(int d)
{
tree_node * t = root;
bool found = false;
while (!found && t!=0)
{
if (d < t->data) // descend left
t = t->left;
else if (t->data < d) // descend right
t = t->right;
else // item found
found = true;
}
return found;
}