void BinarySearchTree::insert(int d)
{
tree_node* t = new tree_node;
tree_node* parent;
t->data = d;
t->left = NULL;
t->right = NULL;
parent = NULL;
// is this a new tree?
if(isEmpty()) root = t;
else
{
//Note: ALL insertions are as leaf nodes
tree_node* curr;
curr = root;
// Find the Node's parent
while(curr)
{
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;
}
}
Questions:
Why do I need to allocate memory for tree_node* t; using new but not for tree_node* parent;?
What exactly is tree_node* what does it look like in memory and what does it do?
Can someone explain to me the -> operator and how it works?
->operator is an accessor so that you can get data stored within the object. As for what tree_node actually is? That's probably a custom data type made specifically for this tree implementation. You'd have to look through your source for it.->means the same thing as.in other languages; it references members of an object. Soparent->rightmeans therightmember in theparentobject.