数据结构树的实验报告.docx

上传人:b****5 文档编号:2790503 上传时间:2022-11-15 格式:DOCX 页数:12 大小:53.77KB
下载 相关 举报
数据结构树的实验报告.docx_第1页
第1页 / 共12页
数据结构树的实验报告.docx_第2页
第2页 / 共12页
数据结构树的实验报告.docx_第3页
第3页 / 共12页
数据结构树的实验报告.docx_第4页
第4页 / 共12页
数据结构树的实验报告.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

数据结构树的实验报告.docx

《数据结构树的实验报告.docx》由会员分享,可在线阅读,更多相关《数据结构树的实验报告.docx(12页珍藏版)》请在冰豆网上搜索。

数据结构树的实验报告.docx

数据结构树的实验报告

数据结构

 

目的要求

1.掌握二叉树的存储实现。

2.掌握二叉树的遍历思想。

3.掌握二叉树的常见算法的程序实现。

实验内容

1.输入字符序列,建立二叉链表。

2.中序遍历二叉树:

递归算法。

3.中序遍历二叉树:

非递归算法。

(最好也能实现先序,后序非递归算法)

4.求二叉树的高度。

5.求二叉树的叶子个数。

6.借助队列实现二叉树的层次遍历。

7.在主函数中设计一个简单的菜单,分别调试上述算法。

源程序:

1.头文件:

栈和队列stack

#include

#include

#defineTRUE1

#defineFALSE0

#defineOK1

#defineERROR0

#defineOVERFLOW-2

typedefcharTElemType;

typedefstructBiTNode

{TElemTypedata;

structBiTNode*lchild,*rchild;

}BiTNode,*BiTree;

typedefBiTreeSElemType;

typedefBiTreeQElemType;

typedefBiTreeStatus;

typedefstructNode{//链栈的定义

SElemTypedata;

structNode*next;

}NODE;

typedefstruct{

NODE*top;//链式栈的栈顶指针

}Stack;

//链式栈的操作示例

voidInitStack(Stack&S){

S.top=(NODE*)malloc(sizeof(NODE));

if(!

S.top)exit(OVERFLOW);//分配失败

S.top->next=NULL;

}

intStackEmpty(StackS){

if(S.top->next==NULL)return(TRUE);

elsereturn(FALSE);

}

voidPush(Stack&S,SElemTypee){

NODE*p;

p=(NODE*)malloc(sizeof(NODE));

if(!

p)exit(OVERFLOW);//分配失败

p->data=e;

p->next=S.top->next;

S.top->next=p;

}

voidPop(Stack&S,SElemType&e)

{

NODE*p;

if(StackEmpty(S))return;//栈空

else{p=S.top->next;

e=p->data;

S.top->next=p->next;

free(p);

}

}

//队的操作

typedefstructQNode{

QElemTypedata;

structQNode*next;

}QNode,*QueuePtr;

typedefstruct{

QueuePtrfront;

QueuePtrrear;

}LinkQueue;

voidInitQueue(LinkQueue&Q){//初始化队列

Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));

if(!

Q.front)exit(OVERFLOW);//存储分配失败

Q.front->next=NULL;

}

intEnQueue(LinkQueue&Q,QElemTypee)//插入元素e为Q的新的队尾元素

{

QueuePtrp;

p=(QueuePtr)malloc(sizeof(QNode));

if(!

p)exit(OVERFLOW);

p->data=e;

p->next=NULL;

Q.rear->next=p;

Q.rear=p;

returnOK;

}

intDeQueue(LinkQueue&Q,QElemType&e)//删除Q的队头元素,用e返回其值

{

if(Q.front==Q.rear)returnERROR;

QueuePtrp;

p=Q.front->next;

e=p->data;

Q.front->next=p->next;

if(Q.rear==p)Q.rear=Q.front;

free(p);

returnOK;

}

2.主程序

#include

#include

#include"math.h"

#include"stack.h"

inth=0,max=0,flag=0;//全局变量

