|
|
/---------------------2:1.58 1-------------------------\
| |
| |
/----------4:1.81 0---\ /---------------------9:1.75 0---\
| | | |
| | | |
8:1.74 0 5:1.68 0 1:1.64 1---\ 6:1.72 1
|
|
7:1.55 1
先序遍历序列:
2 1.58 1
4 1.81 0
8 1.74 0
5 1.68 0
9 1.75 0
1 1.64 1
7 1.55 1
6 1.72 1
每位同学的身高增加0.1后的二叉树:
|
|
/---------------------2:1.68 1-------------------------\
| |
| |
/----------4:1.91 0---\ /---------------------9:1.85 0---\
| | | |
| | | |
8:1.84 0 5:1.78 0 1:1.74 1---\ 6:1.82 1
|
|
7:1.65 1
交换左右子树后的二叉树:
|
|
/--------------------------------2:1.68 1--------------\
| |
| |
/----------9:1.85 0--------------\
- //records.txt
- 2 1.58 1
- 4 1.81 0
- 8 1.74 0
- 0
- 0
- 5 1.68 0
- 0
- 0
- 9 1.75 0
- 1 1.64 1
- 0
- 7 1.55 1
- 0
- 0
- 6 1.72 1
- 0
- 0
- #include
- #include
- #include
- typedef struct {
- int xh; /*学号*/
- float sg; /*身高*/
- int sex; /*性别,0为男生,1为女生*/
- } datatype;
- typedef struct node{
- datatype data;
- struct node *left,*right;
- } BiNode,*BiTree;
-
-
- //以下函数声明为二叉树显示,不要求掌握
- #define NODE_WIDTH 11
- #define NODE_HEIGHT 3
- BiNode* createTree();
- BiNode* createTree2(FILE *fp);
- void show_tree(BiTree bt);
- void showBuf(char x[][500],int m, int n);
- void printInBuf(BiTree bt, char buf[][500], int x, int y);
- int getWidth(BiTree bt);
- int getHeight(BiTree bt);
-
- //以下为二叉树显示,不要求掌握
- void show_tree(BiTree bt)
- {
- char buf[50][500];
- int m=50,n=500,i,j; //以二维数组的方式存储二叉树,行m,列n,字符串长度10
- for (i=0; i
- for (j=0; j
- buf[i][j]=0;
- m=getHeight(bt)*NODE_HEIGHT;
- n=getWidth(bt)*NODE_WIDTH;
- printInBuf(bt,buf,0,0);
- showBuf(buf,m,n);
- printf("\n");
- }
-
- void showBuf(char x[][500],int m,int n)
- {
- int i,j;
- for (i=0; i
- {
- for (j=0; j
- printf("%c",x[i][j]=='\0'?' ':x[i][j]);
- printf("\n");
- }
- }
-
- void printInBuf(BiTree bt, char buf[][500], int x, int y)
- {
- int pl,pr,rootpos,i;
- char sv[500];
- char tempsg[5],tempsex[5];
- sprintf(sv,"%d:",bt->data.xh);
- sprintf(tempsg,"%.2f",bt->data.sg);
- sprintf(tempsex,"%d",bt->data.sex);
- strcat(sv,tempsg);
- strcat(sv," ");
- strcat(sv,tempsex);
-
- if (bt->left==NULL)
- pl=0;
- else
- {
- pl=NODE_WIDTH;
- if (bt->left->right!=NULL)
- pl+=getWidth(bt->left->right)*NODE_WIDTH;
- }
-
- if (bt->right==NULL)
- pr=0;
- else
- {
- pr=NODE_WIDTH;
- if (bt->right->left!=NULL)
- pr+=getWidth(bt->right->left)*NODE_WIDTH;
- }
-
- rootpos=x;
- if (bt->left!=NULL)
- rootpos+=getWidth(bt->left)*NODE_WIDTH;
-
- for (i=0; i
- buf[y+i][rootpos]='|';
- for (i=rootpos-pl; i<=rootpos+pr; i++)
- buf[y+NODE_HEIGHT-1][i]='-';
- for (i=rootpos; i
strlen(sv); i++) - buf[y+NODE_HEIGHT-1][i]=sv[i-rootpos];
- if (pl>0)
- buf[y+NODE_HEIGHT-1][rootpos-pl]='/';
- if (pr>0)
- buf[y+NODE_HEIGHT-1][rootpos+pr]='\\';
-
- if (bt->left!=NULL)
- printInBuf(bt->left,buf,x,y+NODE_HEIGHT);
- if (bt->right!=NULL)
- printInBuf(bt->right,buf,rootpos+NODE_WIDTH,y+NODE_HEIGHT);
- }
-
- int getWidth(BiTree bt)
- {
- int w=1;
- if (bt==NULL)
- return 0;
- w += getWidth(bt->left);
- w += getWidth(bt->right);
- return w;
- }
- int getHeight(BiTree bt)
- {
- int h=1,l,r;
- if (bt==NULL)
- return 0;
- l = getHeight(bt->left);
- r = getHeight(bt->right);
- h += l>r ? l : r;
- return h;
- }
-
- BiNode* createTree()
- {
- BiNode* T;
- FILE *fp;
- if((fp=fopen("records.txt","r"))==NULL)
- {
- printf("can not open read file !");
- exit(1) ;
- }
- while(!feof(fp))
- {
- T = createTree2(fp);
- }
- fclose(fp);
- return T;
- }
-
- BiNode* createTree2(FILE *fp)
- {
- BiNode* T;
- datatype stu;
- fscanf(fp,"%d",&stu.xh);
- if (stu.xh==0)
- return NULL;
- fscanf(fp,"%f%d",&stu.sg,&stu.sex);
- T=(BiNode*)malloc(sizeof(BiNode)); /*生成二叉树的根结点*/
- T->data=stu;
- T->left=createTree2(fp); /*递归实现左子树的建立*/
- T->right=createTree2(fp); /*递归实现右子树的建立*/
-
- return T;
- }
- #define _CRT_SECURE_NO_WARNINGS
- /*
- 1.题目要求
- 阅读程序文件sy3.c,调用bitree2.c提供的功能函数(以#include " bitree2.c" 方式导入函数库),从数据文件records.txt中读取学生信息,建立学生信息二叉树并打印在屏幕上。调用自定义的函数完成以下操作:
- 先序遍历二叉树;
- 将二叉树中所有学生的身高增加0.1;
- 二叉树中所有结点的左右子树交换;
- 统计二叉树中度为1的结点个数;
- 统计二叉树中身高达标人数;
- 统计二叉树的深度;
- 在程序文件sy3.c中需再定义以下6个功能函数:*/
- //(1)void preOrder(BiNode * T); /*前序遍历*/
- //(2)void changeHeight(BiNode* T); /*每位同学的身高+0.1*/
- //(3)void exchange(BiNode* T); /*二叉树左右子树交换*/
- //(4)int oneChild(BiNode* T); /*求度为1结点数,并返回*/
- //(5)int count(BiNode* T, float sg); /*求身高高于 sg变量的结点数,并返回*/
- //(6)int deep(BiNode* T); /*求二叉树深度*/
-
- //2.请根据题目功能要求或程序中的注释完成sy3.c代码
-
-
- #include "bitree2.h"
- void preOrder(BiNode* T); /*前序遍历*/
- void changeHeight(BiNode* T); /*每位同学的身高+0.1*/
- void exchange(BiNode* T); /*二叉树左右子树交换*/
- int oneChild(BiNode* T); /*求度为1结点数,并返回*/
- int count(BiNode* T, float sg); /*求身高高于 sg变量的结点数,并返回*/
- int deep(BiNode* T); /*求二叉树深度*/
-
- int main()
- {
- BiNode* root, * tmp;
- root = createTree(); /*利用二叉树前序遍历的结果建成一棵二叉树*/
- show_tree(root); /*二叉树的显示,不要求掌握*/
- printf("先序遍历序列:\n");
- preOrder(root);
-
- printf("\n每位同学的身高增加0.1后的二叉树:\n");
- changeHeight(root);
- show_tree(root);/*二叉树的显示,不要求掌握*/
- printf("\n交换左右子树后的二叉树:\n");
- exchange(root); /*交换二叉树的左右子树*/
- show_tree(root);/*二叉树的显示,不要求掌握*/
- printf("\n度为1结点数=%d", oneChild(root));
- printf("\n身高高于1.67的结点数=%d", count(root, 1.67));
- printf("\n树的深度=%d", deep(root));
-
- return 0;
- }
- /*前序遍历*/
- void preOrder(BiNode* T)
- {
- if (T)
- {
- printf("%d %.2f %d\n", T->data.xh, T->data.sg, T->data.sex); /*打印结点*/
- preOrder(T->left);
- preOrder(T->right);
- }
- }
-
- /*每位同学的身高增加0.1*/
- void changeHeight(BiNode* T)
- {
- if (T)
- {
- T->data.sg += 0.1;
- changeHeight(T->left);
- changeHeight(T->right);
- }
-
- }
- /*二叉树左右子树交换*/
- void exchange(BiNode* T)
- {
- BiNode* tmp;
- if (T)
- {
- BiTree tmp = T->left;
- T->left = T->right;
- T->right = tmp;
- exchange(T->left);
- exchange(T->right);
- }
- }
-
- /*求度为1结点数*/
- int oneChild(BiNode* T)
- {
-
- if (!T)
- {
- return 0;
- }
-
- else if ((T->left == NULL && T->right != NULL) || (T->left != NULL && T->right == NULL))
- {
- return oneChild(T->left) + oneChild(T->right) + 1;
- }
-
- else
- {
- return oneChild(T->left) + oneChild(T->right);
- }
-
- }
-
- /*求身高高于 sg变量的结点数,并返回*/
- int count(BiNode* T, float sg)
- {
-
- if (!T)
- {
- return 0;
- }
- else
- {
- if (T->data.sg > sg)
- {
- return count(T->left, sg) + count(T->right, sg) + 1;
- }
- else
- {
- return count(T->left, sg) + count(T->right, sg);
- }
- }
- }
-
- /*求二叉树深度*/
- int deep(BiNode* T)
- {
- if (!T)
- {
- return 0;
- }
- else
- {
- int left = deep(T->left);
- int right = deep(T->right);
- return (left >= right ? left : right) + 1;
-
- }
-
- }
-
相关阅读:
[附源码]Python计算机毕业设计Django家庭医生签约服务管理系统
python一行命令搭建web服务,实现内网共享文件
Docker项目部署
【开题报告】基于微信小程序的高校党支部党务管理的系统设计与实现
算法设计与分析100例子(C语言版)
基于分水岭分割算法的CT图像智能诊断研究-含Matlab代码
英特尔 Linux Vulkan 驱动程序的首席开发人员离职;JDK 18 功能集被冻结,进入 Rampdown 第一阶段;Ubuntu 禁用 os-prober | 开源日报
Mac文件搜索工具HoudahSpot 6.4.1中文版
银行人总结5个影响系统性能的因素,怕是很多人都会忽略
海丝一号-中国-2020
-
原文地址:https://blog.csdn.net/laocooon/article/details/127780706