I m 定义双双平级搜索树类:
template <class T>
class BSTNode
{
public:
BSTNode(){
right = left = 0;
}
BSTNode(const T& el, BSTNode * l = 0, BSTNode * r = 0)
{
val = el; left = l; right = r;
}
T val;
BSTNode * right;
BSTNode * left;
};
template <class T>
class BST
{
public:
BST(){ root = 0;}
void Insert(const T& el);
private:
BSTNode<T> * root;
};
and I implement the Insert()
function like this:
template <class T>
void BST<T>::Insert(const T& el)
{
bool bEqual = false;
BSTNode<T> * p = root;
while(p)
{
if(el == p->val)
{
bEqual = true;
return;
}
if(el < p->val)
{
// go left
p = p->left;
}
if(el > p->val)
{
// go right
p = p->right;
}
}
if(!p)
{
std::cout << "creating new node " << el << "
";
p = new BSTNode<T>(el);
}
}
为什么<代码>root点变量停留在0,而不是新物体的地址?