模板的练习二叉树.docx
《模板的练习二叉树.docx》由会员分享,可在线阅读,更多相关《模板的练习二叉树.docx(9页珍藏版)》请在冰豆网上搜索。
![模板的练习二叉树.docx](https://file1.bdocx.com/fileroot1/2023-1/21/e222bfbc-d900-40dc-939e-8e51415eb4ff/e222bfbc-d900-40dc-939e-8e51415eb4ff1.gif)
模板的练习二叉树
模板的练习---二叉树
分类:
C/C++2004-11-2401:
011165人阅读评论(0)收藏举报
以下是对二叉树的基本操作的实现,如创建无序二叉树,二叉排序树,三种递归遍历和非递归遍历,查找,插入,删除,以及树叶的计算和树的深度的计算等。
#include"iostream.h"
#include"stdlib.h"
#include"stack.h"
#pragmaonce
template
classCBiTree
{
public:
CBiTree(void)
{
}
~CBiTree(void)
{
}
staticstruct_tagBiTNode
{
Tdata;
struct_tagBiTNode*lchild;
struct_tagBiTNode*rchild;
};
private:
typedef_tagBiTNodeBiTNode,*BiTree;
public:
boolCreateBiTree(BiTree&t);
boolPreOrderTraverse(BiTreet,void(*visit)(Tdata));
boolPreOrderTraverseEx(BiTreet,void(*visit)(Tdata));
boolInOrderTraverse(BiTreet,void(*visit)(Tdata));
boolInOrderTraverseEx(BiTreet,void(*visit)(Tdata));
boolPostOrderTraverse(BiTreet,void(*visit)(Tdata));
boolPostOrderTraverseEx(BiTreet,void(*visit)(Tdata));
voidCountLeaf(BiTreet,int&count);
intBiTreeDepth(BiTreet);
//二叉排序树的操作
voidCreateSortBiTree(BiTree&root,T*a,intn);
voidInsertNode(BiTree&root,Te);
boolDeleteNode(BiTree&t,T&e);
boolSearchNode(BiTreet,Tkey,BiTreef,BiTree&p);
private:
voiddeleteNode(BiTree&p);
};
//创建一个无序二叉树
template
boolCBiTree:
:
CreateBiTree(BiTree&t)
{
intn;
cin>>n;
if(n==0)t=NULL;
else
{
if(!
(t=newBiTNode))returnfalse;
t->data=n;
CreateBiTree(t->lchild);
CreateBiTree(t->rchild);
}
returntrue;
}
//先根遍历递归表示
template
boolCBiTree:
:
PreOrderTraverse(BiTreet,void(*visit)(Tdata))
{
if(t!
=NULL)
{
visit(t->data);
PreOrderTraverse(t->lchild,visit);
PreOrderTraverse(t->rchild,visit);
}
returnfalse;
}
//先根遍历,栈表示
template
boolCBiTree:
:
PreOrderTraverseEx(BiTreet,void(*visit)(Tdata))
{
try
{
CStack:
:
_tagStacks;//栈
CStackstack;
//if(stack==NULL)
//returnfalse;
BiTreep=newBiTNode;
if(p==NULL)
returnfalse;
stack.InitStack(s);
p=t;
while(p||!
stack.StackEmpty(s))
{
if(p)
{
visit(p->data);
stack.Push(s,p);
p=p->lchild;
}
else
{
stack.Pop(s,p);
p=p->rchild;
}
}
stack.DestroyStack(s);
returntrue;
}
catch(...)
{
visit(-1);
returnfalse;
}
}
//中序遍历,递归算法
template
boolCBiTree:
:
InOrderTraverse(BiTreet,void(*visit)(Tdata))
{
if(t!
=NULL)
{
InOrderTraverse(t->lchild,visit);
visit(t->data);
InOrderTraverse(t->rchild,visit);
}
returntrue;
}
//中序遍历,栈表示
template
boolCBiTree:
:
InOrderTraverseEx(BiTreet,void(*visit)(Tdata))
{
try
{
CStack:
:
_tagStacks;
CStackstack;
BiTreep=newBiTNode;
p=t;
stack.InitStack(s);
while(p||!
stack.StackEmpty(s))
{
if(p)
{
stack.Push(s,p);
p=p->lchild;
}
else
{
stack.Pop(s,p);
visit(p->data);
p=p->rchild;
}
}
stack.DestroyStack(s);
returntrue;
}
catch(...)
{
visit(-1);
returnfalse;
}
}
//后序遍历,递归算法
template
boolCBiTree:
:
PostOrderTraverse(BiTreet,void(*visit)(Tdata))
{
if(t!
=NULL)
{
PreOrderTraverse(t->lchild,visit);
PreOrderTraverse(t->rchild,visit);
visit(t->data);
}
returntrue;
}
//后序遍历,栈表示
template
boolCBiTree:
:
PostOrderTraverseEx(BiTreet,void(*visit)(Tdata))
{
try
{
CStack:
:
_tagStacks;
CStackstack;
BiTreep=newBiTNode;
p=t;
stack.InitStack(s);
while(p||!
stack.StackEmpty(s))
{
if(p)
{
stack.Push(s,p->lchild);
p=p->lchild;
}
else
{
visit(p->data);
stack.Pop(s,p);
p=p->rchild;
visit(p->data);
}
}
returntrue;
}
catch(...)
{
visit(-1);
returnfalse;
}
}
//计数树叶的个数
template
voidCBiTree:
:
CountLeaf(BiTreet,int&count)
{
if(t!
=NULL)
{
CountLeaf(t->lchild,count);
CountLeaf(t->rchild,count);
//检查结点t是否为叶子结点,若是,则定量count++
if(t->lchild==NULL&&t->rchild==NULL)
count++;
}
}
//树的深度
template
intCBiTree:
:
BiTreeDepth(BiTreet)
{
intdep;
intdepleft;
intdeprigth;
if(t==NULL)
dep=-1;
if(t!
=NULL)
{
depleft=BiTreeDepth(t->lchild);
deprigth=BiTreeDepth(t->rchild);
dep=1+(depleft>deprigth?
depleft:
deprigth);
}
returndep;
}
//
template
boolCBiTree:
:
SearchNode(BiTreet,Tkey,BiTreef,BiTree&p)
{
if(t==NULL)
{
p=f;
returnfalse;
}
elseif(key==t->data)
{
p=t;
returntrue;
}
elseif(keydata)
{
SearchNode(t->lchild,key,t,p);
}
else
SearchNode(t->rchild,key,t,p);
}
template
voidCBiTree:
:
InsertNode(BiTree&root,Te)
{
BiTreet=root;
BiTreep=NULL;
BiTreenewNode=newBiTNode;
while(t)
{
p=t;
if(e<=t->data)
t=t->lchild;
else
t=t->rchild;
}
newNode->data=e;
newNode->lchild=newNode->rchild=NULL;
if(p==NULL)
root=newNode;
else
if(edata)
p->lchild=newNode;
else
p->rchild=newNode;
}
//找到要删除的结点,删除结点
template
voidCBiTree:
:
deleteNode(BiTree&p)
{
BiTreeq;
BiTrees;
if(!
p->lchild)
{
q=p;
p=p->rchild;
free(q);
}
elseif(!
p->rchild)
{
q=p;
p=p->lchild;
free(q);
}
else
{
q=p;
s=p->lchild;
while(s->rchild)
{
q=s;
s=s->rchild;
}
p->data=s->data;
if(q!
=p)
q->rchild=s->lchild;
else
q->lchild=s->lchild;
}
}
//查找要删除的结点,并删除
template
boolCBiTree:
:
DeleteNode(BiTree&t,T&e)
{
if(!
t)
returnfalse;
else
{
if(e==t->data)
deleteNode(t);
elseif(edata)
DeleteNode(t->lchild,e);
else
DeleteNode(t->rchild,e);
returntrue;
}
}
//n为数据的总长度用n=sizeof(a)/sizeof(a[0]);
template
voidCBiTree:
:
CreateSortBiTree(BiTree&root,T*a,intn)
{
for(inti=0;i{
InsertNode(root,a[i]);
}
}
/*
*************************************************************
test
*************************************************************
*/
voidprint(intdata)
{
if(data==-1)
cout<<"/nError"<cout<}
int_tmain(intargc,_TCHAR*argv[])
{
cout<<"/nBiTree:
-----------------------/n";
CBiTree:
:
_tagBiTNode*p1=NULL;
CBiTree*tree=newCBiTree;
cout<<"/n树的深度为:
"<BiTreeDepth(t)<intn=0;
inta[]={8,6,9,10,23,2,3,0,4,0,5};
tree->CreateSortBiTree(p1,a,sizeof(a)/sizeof(a[0]));
cout<<"/n前序遍历/n";
tree->PreOrderTraverse(p1,&print);
cout<<"/n";
tree->PreOrderTraverseEx(p1,&print);
cout<<"/n中序遍历/n"<tree->InOrderTraverse(p1,&print);
cout<<"/n";
tree->InOrderTraverseEx(p1,&print);
tree->CountLeaf(p1,n);
cout<<"/n树的深度为:
"<BiTreeDepth(p1)<cout<<"叶子:
"<cout<<"查找"<intk0=3;
if(tree->SearchNode(p1,3,NULL,t))
{
cout<<"找到了"<tree->DeleteNode(p1,k0);
tree->InOrderTraverse(p1,&print);
}
else
cout<<"没找到"<}