1) root is uninitialized so you are accessing random memory.
2) you probably want a "deep" copy of the key, not just a copy of the pointer.
3) you probably want to pass key as a const char*.
1 2 3 4 5 6 7 8 9 10
class Map {
// ...
Map( constchar* key, double val ) // Pass by const char*
{
root = new tree_node; // Initialize the root pointer before using
root->key = strdup( key ); // "Deep" copy of the string, not just a pointer copy
root->data = val;
root->left = root->right = 0;
}
};
You will then need to ensure that tree_node::key gets destroyed so the memory
is freed. Here is one way: