浙大2020年Mooc数据结构笔记–第三讲 树(下)

〇、前言

  • 这几天开始跟着学数据结构,鉴于当初数据结构实在学的太弱,加之这项工作算是为大家之后复习、机试铺路。确实是一个迫切需要做的大规模工作。行胜于言,虽然从第二篇开始,坚持下去。

  • 此文部分为自己写,部分参考网上内容。提前说一下哈。期待批评指正。

一、堆

二、堆的基本操作

  • MaxHeap Create( int MaxSize ):创建一个空的最大堆。
  • Boolean IsFull( MaxHeap H ):判断最大堆H是否已满。
  • Insert( MaxHeap H, ElementType item ):将元素item插入最大堆H。
  • Boolean IsEmpty( MaxHeap H ):判断最大堆H是否为空。
  • ElementType DeleteMax( MaxHeap H ):返回H中最大元素(高优先级)。
typedef  struct HeapStruct *MaxHeap;
struct HeapStruct{ElementType *Elements;  //存储堆元素的数组int Size;                   //堆的当前元素个数int Capacity;           //堆的最大容量
};MaxHeap Create(int MaxSize) {MaxHeap H  =   (MaxHeap)malloc(sizeof(struct HeapStruct));H->Elements = (ElementType*)malloc((MaxSize + 1) * sizeof(ElementType));H->Size = 0;H->Capacity = MaxSize;int MaxData = 0;H->Elements[0] = MaxData;return H;
}
bool IsFull(MaxHeap H) {if (H->Capacity == H->Size)return 1;return 0;
}bool IsEmpty(MaxHeap H) {if (0 == H->Size)return 1;return 0;
}void Insert(MaxHeap H, ElementType item) {int i;if (IsFull(H)) {printf("最大堆已满");return;}i = ++H->Size;for (; H->Elements[i / 2] < item; i /= 2) {H->Elements[i] = H->Elements[i / 2];}H->Elements[i] = item;
}ElementType DeleteMax(MaxHeap H) {int Parent, Child;ElementType MaxItem, temp;if (IsEmpty(H)) {printf("最大堆已空");return;}MaxItem = H->Elements[1];//取出根节点的最大值temp = H->Elements[H->Size--];for (Parent = 1; Parent * 2 <= H->Size; Parent = Child) {Child = Parent * 2;if ((Child != H->Size) && (H->Elements[Child] < H->Elements[Child + 1])) {Child++;} else {H->Elements[Parent] = H->Elements[Child];}}H->Elements[Parent] = temp;return MaxItem;}

三、哈夫曼树

四、集合及运算

  • 集合运算:交、并、补、差,判定一个元素是否属于某一集合
  • 并查集:集合并、查某元素属于什么集合
  • 并查集问题中集合存储如何实现?
  • 可以用树结构表示集合,树的每个结点代表一个集合元素
#include <iostream>
#define ElementType int typedef struct MyStruct
{ElementType Data;int Parent;
}SetType;//查找某个元素所在的集合
int Find(SetType S[], ElementType X) {int i;      //在数组S 中查找值为X的元素所属的集合//MaxSize是全局变量,为数组S的最大长度for (int i = 0; i < MaxSize && S[i].Data != X; i++) {if (i >= MaxSize) {return -1;}}for (; S[i].Parent >= 0; i = S[i].Parent);return i;
}//集合的并运算
void Union(SetType S[], ElementType X1, ElementType X2) {int Root1, Root2;Root1 = Find(S, X1);Root2 = Find(S, X2);if (Root1 != Root2) {S[Root2].Parent = Root1;}
}

五、堆中的路径

六、课后题

1、05-树7 堆中的路径 (25分)


输入样例:

5 3
46 23 26 24 10
5 4 3

输出样例:

24 23 10
46 23 10
26 10

#include <stdio.h> #define MAXN 1001
#define MINH -10001int H[MAXN];
int size;
void Create()
{size = 0;H[0] = MINH;/*设置“岗哨”*/
}void Insert(int X)
{/* 将X插入H。这里省略检查堆是否已满的代码 */int i;for (i = ++size; H[i / 2] > X; i /= 2)H[i] = H[i / 2];H[i] = X;
}int main()
{int n, m, x, i, j;scanf("%d %d", &n, &m);Create(); /* 堆初始化 */for (i = 0; i < n; i++) { /*以逐个插入方式建堆 */scanf("%d", &x);Insert(x);}for (i = 0; i < m; i++) {scanf("%d", &j);printf("%d", H[j]);while (j > 1) { /*沿根方向输出各结点*/j /= 2;printf(" %d", H[j]);}printf("\n");}return 0;
}

2、05-树8 File Transfer (25分)


Sample Input 1:

5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
S

Sample Output 1:

no
no
yes
There are 2 components.

Sample Input 2:

5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
I 1 3
C 1 5
S

Sample Output 2:

no
no
yes
yes
The network is connected.

点击这里有详细解释哦

#include<stdio.h>
#define MaxSize 10001typedef int ElementType;    /*默认元素可以用非负整数表示*/
typedef int SetName;    /*默认用根结点的下标作为集合名称*/
typedef ElementType SetType[MaxSize];SetName Find(SetType S, ElementType X) {while (S[X] >= 0) {X = S[X];}return X;
}void Union(SetType S,SetName Root1,SetName Root2) { //使用树根的负数的绝对值来确定规模的大小if (S[Root2] < S[Root1]) {S[Root2] += S[Root1];S[Root1] = Root2;}else {S[Root1] += S[Root2];S[Root2] = Root1;}
}void Input_connection(SetType S) {ElementType u, v;SetName Root1, Root2;scanf("%d %d\n", &u, &v);Root1 = Find(S, u - 1);        //找到该点所在的根节点Root2 = Find(S, v - 1);        if (Root1 != Root2) {Union(S, Root1, Root2);}
}void Check_connection(SetType S) {ElementType u, v;SetName Root1, Root2;scanf("%d %d\n", &u, &v);Root1 = Find(S, u - 1);        //找到该点所在的根节点Root2 = Find(S, v - 1);if (Root1 == Root2)printf("yes\n");else printf("no\n");
}void Initalization(SetType S, int n) {int i;for (i = 0; i < n; i++) {S[i] = -1;}
}void Check_network(SetType S, int n) {int i, counter = 0;for (i = 0; i < n; i++) {if (S[i] < 0) counter++;}if (counter == 1)printf("The network is connected.\n");elseprintf("There are %d components.\n", counter);
}int main() {SetType S; //并查集Sint n;            //n为集合里面元素的个数char in;scanf("%d\n", &n);Initalization( S, n );do {scanf("%c", &in);switch (in){case 'I': Input_connection(S) ; break;case 'C': Check_connection(S); break;case 'S':Check_network(S, n); break;}} while (in != 'S');return 0;
}

3、05-树9 Huffman Codes (30分)



Sample Input:

7
A 1 B 1 C 1 D 3 E 3 F 6 G 6
4
A 00000
B 00001
C 0001
D 001
E 01
F 10
G 11
A 01010
B 01011
C 0100
D 011
E 10
F 11
G 00
A 000
B 001
C 010
D 011
E 100
F 101
G 110
A 00000
B 00001
C 0001
D 001
E 00
F 10
G 11

Sample Output:

Yes
Yes
No
No

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Maxn 64int N, w[Maxn];
char ch[Maxn];
int codelen, cnt1, cnt2;typedef struct TreeNode* Tree;
struct TreeNode {int Weight;Tree Left, Right;
};typedef struct HeapNode* Heap;
struct HeapNode {struct TreeNode Data[Maxn];int Size;
};Tree CreatTree() {Tree T;T = (Tree)malloc(sizeof(struct TreeNode));T->Left = T->Right = NULL;T->Weight = 0;return T;
}Heap CreatHeap() {Heap H;H = (Heap)malloc(sizeof(struct HeapNode));H->Size = 0;H->Data[0].Weight = -1;return H;
}void Insert(Heap H, struct TreeNode T) {int i = ++H->Size;for( ; T.Weight < H->Data[i/2].Weight; i /= 2)H->Data[i] = H->Data[i/2];H->Data[i] = T;
}Tree Delete(Heap H) {int parent, child;struct TreeNode Temp = H->Data[H->Size--];Tree T = CreatTree();*T = H->Data[1];for(parent = 1; 2*parent <= H->Size; parent = child) {child = 2 * parent;if(child != H->Size && H->Data[child].Weight > H->Data[child+1].Weight) child++;if(Temp.Weight < H->Data[child].Weight) break;H->Data[parent] = H->Data[child];}H->Data[parent] = Temp;return T;
}Tree Huffman(Heap H) {Tree T = CreatTree(); //分配空间while(H->Size != 1) {T->Left = Delete(H);T->Right = Delete(H);T->Weight = T->Left->Weight + T->Right->Weight;Insert(H, *T);}T = Delete(H);return T;
}int WPL(Tree T, int Depth) {if(!T->Left && !T->Right) { return Depth*T->Weight; }else return WPL(T->Left, Depth+1) + WPL(T->Right, Depth+1);
}void JudgeTree(Tree T) {if(T) {if(T->Left && T->Right) cnt2++;else if(!T->Left && !T->Right) cnt1++;else cnt1 = 0;JudgeTree(T->Left);JudgeTree(T->Right);}
}int Judge() {int i, j, wgh, flag = 1;char s1[Maxn], s2[Maxn];Tree T = CreatTree(), pt = NULL;for(i = 0; i < N; i++) {scanf("%s%s", s1, s2);if(strlen(s2) > N) return 0;for(j = 0; s1[0] != ch[j]; j++); wgh = w[j];pt = T;//每次建树前先将指针移动到根节点上;for(j = 0; s2[j]; j++) {if(s2[j] == '0') {if(!pt->Left) pt->Left = CreatTree();pt = pt->Left;}if(s2[j] == '1') {if(!pt->Right) pt->Right = CreatTree();pt = pt->Right;}if(pt->Weight) flag = 0;if(!s2[j+1]) {if(pt->Left || pt->Right) flag = 0;//判断是否为前缀pt->Weight = wgh;}}}if(!flag) return 0;cnt1 = cnt2 = 0;JudgeTree(T);//判断是否不存在度数1的节点if(cnt1 != cnt2 + 1) return 0;if(codelen == WPL(T, 0)) return 1;else return 0;
}int main() {int i, n;Heap H;Tree T;H = CreatHeap();T = CreatTree();scanf("%d", &N);for(i = 0; i < N; i++) {getchar();scanf("%c %d", &ch[i], &w[i]);H->Data[H->Size].Left = H->Data[H->Size].Right = NULL;T->Weight = w[i];Insert(H, *T);}T = Huffman(H);     //PreTravel(T);codelen = WPL(T, 0);scanf("%d", &n);while(n--) {if(Judge()) printf("Yes\n");else printf("No\n");}return 0;
}

浙大2020年Mooc数据结构笔记--第三讲 树(下)相关推荐

  1. 【数据结构笔记27】树习题:完全二叉搜索树(Complete Binary Search Tree)

    本次笔记内容: 树习题-CBST. 1 数据结构的选择 树习题-CBST. 2 核心算法 树习题-CBST. 3 计算左子树的规模 文章目录 题意理解 分析:用链表还是数组表示树 核心算法 核心递归算 ...

  2. 【数据结构笔记】B树和B+树的实现,哈希查找,STL中的hash_map和unordered_map容器用法

    B和B+树 哈希查找 用开放定址法解决哈希冲突的哈希查找算法 链地址法: 利用哈希表查找一个字符串中第一个只出现一次的字符 hash_map和unordered_map 设计算法删除重复的元素 设计算 ...

  3. 数据结构笔记五_树(c++超详细版)

    目录 树 1.树的概念 1.1 树的逻辑结构和基本运算 1.1.1 树的定义 1.1.2 树的常见基本操作 1.2 树的物理结构 2.二叉树 2.1 二叉树的概念 2.1.1 二叉树的定义 2.1.2 ...

  4. 陈越《数据结构》第三讲 树(上)

    3.1 树与树的表示 3.1.1 引子:查找 分层次组织在管理上具有更高的效率! 查找 \color{red}{查找} 1. 定义: 根据某个给定 关键字K ,从 集合R 中找出关键字与K 相同的记录 ...

  5. 视觉SLAM十四讲学习笔记-第三讲-相似、仿射、射影变换和eigen程序、可视化演示

    专栏系列文章如下: 视觉SLAM十四讲学习笔记-第一讲_goldqiu的博客-CSDN博客 视觉SLAM十四讲学习笔记-第二讲-初识SLAM_goldqiu的博客-CSDN博客 视觉SLAM十四讲学习 ...

  6. 数据结构笔记(王道考研) 第八章:排序

    大部分内容基于中国大学MOOC的2021考研数据结构课程所做的笔记,该课属于付费课程(不过盗版网盘资源也不难找...).后续又根据23年考研的大纲对内容做了一些调整,将二叉排序树和平衡二叉树的内容挪到 ...

  7. 数据结构笔记(王道考研) 第五章:树和二叉树

    大部分内容基于中国大学MOOC的2021考研数据结构课程所做的笔记,该课属于付费课程(不过盗版网盘资源也不难找...).后续又根据23年考研的大纲对内容做了一些调整,将二叉排序树和平衡二叉树的内容挪到 ...

  8. 数据结构笔记(王道考研) 第一章:绪论

    大部分内容基于中国大学MOOC的2021考研数据结构课程所做的笔记,该课属于付费课程(不过盗版网盘资源也不难找...).后续又根据23年考研的大纲对内容做了一些调整,将二叉排序树和平衡二叉树的内容挪到 ...

  9. 王道视频-数据结构-笔记6:图

    文章目录 0 笔记说明 1 图的基本概念 2 图的存储及基本操作 2.1 邻接矩阵法 2.2 邻接表法 2.3 十字链表法 2.4 邻接多重表法 2.5 图的基本操作 3 图的遍历 3.1 广度优先搜 ...

最新文章

  1. 可视化-grafana_使用influxDB数据
  2. Commons-logging + Log4j 入门指南
  3. 前端白屏问题_前端优化-如何计算白屏和首屏时间
  4. LIVE555再学习 -- 初识
  5. MongoDB 之 手把手教你增删改查 MongoDB - 2
  6. 网易云信携手“瑶台”,打造元宇宙商业化实践标杆案例
  7. access里面的表达式运用_Access表达式解析
  8. CF-1238E. Keyboard Purchase (状压dp)
  9. createBindingContext in SAP UI5
  10. 【jQuery笔记Part1】12-jQuery元素的角标
  11. xhtml、html与html5的区别
  12. Layui 数据表格动态cols(字段)动态变化
  13. java-线程同步问题
  14. java kafka 开发,Kafka JAVA API开发-基础案例
  15. Linux ls -l 各字段解释,硬链接软连接
  16. 2022【高淇Java300集】,零基础必备课程,全新知识讲解
  17. 简单易操作Jsp动态网页
  18. 磕碰,擦伤了,紧急处理方法
  19. 短信验证码平台有哪些比较好用?
  20. 2015深圳实习感悟

热门文章

  1. libcurl-windows下静态库版本的编译-编译和调用非常顺利
  2. 百度轻应用接入android应用,【畅谈百度轻应用】+轻应用时代
  3. 日常随笔: React useEffect中使用异步更新数据方法遇到的问题
  4. JavaScript jQuery
  5. cnpm的安装与使用
  6. golang桌面应用入门,基于github.com/lxn/walk开发
  7. 2022最新H5盲盒交友系统
  8. 修改服务器lldp的mac地址,通过命令行界面(CLI)配置链路层发现协议(LLDP)在交换机的端口设置...
  9. RobotFramework-Ride安装步骤
  10. slf4j与log4j