M
mathon
Hello,
im currently implementing a binary search tree means, that a greater
number than root will be added as right child and a less number as left
child. My insert function looks currently like this:
So in the else-branch he should look for the right place of the entry
and then insert the new node. I tried it with a while loop and step
through the tree, but i do not know exactly if this is right
respectively how should i then insert the new node.
Has somebody here an idea...?
lg matti
im currently implementing a binary search tree means, that a greater
number than root will be added as right child and a less number as left
child. My insert function looks currently like this:
Code:
template <class Item>
void bag<Item>::insert(const Item& entry)
// Header file used: bintree.h
{
binary_tree_node<Item> *cursor;
if (root_ptr == NULL)
{ // Add the first node of the binary search tree:
root_ptr = new binary_tree_node<Item>(entry);
return;
}
else
{ // Move down the tree and add a new leaf:
cursor = root_ptr;
while(cursor != NULL)
{
if(cursor->data() > entry)
cursor=cursor->right();
else if (cursor->data() <= entry)
cursor = cursor->left();
}
}
}
So in the else-branch he should look for the right place of the entry
and then insert the new node. I tried it with a while loop and step
through the tree, but i do not know exactly if this is right
respectively how should i then insert the new node.
Has somebody here an idea...?
lg matti