二叉搜索树:也称二叉排序树。它可能是一个空树,具有以下的性质:
1.若它的左子树不为空,则左子树上的所有节点的值均小于根节点
2.若它的右子树不为空,则右子树上的所有节点的值均小于根节点
3.它的左右子树也分别为二叉搜索树
实现代码:
#include <iostream>
#include <assert.h>
using namespace std;
template<class K,class V>
struct BSTreeNode
{
BSTreeNode(const K& key,const V& value):_pLeft(NULL),_pRight(NULL),_key(key),_value(value)
{}
BSTreeNode<K,V>* _pLeft;
BSTreeNode<K,V>* _pRight;
K _key;
V _value;
};
template<class K,class V>
class BSTree
{
public:
typedef BSTreeNode<K,V> Node;
BSTree():_pRoot(NULL)
{}
BSTree(const BSTree<K,V>& bst)
{
_pRoot=_copy(bst._pRoot);
}
BSTree<K,V>& operator=(const BSTree<K,V>& bst)
{
if(this!=&bst)
{
//销毁原空间
_destroy(_pRoot);
//拷贝内容
_pRoot=_copy(bst._pRoot);
}
return *this;
}
~BSTree()
{
_destroy(_pRoot);
}
bool Insert(const K& key,c