自考数据结构上机题目3 二叉树Word格式文档下载.docx

上传人:b****5 文档编号:19142519 上传时间:2023-01-04 格式:DOCX 页数:8 大小:15.32KB
下载 相关 举报
自考数据结构上机题目3 二叉树Word格式文档下载.docx_第1页
第1页 / 共8页
自考数据结构上机题目3 二叉树Word格式文档下载.docx_第2页
第2页 / 共8页
自考数据结构上机题目3 二叉树Word格式文档下载.docx_第3页
第3页 / 共8页
自考数据结构上机题目3 二叉树Word格式文档下载.docx_第4页
第4页 / 共8页
自考数据结构上机题目3 二叉树Word格式文档下载.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

自考数据结构上机题目3 二叉树Word格式文档下载.docx

《自考数据结构上机题目3 二叉树Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《自考数据结构上机题目3 二叉树Word格式文档下载.docx(8页珍藏版)》请在冰豆网上搜索。

自考数据结构上机题目3 二叉树Word格式文档下载.docx

typedefBinTNode*BinTree;

//建立二叉树

voidCreateBinTree(BinTree*T)

charch;

if((ch=getchar())=='

'

{

printf("

建立空节点\n"

);

*T=NULL;

}

else

*T=(BinTNode*)malloc(sizeof(BinTNode));

(*T)->

data=ch;

建立节点%c\n"

ch);

CreateBinTree(&

(*T)->

lchild);

rchild);

//在序列中查找节点位置

intSearch(chararray[],charc)

inti=0;

while(array[i]!

=c&

&

array[i])

i++;

if(array[i]==c)

returni;

return-1;

}

//基于先序和中序序列的构造算法

voidCreateBinTree2(BinTree*T,charpre[],charino[],intps,intis,intn)

intk;

if(n==0)

k=Search(ino,pre[ps]);

if(k==-1)

puts("

Error\n"

data=pre[ps];

if(k==is)

lchild=NULL;

CreateBinTree2(&

lchild,pre,ino,ps+1,is,k-is);

if(k==is+n-1)

rchild=NULL;

rchild,pre,ino,ps+1+(k-is),k+1,n-(k-is)-1);

//基于中序和后序序列的构造算法

voidCreateBinTree3(BinTree*T,charino[],charpost[],intis,intps,intn)

k=Search(ino,post[ps+n-1]);

Error!

\n"

data=post[ps+n-1];

CreateBinTree3(&

lchild,ino,post,is,ps,k-is);

 

rchild,ino,post,k+1,ps+(k-is),n-(k-is)-1);

//先序遍历二叉树

voidPreorder(BinTreeT)

if(T)

%c"

T->

data);

Preorder(T->

//中序遍历二叉树

voidInorder(BinTreeT)

Inorder(T->

//后序遍历二叉树

voidPostOrder(BinTreeT)

PostOrder(T->

//求二叉树的深度(高度)

intGetDepth(BinTreeT)

intld,rd;

if(!

T)

return0;

ld=GetDepth(T->

rd=GetDepth(T->

returnld>

rd?

ld+1:

rd+1;

//求二叉树叶子节点数

intGetLeafCount(BinTreeT)

intcount;

if(T==NULL)

count=0;

elseif((T->

lchild==NULL)&

(T->

rchild==NULL))

count=1;

count=GetLeafCount(T->

lchild)+GetLeafCount(T->

returncount;

//求度为1的节点个数

intGetOneDegreeLeaf(BinTreeT)

rchild!

=NULL))

lchild!

=NULL)&

count=GetOneDegreeLeaf(T->

lchild)+GetOneDegreeLeaf(T->

voidmain()

intdepth,leafCount,oneDegreeLeafCount;

charpre[20],ino[20],post[20];

BinTreeroot=NULL;

//puts("

输入前序序列:

"

//gets(pre);

输入中序序列:

gets(ino);

输入后序序列:

gets(post);

//CreateBinTree(&

root);

//CreateBinTree2(&

root,pre,ino,0,0,6);

root,ino,post,0,0,11);

\n前序遍历二叉树:

Preorder(root);

\n中序遍历二叉树:

Inorder(root);

\n后序遍历二叉树:

PostOrder(root);

depth=GetDepth(root);

\n二叉树深度(高度)为:

%d"

depth);

leafCount=GetLeafCount(root);

\n二叉树叶节点为:

leafCount);

oneDegreeLeafCount=GetOneDegreeLeaf(root);

\n二叉树度为一的节点数为:

oneDegreeLeafCount);

getch();

/*

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

当前位置:首页 > 农林牧渔 > 水产渔业

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

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