二叉树链式存储.docx
- 文档编号:27977686
- 上传时间:2023-07-07
- 格式:DOCX
- 页数:30
- 大小:114.51KB
二叉树链式存储.docx
《二叉树链式存储.docx》由会员分享,可在线阅读,更多相关《二叉树链式存储.docx(30页珍藏版)》请在冰豆网上搜索。
二叉树链式存储
测试函数
包含关系图
测试.cpp
#include"链式.h"
voidmain()
{inti;
BiTreeT,p,c;
TElemtypee1,e2;
InitBiTree(T);
printf("清空二叉树后树空否:
%d。
树的深度:
%d\n",BiTreeEmpty(T),BiTreeDepth(T));
e1=Root(T);
if(e1!
=Nil)
#ifdefCHAR
printf("二叉树的根为:
%c\n",e1);
#endif
#ifdefINT
printf("二叉树的根为:
%d\n",e1);
#endif
else
printf("树空,无根\n");
#ifdefCHAR
printf("请先序输入二叉树(如ab三个空格表示a为根节点,b为左子树的二叉树)");
#endif
#ifdefINT
printf("请先序输入二叉树(如12000表示1为根结点,2为左子树的二叉树)\n");
#endif
CreateBiTree(T);
printf("建立二叉树后,树空否?
%d,树的深度",BiTreeEmpty(T),BiTreeDepth(T));
e1=Root(T);
if(e1!
=Nil)
#ifdefCHAR
printf("二叉树的根为:
%c\n",e1);
#endif
#ifdefINT
printf("二叉树的根为:
%d\n",e1);
#endif
else
printf("树空,无根\n");
printf("中序递归遍历二叉树:
\n");
InOrderTraverse(T,visitT);
printf("\n非递归中序遍历二叉树\n");
InOrderTraverse_1(T,visitT);
printf("后序递归遍历二叉树\n");
PostOrderTraverse(T,visitT);
printf("\n层序递归遍历二叉树\n");
LevelOrderTraverse(T,visitT);
printf("请输入一个结点的值:
");
#ifdefCHAR
scanf("%*c%c",&e1);
#endif
#ifdefINT
scanf("%d",&e1);
#endif
p=Point(T,e1);//p为e1的指针
#ifdefCHAR
printf("结点的值为%c\n",Value(p));
#endif
#ifdefINT
printf("结点的值为%d\n",Value(p));
#endif
printf("欲改变此结点的值,请输入新值:
");
#ifdefCHAR
scanf("%*c%c%*c",&e2);
#endif
#ifdefINT
scanf("%d",&e2);
#endif
Assign(p,e2);
printf("层序遍历二叉树:
\n");
LevelOrderTraverse(T,visitT);
e1=Parent(T,e2);
if(e1!
=Nil)
#ifdefCHAR
printf("%c的双亲是%c\n",e2,e1);
#endif
#ifdefINT
printf("%d的双亲是%d\n",e2,e1);
#endif
else
#ifdefCHAR
printf("%c没有双亲\n",e2);
#endif
#ifdefINT
printf("%d没有双亲\n",e2);
#endif
e1=LeftChild(T,e2);
if(e1!
=Nil)
#ifdefCHAR
printf("%c的左孩子是%c\n",e2,e1);
#endif
#ifdefINT
printf("%d的左孩子是%d\n",e2,e1);
#endif
else
#ifdefCHAR
printf("%c没有左孩子\n",e2);
#endif
#ifdefINT
printf("%d没有左孩子\n",e2);
#endif
e1=RightChild(T,e2);
if(e1!
=Nil)
#ifdefCHAR
printf("%c的右孩子是%c\n",e2,e1);
#endif
#ifdefINT
printf("%d的右孩子是%d\n",e2,e1);
#endif
else
#ifdefCHAR
printf("%c没有右孩子\n",e2);
#endif
#ifdefINT
printf("%d没有右孩子\n",e2);
#endif
e1=LeftSibling(T,e2);
if(e1!
=Nil)
#ifdefCHAR
printf("%c的左兄弟是%c\n",e2,e1);
#endif
#ifdefINT
printf("%d的左兄弟是%d\n",e2,e1);
#endif
else
#ifdefCHAR
printf("%c没有左兄弟\n",e2);
#endif
#ifdefINT
printf("%d没有左兄弟\n",e2);
#endif
e1=RightSibling(T,e2);
if(e1!
=Nil)
#ifdefCHAR
printf("%c的右兄弟是%c\n",e2,e1);
#endif
#ifdefINT
printf("%d的右兄弟是%\n",e2,e1);
#endif
else
#ifdefCHAR
printf("%c没有右兄弟\n",e2);
#endif
#ifdefINT
printf("%d没有右兄弟\n",e2);
#endif
InitBiTree(c);
printf("构造一棵右子树为空的二叉树c:
\n");
#ifdefCHAR
printf("请先序输入二叉树\n");
#endif
#ifdefINT
printf("请先序输入二叉树\n");
#endif
CreateBiTree(c);
printf("先序递归遍历二叉树c:
\n");
PreOrderTraverse(c,visitT);
printf("\n树c插到树T中,请输入T中树C的双亲结点c为左(0)右
(1)子树\n");
#ifdefCHAR
scanf("%*c%c%d",&e1,&i);
#endif
#ifdefINT
scanf("%d%d",&e1,&i);
#endif
p=Point(T,e1);
InsertChild(p,i,c);
printf("先序递归遍历二叉树:
\n");
PreOrderTraverse(T,visitT);
printf("\n删除子树,请输入子树的双亲结点左(0)右
(1)子树:
");
#ifdefCHAR
scanf("%*c%c%d",&e1,&i);
#endif
#ifdefINT
scanf("%d%d",&e1,&i);
#endif
p=Point(T,e1);
DeleteChild(p,i);
printf("先序递归遍历二叉树:
\n");
PreOrderTraverse(T,visitT);
printf("\n");
DestroyBiTree(T);
}
链式.h
#include
#include
#include
#include
usingnamespacestd;
#defineCHAR
#ifdefCHAR
typedefcharTElemtype;
TElemtypeNil='';//字符型以空格符为空
#endif
#ifdefINT
typedefintTElemtype;
TElemtypeNil=0;
#endif
typedefstructBiTNode
{
TElemtypedata;
BiTNode*lchild,*rchild;
}BiTNode,*BiTree;
typedefBiTreeQElemtype;
#include"G:
\学习\12年暑假\3——4队列\3——4队列\1.h"
#defineOK1
#defineTRUE1
#defineFALSE0
#defineERROR0
typedefintstatus;
//typedefintTElemtype;
statusInitBiTree(BiTree&T)
{
T=NULL;
returnOK;
}
voidDestroyBiTree(BiTree&T)
{//初始条件:
二叉树T存在。
操作结果:
销毁二叉树T
if(T)//非空树
{if(T->lchild)
DestroyBiTree(T->lchild);
if(T->rchild)
DestroyBiTree(T->rchild);
free(T);
T=NULL;
}
}
voidCreateBiTree(BiTree&T)
{//算法6.4:
按先序次序输入二叉树中结点的值(可为字符型或整型,在主程中定义
//构造二叉链表表示的二叉树T。
变量Nil表示空树,有改动
TElemtypech;
#ifdefCHAR
scanf("%c",&ch);
#endif
#ifdefINT
scanf("%d",&ch);
#endif
if(ch==Nil)
T=NULL;
else
{
T=(BiTree)malloc(sizeof(BiTNode));
if(!
T)
exit(OVERFLOW);
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
statusBiTreeEmpty(BiTreeT)
{//初始条件:
二叉树T存在。
操作结果:
若T为空二叉树,返回TRUE否则返回FALSE
if(T)
returnFALSE;
else
returnTRUE;
}
#defineClearBiTreeDestroyBiTree
intBiTreeDepth(BiTreeT)
{//初始条件:
二叉树T存在。
操作结果:
返回T的深度
inti,j;
if(!
T)
return0;
if(T->rchild)
i=BiTreeDepth(T->lchild);
else
i=0;
if(T->rchild)
j=BiTreeDepth(T->rchild);
elsej=0;
returni>j?
i+1:
j+1;
}
TElemtypeRoot(BiTreep)
{if(BiTreeEmpty(p))
returnNil;
else
returnp->data;
}
TElemtypeValue(BiTreep)
{
returnp->data;
}
voidAssign(BiTreep,TElemtypevalue)
{
p->data=value;
}
TElemtypeParent(BiTreeT,TElemtypee)
{//初始条件:
二叉树T存在,e是T中某个结点
//操作结果:
若e是T非根结点,则返回它的双亲,否则返回空
LinkQueueq;
QElemtypea;
if(T)//非空树
{InitQueue(q);
EnQueue(q,T);
while(!
QueueEmpty(q))
{DeQueue(q,a);
if(a->lchild&&a->lchild->data==e||a->rchild&&a->rchild->data==e)
returna->data;
else
{
if(a->lchild)
EnQueue(q,a->lchild);
if(a->rchild)
EnQueue(q,a->rchild);
}
}
}
returnNil;
}
BiTreePoint(BiTreeT,TElemtypes)
{//返回二叉树中指向元素值为s的结点的指针
LinkQueueq;
QElemtypea;
if(T)
{
InitQueue(q);//初始化队列
EnQueue(q,T);//根结点入队
while(!
QueueEmpty(q))//队不空
{
DeQueue(q,a);//出队,队列元素赋给a
if(a->data==s)
returna;
if(a->lchild)
EnQueue(q,a->lchild);//
if(a->rchild)
EnQueue(q,a->rchild);//入队右孩子
}
}
returnNULL;
}
TElemtypeLeftChild(BiTreeT,TElemtypee)
{//初始条件:
二叉树T存在,e是T中某个结点。
操作结果:
返回e的左孩子。
若
//e无左孩子,则返回“空”
BiTreea;
if(T)//非空树
{a=Point(T,e);//a是结点e的指针
if(a&&a->lchild)//T中存在结点e且e存在左孩子
returna->lchild->data;//返回e的左孩子的值
}
returnNil;
}
TElemtypeRightChild(BiTreeT,TElemtypee)
{//初始条件:
二叉树T存在,e是T中某个结点
//操作结果:
返回e的右孩子若无右孩子则返回空
BiTreea;
if(T)//非空树
{
a=Point(T,e);//a是结点e的指针
if(a&&a->rchild)//T中存在结点e且e存在右孩子
returna->rchild->data;//返回e的右孩子的值
}
returnNil;//其余情况返回空
}
TElemtypeLeftSibling(BiTreeT,TElemtypee)
{//初始条件:
二叉树T存在,e是T中某个结点
//操作结果:
返回e的左兄弟。
若e是T的左孩子或无左兄弟。
则返回“空”
TElemtypea;
BiTreep;
if(T)//非空树
{
a=Parent(T,e);//a其e的双亲
p=Point(T,a);//p为指向结点a的指针
if(p->lchild&&p->rchild&&p->rchild->data==e)
returnp->lchild->data;
}
returnNil;
}
TElemtypeRightSibling(BiTreeT,TElemtypee)
{//
//
TElemtypea;
BiTreep;
if(T)
{
a=Parent(T,e);
p=Point(T,a);
if(p->lchild&&p->rchild&&p->lchild->data==e)
returnp->rchild->data;
}
returnNil;
}
statusInsertChild(BiTreep,intLR,BiTreec)
{//
//
if(p)
{
if(LR==0)
{c->rchild=p->lchild;
p->rchild=c;
}
else
{c->rchild=p->rchild;
p->rchild=c;
}
returnOK;
}
returnERROR;
}
statusDeleteChild(BiTreep,intLR)
{
//
//
if(p)
{
if(LR==0)
ClearBiTree(p->lchild);
else
ClearBiTree(p->rchild);
returnOK;
}
returnERROR;
}
voidPreOrderTraverse(BiTreeT,status(*visit)(TElemtype))
{//
if(T)
{
visit(T->data);
PreOrderTraverse(T->lchild,visit);
PreOrderTraverse(T->rchild,visit);
}
}
voidInOrderTraverse(BiTreeT,status(*visit)(TElemtype))
{
if(T)
{
InOrderTraverse(T->lchild,visit);
visit(T->data);
InOrderTraverse(T->rchild,visit);
}
}
typedefBiTreeSElemtype;
#include"G:
\学习\12年暑假\栈和队列1\栈和队列1\3_1.h"
statusInOrderTraverse_1(BiTreeT,status(*visit)(TElemtype))
{//
//非递归中序遍历
SqStackS;
InitStack(S);
while(T||!
StackEmpty(S))
{
if(T)
{Push(S,T);
T=T->lchild;
}
else
{Pop(S,T);
if(!
visit(T->data))
returnERROR;
T=T->rchild;
}
}
printf("\n");
returnOK;
}
voidPostOrderTraverse(BiTreeT,status(*visit)(TElemtype))
{
if(T)
{
PostOrderTraverse(T->lchild,visit);
PostOrderTraverse(T->rchild,visit);
visit(T->data);
}
}
voidLevelOrderTraverse(BiTreeT,status(*visit)(TElemtype))
{
LinkQueueq;
QElemtypea;
if(T)
{
InitQueue(q);
EnQueue(q,T);
while(!
QueueEmpty(q))
{DeQueue(q,a);
visit(a->data);
if(a->lchild!
=NULL)
EnQueue(q,a->lchild);
if(a->rchild!
=NULL)
EnQueue(q,a->rchild);
}
printf("\n");
}
}
statusvisitT(TElemtypee)
{
#ifdefCHAR
printf("%c",e);
#endif
#ifdefINT
printf("%d",e);
#endif
returnOK;
}
1.h
#include
#include
#include
#include
#defineTRUE1
#defineFALSE0
#defineOK1
#defineERROR0
#defineINFEASIBLE-1
typedefintstatus;
typedefintBoolean;
//typedefintQElemtype;
typedefstructQNode//单链队列
{QElemtypedata;
QNode*next;
}*QueuePtr;
structLinkQueue
{
QueuePtrfront,rear;//对头队尾指针
};
//和栈一样,队列也是操作受限的线性表,只允许在队尾插入元素,在队头删除元素
//为了便于插入元素,设立了队尾指针
statusInitQueue(LinkQueue&Q)
{if(!
(Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode))))
exit(OVERFLOW);
Q.front->next=NULL;
returnOK;
}
statusDestroyQueue(LinkQueue&Q)
{while(Q.front)
{Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
returnOK;
}
statusClearQueue(LinkQueue&Q)
{QueuePtrp,q;
Q.rear=Q.front;
p=Q.front->next;
Q.front->next=NULL;
while(p)
{q=p;
p=p->next;
free(q);
}
returnOK;
}
statusQueueEmpty(LinkQueueQ)
{if(Q.front==Q.rear)
returnOK;
else
returnFALSE;
}
intQueueLength(LinkQueueQ)
{
inti=0;
QueuePtrp,q;
p=Q.front;
while(p!
=Q.rear)
{i++;
p=p->next;
}
returni;
}
statusGetHead(LinkQueueQ,QElemtype&e)\
{//若队列不空则用e返回Q的队头
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 二叉 链式 存储