voidCreateBiTree(BiTree&T)

{TElemTypech;

scanf("%c",&ch);

if(ch=='')T=NULL;

else{

T=(BiTree)malloc(sizeof(BiTNode));

T->data=ch;

CreateBiTree(T->lchild);

CreateBiTree(T->rchild);

}

}

//2.中序遍历二叉树:

递归算法。

voidzhbianli(BiTreeT)

{

if(!

T)return;

zhbianli(T->lchild);

printf("%c",T->data);

zhbianli(T->rchild);

}

//先序非递归遍历

voidxifbianli(BiTreeT){

BiTreep;

StackS;

InitStack(S);

if(T)Push(S,T);

while(!

StackEmpty(S)){

Pop(S,p);

printf("%2c",p->data);

if(p->rchild)Push(S,p->rchild);

if(p->lchild)Push(S,p->lchild);

}

}

//3.中序遍历二叉树:

非递归算法。

voidzhfbianli(BiTreeT){

BiTreep;

StackS;

InitStack(S);

p=T;

while(p||!

StackEmpty(S)){

if(p)

{

Push(S,p);

p=p->lchild;

}

else

{

Pop(S,p);

if(p)

printf("%c",p->data);

p=p->rchild;

}

}

}

//后序非递归遍历

voidhofbianli(BiTreeT)

{

BiTreep;

StackS;

InitStack(S);

p=T;

while(T||!

StackEmpty(S))

{

if(T)

{

Push(S,T);

T=T->lchild;

}

else

{

T=S.top->next->data;

if(T->rchild==NULL||T->rchild==p)

{

printf("%c",T->data);

Pop(S,T);

p=T;

T=NULL;

}

else

{

T=T->rchild;

}

}

}

}

//4.求二叉树的高度。

inthight(BiTreeT)

{

inth,hl,hr;

if(T)

{

if(!

T->lchild&&!

T->rchild)

h=1;

else

{

hl=hight(T->lchild);

hr=hight(T->rchild);

h=(hl>hr?

hl:

hr)+1;

}

}

else

h=0;

return(h);

}

//5.求二叉树的叶子个数

intleaf(BiTreeT)

{intn;

if(!

T)n=0;

else

if(!

T->lchild&&!

T->rchild)n=1;

elsen=leaf(T->lchild)+leaf(T->rchild);

return(n);

}

//8.借助队列实现二叉树的层次遍历。

voidduiliebianli(BiTreeT)

{

LinkQueueQ;

InitQueue(Q);

if(T==NULL)return;

BiTreep=T;

printf("%c",p->data);

if(p->lchild)EnQueue(Q,p->lchild);

if(p->rchild)EnQueue(Q,p->rchild);

while(Q.front!

=Q.rear)

{

DeQueue(Q,p);

printf("%c",p->data);

if(p->lchild)EnQueue(Q,p->lchild);

if(p->rchild)EnQueue(Q,p->rchild);

}

return;

}

voidmain(){

BiTreeroot;intselect;

printf("二叉树实验\n");

printf("建立二叉树,输入字符序列:

\n");

CreateBiTree(root);

do{

printf("\n1中序递归遍历2中序非递归遍历序列\n");

printf("3.先序非递归遍历序列4.后序非递归遍历序列\n");

printf("5.计算叶子6.计算高度\n");

printf("7.队列实现二叉树的层次遍历\n");

printf("请输入选择:

");

scanf("%d",&select);

switch(select){

case1:

printf("\n中序递归遍历序列:

");

zhbianli(root);

break;

case2:

printf("\n中序非递归遍历序列:

");

zhfbianli(root);

break;

case3:

printf("\n先序非递归遍历序列:

");

xifbianli(root);

break;

case4:

printf("\n后序非递归遍历序列:

");

hofbianli(root);

break;

case5:

printf("\n二叉树中叶子个数为:

%d\n",leaf(root));

break;

case6:

hight(root);

printf("\n二叉树中高度为:

%d\n",hight(root));

break;

case7:

printf("\

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1