读前声明:本人所写帖子主要为了记录本人学习的一个过程,无他想法,由于内容比较肤浅,如有雷同,非常正常!!!
本文内容:
本文主要是参考《算法导论》这本书,完成部分算法编写,可能编程习惯或者风格比较差,还请多多批评。
从二分搜索算法性能中得知,如果每次从搜索序列的中间进行搜索,把区间缩小一半,通过有限次迭代,很快就能逼近到所要寻找的元素。进一步,如果我们直接输入搜索序列,构造出类似于二分查找的判定树那样的树形结构,就能实现快速搜索。这种树形结构就是二叉搜索树。
二叉搜索树(binary seach tree)或者是一棵空树,或者是具有下列性质的二叉树:
1)每个结点都有一个座位搜索依据的关键码(key),所有结点的关键码互不相同;
2)左子树(如果存在)上所有结点的关键码都小于根结点的关键码;
3)右子树(如果存在)上所有结点的关键码都大于根节点的关键码;
4)左子树和右子树也是二叉搜索树。
关键码事实上是结点所保存元素的某个域的值吗,它能够唯一地表示这个结点。因此,如果对一棵二叉搜索树进行中序遍历,可以按照从小到大的顺序,将各结点关键码排列起来,因此二叉搜索树也称为二叉排序树(binary sorting tree)。
二叉搜索树的类定义:
template <class E,class K>
struct BSTNode
{
E data; //数据域
BSTNode<E,K> *left,*right; //左子女和右子女
BSTNode():left(NULL),right(NULL){}
BSTNode(const E d,BSTNode<E,K> *L=NULL,BSTNode<E,K> *R=NULL):data(d),left(L),right(R){}
~BSTNode(){}
void SetData(E d){data = d;}
E GetData(){return data;}
bool operator <(const E& x){return (data.key<x.key)?true:false;}
bool operator >(const E& x){return (data.key>x.key)?true:false;}
bool operator ==(const E& x){return (data.key==x.key)?true:false;}
};
template <class E,class K>
class BST
{
public:
BST():root(NULL){}
BST(K value);
~BST(){};
bool Search(const K x)const{return (Search(x,root)!=NULL)?true:false;}
BST<E,K>& operator =(const BST<E,K>& R); //赋值
void makeEmpty(){makeEmpty(root);root = NULL;} //置空
void PrintTree()const{PrintTree(root);} //打印
E Min(){return Min(root)->data;} //求最小
E Max(){return Max(root)->data;} //求最大
bool Insert(const E& e){return Insert(e,root);} //插入
bool Remove(const E x){return Remove(x,root);} //移除
private:
BSTNode<E,K> *root;
K Refvalue;
BSTNode<E,K> *Search(const K x,BSTNode<E,K> *ptr); //递归,搜索
void makeEmpty(BSTNode<E,K> *&ptr); //递归,置空
void PrintTree(BSTNode<E,K> *ptr)const; //递归,打印
BSTNode<E,K> *Copy(const BSTNode<E,K> *ptr); //递归,复制
BSTNode<E,K> *Min(BSTNode<E,K> *ptr) const; //递归,求最小
BSTNode<E,K> *Max(BSTNode<E,K> *ptr) const; //递归,求最大
bool Insert(const E& e,BSTNode<E,K> *&ptr); //递归,插入
bool Remove(const E x,BSTNode<E,K> *&ptr); //递归,删除
};
二叉搜索树如果函数的定义:
template <class E,class K>
BSTNode<E,K> *BST<E,K>::Search(const K x,BSTNode<E,K> *ptr)
{
if (ptr==NULL)
{
return NULL;
}
if (x<ptr->data)
{
return Search(x,ptr->left);
}
else if (x>ptr->data)
{
return Search(x,ptr->right);
}
else
return ptr;
};
template <class E,class K>
bool BST<E,K>::Insert(const E& e,BSTNode<E,K> *&ptr)
{
if (ptr==NULL)
{
ptr = new BSTNode<E,K>(e);
if (ptr==NULL)
{
cout<<"空间溢出"<<endl;
exit(1);
}
return true;
}
if (e>ptr->data)
{
return Insert(e,ptr->right);
}
else if (e<ptr->data)
{
return Insert(e,ptr->left);
}
else return false;
};
template <class E,class K>
BST<E,K>::BST(K value)
{
E x;
root = NULL;
Refvalue = value;
cin>>x;
while (x.key!=Refvalue)
{
Insert(x,root);cin>>x;
}
};
template <class E,class K>
bool BST<E,K>::Remove(const E x,BSTNode<E,K> *&ptr)
{
if (ptr==NULL)
{
return false;
}
BSTNode<E,K> *temp;
if (x<ptr->data)
{
return Remove(x,ptr->left);
}
else if (x>ptr->data)
{
return Remove(x,ptr->right);
}
else
{
if (ptr->left!=NULL&&ptr->right!=NULL)
{
temp = ptr->right;
while (temp->left!=NULL)
{
temp = temp->left;
}
ptr->data = temp->data;
Remove(ptr->data,ptr->right);
}
else
{
temp = ptr;
if (temp->left==NULL)
{
ptr = ptr->right;
}
else ptr = ptr->left;
delete temp;
return true;
}
}
};
template <class E,class K>
void BST<E,K>::makeEmpty(BSTNode<E,K> *&ptr)
{
if (ptr!=NULL)
{
BSTNode<E,K> *temp=ptr;
makeEmpty(ptr->left);
makeEmpty(ptr->right);
delete temp;
}
};
template <class E,class K>
void BST<E,K>::PrintTree(BSTNode<E,K> *ptr)const
{
if (ptr!=NULL)
{
PrintTree(ptr->left);
cout<<ptr->data<<endl;
PrintTree(ptr->right);
}
};
template <class E,class K>
BSTNode<E,K> *BST<E,K>::Min(BSTNode<E,K> *ptr)const
{
if (ptr->left==NULL)
{
return ptr;
}
if (ptr->left!=NULL)
{
return Min(ptr->left);
}
return NULL;
};
template <class E,class K>
BSTNode<E,K> *BST<E,K>::Max(BSTNode<E,K> *ptr)const
{
if (ptr->right==NULL)
{
return ptr;
}
if (ptr->right!=NULL)
{
return Max(ptr->right);
}
return NULL;
};
template <class E,class K>
BSTNode<E,K> *BST<E,K>::Copy(const BSTNode<E,K> *ptr)
{
if (ptr!=NULL)
{
BSTNode<E,K> *temp=new BSTNode<E,K>(ptr->data);
temp->left = Copy(ptr->left);
temp->right = Copy(ptr->right);
return temp;
}
};
template <class E,class K>
BST<E,K>& BST<E,K>::operator =(const BST<E,K>& R)
{
BST<E,K> b;
b.root = R.Copy(R.root);
return b;
};