C++实现一个简单的二叉搜索树
Posted Jing Sir
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++实现一个简单的二叉搜索树相关的知识,希望对你有一定的参考价值。
如下所示,这是一个简单的二叉搜索树:
template <class T>
class binarysearchtreenode
{
public:
T data;
binarysearchtreenode<T> *leftchild;
binarysearchtreenode<T> * rightchild;
binarysearchtreenode ()
{
leftchild=rightchild=NULL;
}
binarysearchtreenode(T ele)
{
data=ele;
leftchild=NULL;
rightchild=NULL;
}
binarysearchtreenode(T ele,binarysearchtreenode * left,binarysearchtreenode * right)
{
data=ele;
leftchild=left;
rightchild=right;
}
T getvalue()
{
return data;
}
};
template <class T>
class binarysearchtree
{
public:
binarysearchtreenode<T> * root;
binarysearchtree(binarysearchtreenode<T> * r=NULL)
{
root=r;
}
bool isempty()
{
return root==NULL;
}
void visit(binarysearchtreenode<T> * t)//访问结点中元素
{
cout<<t->data<<' ';
}
void levelorder()//广度优先遍历二叉树
{
queue<binarysearchtreenode<T> *> nodequeue;
binarysearchtreenode<T> * pointer=root;
if(pointer)
nodequeue.push(pointer);
while(!nodequeue.empty())
{
pointer=nodequeue.frontT();
visit(pointer);
nodequeue.pop();
if(pointer->leftchild)
nodequeue.push(pointer->leftchild);
if(pointer->rightchild)
nodequeue.push(pointer->rightchild);
}
}
binarysearchtreenode<T> * search(binarysearchtreenode<T> * r,T key)//二叉搜索树的查找
{
binarysearchtreenode<T> * current=r;
while((r!=NULL)&&(current->getvalue()!=key))
{
current=(key<current->getvalue()?search(current->leftchild,key):search(current->rightchild,key));
}
return current;
}
binarysearchtreenode<T> * searchfather(binarysearchtreenode<T>* r, T key)//查找要删除树的父亲结点
{
binarysearchtreenode<T> * current=r;
binarysearchtreenode<T> * father=NULL;
if(key==r->data)
{
father=NULL;
return father;
}
else
{
while(r!=NULL&&key!=current->data)
{
father=current;
current=(key<current->data?current->leftchild:current->rightchild);
}
return father;
}
}
void insertnode(const T & value)//向二叉搜索树中插入结点
{
binarysearchtreenode<T> * p=root,*prev=NULL;
while(p!=0)
{
prev=p;
if(p->getvalue()<value)
p=p->rightchild;
else
p=p->leftchild;
}
if(root==NULL)
root=new binarysearchtreenode<T>(value);
else if(prev->getvalue()<value)
prev->rightchild=new binarysearchtreenode<T>(value);
else
prev->leftchild=new binarysearchtreenode<T>(value);
}
void deletebymerging(binarysearchtreenode<T>* & node,binarysearchtreenode<T> * & nodefather)//合并删除
{
binarysearchtreenode<T> * tmp=node;
if(node->rightchild==NULL)//被删除的结点没有右子树
{
if(nodefather)
{
if(nodefather->leftchild==node)
{
nodefather->leftchild=node->leftchild;
}
else
{
nodefather->rightchild=node->leftchild;
}
}
else
root=node->leftchild;
delete node;
}
else if(node->leftchild==NULL)//被删除结点的左子树为空
{
if(nodefather)
{
if(nodefather->leftchild==node)
{
nodefather->leftchild=node->rightchild;
}
else
{
nodefather->leftchild=node->leftchild;
}
}
else
root=node->rightchild;
delete node;
}
else
{
tmp=node->leftchild;
while(tmp->rightchild!=NULL)
tmp=tmp->rightchild;
tmp->rightchild=node->rightchild;
if(nodefather->leftchild==node)
{
nodefather->leftchild=node->leftchild;
}
else
{
nodefather->rightchild=node->leftchild;
}
delete node;
}
cout<<endl;
}
void deletebycopying(binarysearchtreenode<T> * & node,binarysearchtreenode<T> * & nodefather)//复制删除
{
binarysearchtreenode<T> *previous,*tmp=node;
if(node->rightchild==NULL)//被删除的结点没有右子树
{
if(nodefather)
{
if(nodefather->leftchild==node)
{
nodefather->leftchild=node->leftchild;
}
else
{
nodefather->rightchild=node->leftchild;
}
}
else
root=node->leftchild;
delete node;
}
else if(node->leftchild==NULL)//被删除结点的左子树为空
{
if(nodefather)
{
if(nodefather->leftchild==node)
{
nodefather->leftchild=node->rightchild;
}
else
{
nodefather->leftchild=node->leftchild;
}
}
else
root=node->rightchild;
delete node;
}
else
{
tmp=node->leftchild;
previous=node;
while(tmp->rightchild!=NULL)
{
previous=tmp;
tmp=tmp->rightchild;
}
node->data=tmp->data;
if(previous==node)
previous->leftchild=tmp->leftchild;
else
previous->rightchild=tmp->leftchild;
delete tmp;
}
cout<<endl;
}
};
以上是关于C++实现一个简单的二叉搜索树的主要内容,如果未能解决你的问题,请参考以下文章