destroytree(root);
return0;
}
创建二叉搜索树
1)添加节点
2)显示树
3)删除节点
4)各种工具函数
BSTTree.cpp
#include
usingnamespacestd;
enumORDER_MODE
{
ORDER_MODE_PREV=0,
ORDER_MODE_MID,
ORDER_MODE_POST
};
template
structBinaryNode
{
Telement;
BinaryNode*left;
BinaryNode*right;
BinaryNode(constT&theElement,
BinaryNode*lt,
BinaryNode*rt):
element(theElement),
left(lt),
right(rt)
{
}
};
template
classBinarySearchTree
{
private:
BinaryNode*m_root;
public:
BinarySearchTree();
BinarySearchTree(constBinarySearchTree&rhs);
~BinarySearchTree();
constT&findMin()const;
constT&findMax()const;
boolcontains(constT&x)const;
voidprintTree(ORDER_MODEeOrderMode=ORDER_MODE_PREV)const;
voidmakeEmpty();
voidinsert(constT&x);
voidremove(constT&x);
private:
//因为树的方法用到了很多递归,所以这里我们需要申明如下的私有成员函数
voidinsert(constT&x,BinaryNode*&t);
voidremove(constT&x,BinaryNode*&t);
BinaryNode*findMin(BinaryNode*t)const;
BinaryNode*findMax(BinaryNode*t)const;
boolcontains(constT&x,constBinaryNode*t)const;
voidmakeEmpty(BinaryNode*&t);
voidprintTreeInPrev(BinaryNode*t)const;
voidprintTreeInMid(BinaryNode*t)const;
voidprintTreeInPost(BinaryNode*t)const;
};
template
BinarySearchTree:
:
BinarySearchTree()
{
m_root=NULL;
}
template
BinarySearchTree:
:
BinarySearchTree(constBinarySearchTree&rhs)
{
m_root=rhs.m_root;
}
template
BinarySearchTree:
:
~BinarySearchTree()
{
makeEmpty();
}
//returntrueifthexisfoundinthetree
template
boolBinarySearchTree:
:
contains(constT&x)const
{
returncontains(x,m_root);
}
template
boolBinarySearchTree:
:
contains(constT&x,constBinaryNode*t)const
{
if(!
t)
returnfalse;
elseif(xelement)
returncontains(x,t->left);
elseif(x>t->element)
returncontains(x,t->right);
else
returntrue;
}
//findtheminvalueinthetree
template
constT&BinarySearchTree:
:
findMin()const
{
returnfindMin(m_root)->element;
}
template
BinaryNode*BinarySearchTree:
:
findMin(BinaryNode*t)const
{
//二叉树的一个特点就是左子叶的值比根节点小,右子叶的比根节点的大
if(!
t)
returnNULL;
if(t->left==NULL)
returnt;
else
returnfindMin(t->left);
}
//findthemaxvalueinthetree
template
constT&BinarySearchTree:
:
findMax()const
{
returnfindMax(m_root)->element;
}
template
BinaryNode*BinarySearchTree:
:
findMax(BinaryNode*t)const
{
//二叉树的一个特点就是左子叶的值比根节点小,右子叶的比根节点的大
if(t!
=NULL)
while(t->right!
=NULL)
t=t->right;
returnt;
}
//insertanelementintotree
template
voidBinarySearchTree:
:
insert(constT&x)
{
insert(x,m_root);
}
template
voidBinarySearchTree:
:
insert(constT&x,BinaryNode*&t)
{
if(t==NULL)
t=newBinaryNode(x,NULL,NULL);//注意这个指针参数是引用
elseif(xelement)
insert(x,t->left);
elseif(x>t->element)
insert(x,t->right);
else
;//donothing
}
//removeaelementintatree
template
voidBinarySearchTree:
:
remove(constT&x)
{
returnremove(x,m_root);
}
template
voidBinarySearchTree:
:
remove(constT&x,BinaryNode*&t)
{
if(t==NULL)
return;
if(xelement)
remove(x,t->left);
elseif(x>t->element)
remove(x,t->right);
else//now==
{
if(t->left!
=NULL&&
t->right!
=NULL)//twochild
{
t->element=findMin(t->right)->element;
remove(t->element,t->right);
}
else
{
BinaryNode*oldNode=t;
t=(t->left!
=NULL)?
t->left:
t->right;
deleteoldNode;
}
}
}
template
voidBinarySearchTree:
:
makeEmpty()
{
makeEmpty(m_root);
}
template
voidBinarySearchTree:
:
makeEmpty(BinaryNode*&t)
{
if(t)
{
makeEmpty(t->left);
makeEmpty(t->right);
deletet;
}
t=NULL;
}
//Printtree
template
voidBinarySearchTree:
:
printTree(ORDER_MODEeOrderMode/*=ORDER_MODE_PREV*/)const
{
if(ORDER_MODE_PREV==eOrderMode)
printTreeInPrev(m_root);
elseif(ORDER_MODE_MID==eOrderMode)
printTreeInMid(m_root);
elseif(ORDER_MODE_POST==eOrderMode)
printTreeInPost(m_root);
else
;//donothing
}
template
voidBinarySearchTree:
:
printTreeInPrev(BinaryNode*t)const
{
if(t)
{
cout<element;
printTreeInPrev(t->left);
printTreeInPrev(t->right);
}
}
template
voidBinarySearchTree:
:
printTreeInMid(BinaryNode*t)const
{
if(t)
{
printTreeInPrev(t->left);
cout<element;
printTreeInPrev(t->right);
}
}
template
voidBinarySearchTree:
:
printTreeInPost(BinaryNode*t)const
{
if(t)
{
printTreeInPost(t->left);
printTreeInPost(t->right);
cout<element;
}
}
intmain(){
BinarySearchTreebst;
bst.insert(5);
bst.insert(6);
bst.insert(7);
bst.insert(9);
bst.insert(4);
bst.printTree();
bst.remove(7);
bst.printTree();
return0;
}
二叉搜索树总结:
1.这里x最好声明为constT&x,涉及到递归,值传递浪费空间,引用同时声明为常量
2.修改树的结构时一般都是引用,是不是在递归一般都这样呢?
?
3.remove的辅助函数findMin这里可以不用通过递归,所以可以不需要在public弄一个接口但是这里还是需要将其放到private中去,findMin目前不会单独作为一个功能提供给开发者,只是一个工具函数,放到private中去更合理
Ps:
还有老多不理解的地方,指针真难,后期跟进!
!