node*聽聽map::treeSearch(node聽*n,KeyType聽key)
{
if(n->key聽==聽key)
return聽n;
if(n==NULL)聽return聽NULL;
else
{
if(keyvalue)
{
聽聽聽聽 treeSearch(n->left,聽key);
}
else
{
treeSearch(n->right,聽key);
}
}
聽
}这个函数放到类里面定义,不要放在类外面,最终修改如下:
template聽
class聽map
{
public:
map();
~map();
void聽PrintMap();
void聽Insert(KeyType聽key,ValType聽value);
bool聽Remove(KeyType聽key);
ValType聽GetValue(KeyType聽key);
int聽size();
bool聽IsContain(KeyType聽key);
struct聽node{
KeyType聽key;
ValType聽value;
node*聽left,*right;
};
private:
node*聽root;
//node*聽treeSearch(node聽*n,KeyType聽key);
void聽treeEnter(node聽*聽&n,KeyType聽key,ValType聽value);
void聽PrintMap(node聽*聽root);
node*聽聽map::treeSearch(node聽*n,KeyType聽key)
{
if(n->key聽==聽key)
return聽n;
if(n==NULL)聽return聽NULL;
else
{
if(keyvalue)
{
聽聽聽聽 treeSearch(n->left,聽key);
}
else
{
treeSearch(n->right,聽key);
}
}
聽
}
//DISALLOW_COPY(map);
};
template聽
void聽map::PrintMap()
{
PrintMap(root);
}
聽
template聽
聽map::map()
{
root聽=聽NULL;
}
template聽
聽map::~map()
{
}
template聽
void聽聽map::PrintMap(node聽*聽root)
{
if(root聽!=NULL)
{
std::coutkey)
{
聽n->value聽=聽value;
}
else聽if(key聽key)
{
treeEnter(n->left,key,value);
}
else
treeEnter(n->right,key,value);
聽
}
template聽
void聽map::Insert(KeyType聽key,ValType聽value){
return聽treeEnter(root,key,value);
}
聽
//#endif
聽
int聽main(int聽argc,聽char聽**argv)
{
map聽*a聽=聽new聽map();
for(int聽i聽=聽0聽;iInsert(randomInteger(1,10000),randomInteger(1,10000));
}
a->PrintMap();
system("pause");
return聽0;
}
由于你给的也不是全部代码,剩下的问题需要你按如上修改后再看一下还有什么问题。也许问题不是出在你给的上述代码中。
|