模板的练习二叉树Word文档下载推荐.docx
《模板的练习二叉树Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《模板的练习二叉树Word文档下载推荐.docx(9页珍藏版)》请在冰豆网上搜索。
typedef_tagBiTNodeBiTNode,*BiTree;
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);
voiddeleteNode(BiTree&
};
//创建一个无序二叉树
typenameT>
boolCBiTree<
T>
:
CreateBiTree(BiTree&
t)
intn;
cin>
>
n;
if(n==0)t=NULL;
else
if(!
(t=newBiTNode))returnfalse;
t->
data=n;
CreateBiTree(t->
lchild);
rchild);
returntrue;
}
//先根遍历递归表示
PreOrderTraverse(BiTreet,void(*visit)(Tdata))
if(t!
=NULL)
visit(t->
data);
PreOrderTraverse(t->
lchild,visit);
rchild,visit);
returnfalse;
//先根遍历,栈表示
PreOrderTraverseEx(BiTreet,void(*visit)(Tdata))
try
CStack<
BiTree>
_tagStacks;
//栈
stack;
//if(stack==NULL)
//returnfalse;
BiTreep=newBiTNode;
if(p==NULL)
stack.InitStack(s);
p=t;
while(p||!
stack.StackEmpty(s))
if(p)
visit(p->
stack.Push(s,p);
p=p->
lchild;
stack.Pop(s,p);
rchild;
stack.DestroyStack(s);
catch(...)
visit(-1);
//中序遍历,递归算法
InOrderTraverse(BiTreet,void(*visit)(Tdata))
if(t!
=NULL)
InOrderTraverse(t->
//中序遍历,栈表示
InOrderTraverseEx(BiTreet,void(*visit)(Tdata))
stack;
//后序遍历,递归算法
PostOrderTraverse(BiTreet,void(*visit)(Tdata))
//后序遍历,栈表示
PostOrderTraverseEx(BiTreet,void(*visit)(Tdata))
stack.Push(s,p->
p=p->
//计数树叶的个数
voidCBiTree<
CountLeaf(BiTreet,int&
count)
CountLeaf(t->
lchild,count);
rchild,count);
//检查结点t是否为叶子结点,若是,则定量count++
if(t->
lchild==NULL&
&
t->
rchild==NULL)
count++;
//树的深度
intCBiTree<
BiTreeDepth(BiTreet)
intdep;
intdepleft;
intdeprigth;
if(t==NULL)
dep=-1;
if(t!
=NULL)
{
depleft=BiTreeDepth(t->
deprigth=BiTreeDepth(t->
dep=1+(depleft>
deprigth?
depleft:
deprigth);
returndep;
//
SearchNode(BiTreet,Tkey,BiTreef,BiTree&
p)
if(t==NULL)
p=f;
elseif(key==t->
data)
elseif(key<
SearchNode(t->
lchild,key,t,p);
rchild,key,t,p);
InsertNode(BiTree&
root,Te)
BiTreet=root;
BiTreep=NULL;
BiTreenewNode=newBiTNode;
while(t)
if(e<
=t->
t=t->
newNode->
data=e;
lchild=newNode->
rchild=NULL;
if(p==NULL)
root=newNode;
p->
p->
lchild=newNode;
rchild=newNode;
//找到要删除的结点,删除结点
deleteNode(BiTree&
BiTreeq;
BiTrees;
lchild)
q=p;
free(q);
elseif(!
rchild)
s=p->
while(s->
q=s;
s=s->
data=s->
data;
if(q!
=p)
q->
rchild=s->
lchild=s->
//查找要删除的结点,并删除
DeleteNode(BiTree&
e)
t)
if(e==t->
deleteNode(t);
elseif(e<
DeleteNode(t->
lchild,e);
rchild,e);
//n为数据的总长度用n=sizeof(a)/sizeof(a[0]);
CreateSortBiTree(BiTree&
root,T*a,intn)
for(inti=0;
i<
i++)
InsertNode(root,a[i]);
/*
*************************************************************
test
*/
voidprint(intdata)
if(data==-1)
cout<
<
"
/nError"
endl;
data<
/t"
;
int_tmain(intargc,_TCHAR*argv[])
/nBiTree:
-----------------------/n"
CBiTree<
int>
_tagBiTNode*p1=NULL;
*tree=newCBiTree<
/n树的深度为:
tree->
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]));
/n前序遍历/n"
PreOrderTraverse(p1,&
print);
/n"
PreOrderTraverseEx(p1,&
/n中序遍历/n"
InOrderTraverse(p1,&
InOrderTraverseEx(p1,&
CountLeaf(p1,n);
BiTreeDepth(p1)<
叶子:
n<
查找"
intk0=3;
if(tree->
SearchNode(p1,3,NULL,t))
找到了"
DeleteNode(p1,k0);
没找到"