-
实验三
实验课程名:二叉树的操作及应用
专业班级:
11
计科(
3
)班
学号:
39
姓名:
廖万君
实验时间:
11.26
–
12.7
实验地点
K4-206
指导教师:
冯珊
一、实验目的
1
、进一步掌握指针变量、动态变量的含义。
2
、掌握二叉树的结构特性,以及各种存储结构的特点和适用范围。
3
、掌握用指针类型描述、访问和处理二叉树的运算。
二、实验内容
1
、以二叉链表作存储结构,试编写计算二叉树深度、所有结 点总数、叶子结点数、双孩子
结点个数、单孩子结点个数的算法
(1)
代码如下
:
.h
头文件定义如下:
#ifndef _Tree
#define _Tree
#include
using namespace std;
typedef char ElemType;
typedef struct TreeNode
{
void CreateTree(Tree &t,char *str,int len){
}
void Deepth(Tree t,int deep,int &max){
static int i=0;
if(i==len)
}
t=(Tree)malloc(sizeof(TreeNode));
t->lchild=NULL;
t->rchild=NULL;
memcpy(&t->data,&str[i++],sizeof(ElemType));
CreateTree(t->lchild,str,len);
CreateTree(t->rchild,str,len);
return;
t=NULL;
i++;
return;
if(str[i]==32){
ElemType data;
struct TreeNode *lchild,*rchild;
}TreeNode,*Tree;
}
deep++;
int flag=1;
if(t->lchild){
}
if(t->rchild){
}
if(flag){
}
if(deep>max)
max=deep;
return;
Deepth(t->rchild,deep,max);
flag=0;
Deepth(t->lchild,deep,max);
flag=0;
int NodeCount(Tree t){
}
int DoubleChildNodeCount(Tree t){
}
int SingleChildNodeCount(Tree t){
}
#endif
函数
if(!(t->rchild||t->lchild))
return 0;
return 1+SingleChildNodeCount(t->lchild);
return 1+SingleChildNodeCount(t->rchild);
else if(t->lchild&&!t->rchild)
else if(t->rchild&&!t->lchild)
else return SingleCh ildNodeCount(t->lchild)+SingleChildNodeCount(t->rc hild);
if(t->rchild&&t->lchild){
}
else
return 0;
return DoubleChild NodeCount(t->lchild)+DoubleChildNodeCount(t->rchil d)+1;
if(t==NULL)
return 0;
return NodeCount(t->lchild)+NodeCount(t->rchild)+1;
#include
“
Tree.h
”
int main(){
}
Tree t;
cout<<
”
请输入一颗树的构造,空间点用空格表示,例如:
abdf e c
“
<
gets(str);
CreateTree(t,str,strlen(str));
int max=0;
Deepth(t,0,max);
cout<<
”
树的深度为:
”
;
cout<
”
树的所有结点数为:
”
;
cout<
”
双孩子的结点个数为
:
”
;
cout<
”
单孩子的结点数位
:
”
;
cout<
(2)
运行结果
:
2
、
赫夫曼树与赫夫曼编码:
已知某系统在通信联络中只可能出现
8
种字符
a,b,c,d,e,f,g,h
,
其概率分别为
{0.05
,
0 .29
,
0.07
,
0.08
,
0.14
,
0.23
,
0.03
,
0.11}
,试设计
Huffma n
编码,
并计算其平均长度
代码如下:
#include
“
Tree.h
”
#include
#include
void GetCode(Tree t,int I,string str){
static int flag=0;
if(flag++)
str+=i+48;
cout<
”
:
”
<
}
}
return;
GetCode(t->lchild,0,str);
GetCode(t->rchild,1,str);
bool HaveLeft(Tree t[],int n,int flag[],int &fmin){
}
void select(Tree t[],int n,int flag[],int &fmin,int &smin){
}
int main(){
char v[9]=
”
abcdefgh
”
;
double w[8]={0.05,0.29,0.07,0.08,0.14,0.23,0.03,0.11};
int I;
int isFirst=0;
for(i=0;i
}
isFirst=0;
for(i=0;i
}
if(flag[i]==1||i==fmin)
continue;
smin=I;
smin=I;
if(!isFirst++)
else if(t[i]->data.w
if(flag[i]==1)
continue;
fmin=I;
fmin=I;
if(!isFirst++)
else if(t[i]->data.w
int I;
int count=0;
for(i=0;i
}
return false;
if(flag[i]!=1&&count++){
}
return true;
}
int flag[8]={0};
Tree t[8];
for(int i=0;i<8;i++){
}
int fmin ,smin;
while(HaveLeft(t, 8,flag,fmin)){
}
string str;
GetCode(t[fmin],0,str);
select(t,8,flag,fmin,smin);
Tree tmp=new TreeNode;
tmp->lchild=t[fmin];
tmp->rchild=t[smin];
tmp->data.w=t[fmin]->data.w+t[smin]->data.w;
tmp->data.v=0;
t[fmin]=tmp;
flag[fmin]=2;
flag[smin]=1;
t[i]=new TreeNode;
t[i]->data.v=v[i];
t[i]->data.w=w[i];
t[i]->lchild=NULL;
t[i]->rchild=NULL;
运行结果
:
3
、分别以顺序存储结构和二叉链表作存储结构,试编程实现前序、中序、后序及层次顺序
遍历二叉树的 算法。
(
1
)
Tree.h
头文件定义如下
#ifndef _Tree
#define _Tree
#include
using namespace std;
typedef char ElemType;
typedef struct TreeNode
{
ElemType data;
struct TreeNode *lchild,*rchild;
}TreeNode,*Tree;
void CreateTree(Tree &t,char *str,int len){
static int i=0;
if(i==len)
return;
if(str[i]==32){
t=NULL;
i++;
return;
}
t=(Tree)malloc(sizeof(TreeNode));
t->lchild=NULL;
t->rchild=NULL;
memcpy(&t->data,&str[i++],sizeof(ElemType));
CreateTree(t->lchild,str,len);
CreateTree(t->rchild,str,len);
}
void PreOrderTree(Tree t){
if(t==NULL)
return
cout<
PreOrderTree(t->lchild);
PreOrderTree(t->rchild);
}
void InOrderTree(Tree t){
if(t==NULL)
return
InOrderTree(t->lchild);
cout<
InOrderTree(t->rchild);
}
void LastOrderTree(Tree t){
if(t==NULL)
return
LastOrderTree(t->lchild);
LastOrderTree(t->rchild);
cout<
}
void LevelOrderTree(Tree t){
if(t==NULL)
return;
int front=0,rear=0;
ElemType e;
Tree array[52];
cout<
ta<<” “;
TreeNode *p;
array[rear++]=t;
while(front!=rear){
p=array[front++];
if(p->lchild){
cout<
>data<<” “;
array[rear++]=p->lchild;
}
if(p->rchild){
cout<
>data<<” “;
array[rear++]=p->rchild;
}
}
}
#endif
(2)
实现代码如下:
#include “Tree.h”
int main(){
char str[52];
Tree t;
cout<<
”
请输入你要构建的树,空节点用空格表示,例如
abd
e
c
”
<
gets(str);
int len=strlen(str);
CreateTree(t,str,len);
cout<<
”
先序遍历
:
”
;
PreOrderTree(t);
cout<
cout<<
”
中序遍历
:
”
;
InOrderTree(t);
cout<
cout<<
”
后序遍历
:
”
;
LastOrderTree(t);
cout<
cout<<
”
层次遍历
:
”
;
LevelOrderTree(t);
cout<
return 0;
}
运行结果如下:
三、实验总结
此次实验,进一步掌握了指针变量,动态变量的含义,
二叉树 的结构特性,以及各种存储结
构的特点和适用范围,掌握用指针类型描述、访问和处理二叉树的运算。< br>
四、教师批阅及成绩
日期:
下午
13
:
00
—
17
:
00
度。全体员工都必须自觉遵守工作时间,实行不定时工作制的员工不必打卡。
3.1.2.2
打卡次数:一日两次,即早上上班打卡一次,下午下班打卡一次。
3.1.2.3
打卡时间:打卡时间为上班到岗时间和下班离岗时间;
3 .1.2.4
因公外出不能打卡:因公外出不能打卡应填写《外勤登记表》
,
注明外出 日期、事由、外勤起止时间。因公外出需事先申请,如因特殊情况不能事先申请,应在事毕到岗当日完成申请、< br>审批手续,否则按旷工处理。因停电、卡钟(工卡)故障未打卡的员工,上班前、下班后要及时到部门考勤 员处填写《未打卡补签申请表》
,由直接主管签字证明当日的出勤状况,报部门经理、
-
-
-
-
-
-
-
-
本文更新与2021-01-25 09:10,由作者提供,不代表本网站立场,转载请注明出处:https://www.bjmy2z.cn/gaokao/565031.html
-
上一篇:(完整版)美国数学建模专业词汇
下一篇:数据结构与算法常用英语词汇