树与二叉树
我不知道你是不是被标题骗进来的,但是来了就看看吧
相信所有人都见过树,树可以长这样:
C++ 复制 1
/ \
2 3
/ \ / \
4 5 6 7
图1(满二叉树)
也可以长这样:
C++ 复制 1
/ \
2 3
/
4
图2(普通树)
以上的也是十分的美丽啊
要想构建一棵树,其实很简单
我们拿图1来解释
首先,观察到 1 这个位置,它是没有父亲节点的
我们把这种没有父亲节点的叫做 ※根节点※
找根节点是至关重要的一步
然后连接 1 的两个点 2, 3,我们把这种点叫做※子节点※
表示为:1 的子节点是 2 和 3
然后继续往下,到 2 这个位置
它是有父亲节点 1 的,而且有两个子节点 4,5
我们把这种点叫做※父亲节点※
然后一直遍历下去即可
知识科普完了,该怎么表示他们的关系呢
我们可以想到用 结构体 的方式来存储,具体表达方式如下:
C++ 复制 struct ly {
int f, l, r;
} a[1001 ];
变量 f 表示父亲节点,l 表示 左子结点,r 表示 右子节点
C++ 复制 for (int i= 1 ;i<= n;i++ ) {
int x, y, id;
cin >> id >> x >> y;
a[id].l = x;
a[id].r = y;
a[x].f = id;
a[y].f = id;
}
就这样即可建树,也是十分的 so easy 啊各位
OK进入下一个环节,树的遍历
1. 先序遍历
简单来说,先序是这样一个顺序:根 左 右
按照图 1 的形状,先序遍历为这样:
C++ 复制 1 2 4 5 3 6 7
比较简单,只要理解 根 左 右 这个顺序就行了
2. 中序遍历
简单来说,中序是这样一个顺序:左 根 右
按照图 1 的形状,中序遍历为这样:
C++ 复制 4 2 5 1 6 3 7
比较简单,只要理解 左 根 右 这个顺序就行了
3. 中序遍历
简单来说,后序是这样一个顺序:左 右 根
按照图 1 的形状,后序遍历为这样:
C++ 复制 4 5 2 6 7 3 1
比较简单,只要理解 左 右 根 这个顺序就行了
OK,我们来尝试一下题目
#3443——二叉树的遍历
链接:
参考答案:
C++ 复制 #include <bits/stdc++.h>
#define int long long
#define fastio ios ::sync_with_stdio (0 ), cin.tie (0 ), cout.tie (0 )
#define rep (z , x , y ) for (int (z) = (x); (z) <= (y); (z)++ )
#define drep (z , x , y ) for (int (z) = (x); (z) >= (y); (z)-- )
using namespace std ;
int n, ans, sum;
struct ly {
int f, l, r;
} a[101 ];
void dfs1 (int x ) {
if (x == 0 ) {
return ;
}
cout << x << " " ;
dfs1 (a[x].l);
dfs1 (a[x].r);
}
void dfs2 (int x ) {
if (x == 0 ) {
return ;
}
dfs2 (a[x].l);
cout << x << " " ;
dfs2 (a[x].r);
}
void dfs3 (int x ) {
if (x == 0 ) {
return ;
}
dfs3 (a[x].l);
dfs3 (a[x].r);
cout << x << " " ;
}
signed main () {
fastio;
cin >> n;
rep (i, 1 , n) {
int x, y, id;
cin >> id >> x >> y;
a[id].l = x;
a[id].r = y;
a[x].f = id;
a[y].f = id;
}
rep (i, 1 , n) {
if (a[i].f == 0 ) {
cout << i << endl;
sum = i;
break ;
}
}
dfs1 (sum);
cout << endl;
dfs2 (sum);
cout << endl;
dfs3 (sum);
cout << endl;
return 0 ;
}
OK过
再给一个例题,课后去做一做
#3338——找树根和孩子
参考答案:
C++ 复制 #include <bits/stdc++.h>
#define int long long
#define fastio ios ::sync_with_stdio (0 ), cin.tie (0 ), cout.tie (0 )
#define rep (z , x , y ) for (int (z) = (x); (z) <= (y); (z)++ )
#define drep (z , x , y ) for (int (z) = (x); (z) >= (y); (z)-- )
using namespace std ;
struct ly {
int f;
vector<int> c;
} a[1005 ];
int root, maxn, t;
signed main () {
fastio;
int n, m;
cin >> n >> m;
rep (i, 1 , m) {
int x, y;
cin >> x >> y;
a[x].c.push_back (y);
a[y].f = x;
}
rep (i, 1 , m) {
if (a[i].f == 0 ) {
root = i;
}
if (maxn < a[i].c.size ()) {
maxn = a[i].c.size ();
t = i;
}
}
cout << root << endl << t << endl;
sort (a[t].c.begin (), a[t].c.end ());
rep (i, 0 , a[t].c.size () - 1 ) { cout << a[t].c[i] << " " ; }
return 0 ;
}
ok,下一个章节:
隐式树
献给例题:
这题如你所见,给你中序遍历和层次遍历的字符串,让你求先序字符串
很多新手小白在这里就缴械投降了,但是,我不一样,我会指导你!
( ⊙ o ⊙ )啊!好感动啊
首先,我们注意到其中有按层遍历,图 1 的按层遍历序列为:
C++ 复制 1 2 3 4 5 6 7
注意到按层遍历的第一个数字会是根结点
于是,我们可以在中序遍历的序列中找到这个根节点,按照 左 根 右 的顺序进行回溯,我们就可以 AC 此题
参考答案如下:
C++ 复制 #include <bits/stdc++.h>
#define int long long
#define fastio ios ::sync_with_stdio (0 ), cin.tie (0 ), cout.tie (0 )
#define rep (z , x , y ) for (int (z) = (x); (z) <= (y); (z)++ )
#define drep (z , x , y ) for (int (z) = (x); (z) >= (y); (z)-- )
using namespace std ;
string a, b;
void f (int l , int r ) {
if (l > r)
return ;
rep (i, 0 , b.size () - 1 ) {
rep (j, l, r) {
if (b[i] == a[j]) {
int mid = j;
cout << a[mid];
f (l, mid - 1 );
f (mid + 1 , r);
return ;
}
}
}
}
signed main () {
fastio;
cin >> a >> b;
f (0 , a.size () - 1 );
return 0 ;
}
举一反三:
#2912——求二叉树后序遍历
链接:
这个题跟刚才那个题的本质差不太多,应该能做对
参考答案:
C++ 复制 #include <bits/stdc++.h>
#define int long long
#define fastio ios ::sync_with_stdio (0 ), cin.tie (0 ), cout.tie (0 )
#define rep (z , x , y ) for (int (z) = (x); (z) <= (y); (z)++ )
#define drep (z , x , y ) for (int (z) = (x); (z) >= (y); (z)-- )
using namespace std ;
string a, b;
void f (int l , int r ) {
if (l > r) {
return ;
}
rep (i, 0 , b.size () - 1 ) {
rep (j, l, r) {
if (a[i] == b[j]) {
int mid = j;
f (l, mid - 1 );
f (mid + 1 , r);
cout << b[mid];
return ;
}
}
}
}
signed main () {
fastio;
cin >> a >> b;
f (0 , a.size () - 1 );
return 0 ;
}
我觉得你应该拿下了此知识点,于是,下一个知识点接踵而至
哈夫曼树
先给例题:
C++ 复制 题目描述
给定N个权值作为N个叶子结点,构造一棵二叉树,若该树的带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman Tree)。
现在,给定N个叶子结点的信息,请你构造哈夫曼树,并输出该树的带权路径长度。
相关知识:
1 、路径和路径长度
在一棵树中,从一个结点往下可以达到的孩子或孙子结点之间的通路,称为路径。通路中分支的数目称为路径长度。若规定根结点的层数为T,则从根结点到第T层结点的路径长度为 。
2 、结点的权及带权路径长度
若将树中结点赋给一个有着某种含义的数值,则这个数值称为该结点的权。结点的带权路径长度为:从根结点到该结点之间的路径长度与该结点的权的乘积。
3 、树的带权路径长度
树的带权路径长度规定为所有叶子结点的带权路径长度之和,记为 WPL。
输入格式
第一行包含整数N,表示叶子结点数量。
第二行包含N个整数,表示每个叶子结点的权值。
输出格式
输出一个整数,表示生成哈夫曼树的带权路径长度。
样例
样例输入
5
1 2 2 5 9
样例输出
37
其实哈夫曼树的根本这个题目已经给你讲述出来了,这里也不在过多赘述,直接上代码:
C++ 复制 #include <bits/stdc++.h>
#define rep (z , x , y ) for (int (z) = (x); (z) <= (y); (z)++ )
#define drep (z , x , y ) for (int (z) = (x); (z) >= (y); (z)-- )
using namespace std ;
priority_queue<int , vector<int> , greater<int>> q;
int n, ans;
int main () {
ios ::sync_with_stdio (0 );
cin.tie (0 );
cin >> n;
for (int i = 0 , x; i < n; i++ ) {
cin >> x;
q.push (x);
}
while (q.size () > 1 ) {
int a = q.top ();
q.pop ();
int b = q.top ();
q.pop ();
int sum = a + b;
ans += sum;
q.push (sum);
}
cout << ans << " \n " ;
return 0 ;
}
细心的小朋友可以发现,这里面用了一个陌生的东西:
C++ 复制 priority_queue<int , vector<int> , greater<int>> q;
我知道!这东西叫※优先队列※
名称 意义 q.push(x) 入队 q.top() 队首元素 q.pop() 弹出队首元素
这不就是普通的队列吗
嘿嘿,当然不是啦,优先队列可以有两种:
1. 大根堆
C++ 复制 priority_queue<int> q;
在里面写直接的“int”,表示队内元素按照从大到小排序
2. 小根堆
C++ 复制 priority_queue<int , vector<int> , greater<int>> q;
在里面写上超多的东西,表示队内元素按照从小到大排序
这个是有极大的用处滴!!!
我们拿例题来看他的用处
#6358——堆
这个题目是优先队列的几个基本步骤,我有讲过,所以直接给代码:
C++ 复制 #include <bits/stdc++.h>
#define rep (z , x , y ) for (int (z) = (x); (z) <= (y); (z)++ )
#define drep (z , x , y ) for (int (z) = (x); (z) >= (y); (z)-- )
using namespace std ;
priority_queue<int , vector<int> , greater<int>> q;
int n, ans;
int main () {
ios ::sync_with_stdio (0 );
cin.tie (0 );
cin >> n;
rep (i, 1 , n) {
int op;
cin >> op;
if (op == 1 ) {
int x;
cin >> x;
q.push (x);
}
if (op == 2 ) {
cout << q.top () << endl;
}
if (op == 3 ) {
q.pop ();
}
}
return 0 ;
}
下一题:
这个题用大根堆,然后再用个数组就行了,还是比较简单的
C++ 复制 #include <bits/stdc++.h>
#define rep (z , x , y ) for (int (z) = (x); (z) <= (y); (z)++ )
#define drep (z , x , y ) for (int (z) = (x); (z) >= (y); (z)-- )
using namespace std ;
priority_queue<int> q;
signed main () {
ios ::sync_with_stdio (0 );
cin.tie (0 );
int n, m, a, b, c;
cin >> n >> m >> a >> b >> c;
rep (i, 1 , m) { q.push (a * i * i + b * i + c); }
rep (i, 2 , n) {
cin >> a >> b >> c;
rep (j, 1 , m) {
if (a * j * j + b * j + c < q.top ()) {
q.push (a * j * j + b * j + c);
q.pop ();
} else {
break ;
}
}
}
int ans[100000 ];
rep (i, 1 , m) {
ans[i] = q.top ();
q.pop ();
}
drep (i, m, 1 ) { cout << ans[i] << " " ; }
return 0 ;
}
结构体排序(优先队列版)
结构体排序有 N 种模版,我只介绍一种:
C++ 复制 struct ly {
string name;
int pr;
bool operator < (const ly & x )const {
return pr< x.pr;
}
}a[100001 ];
priority_queue< ly> q;
这个需要注意两个加一个地方
1. operator(不要忘记怎么写!)
这个后面要写 “<” 注意,const 可写可不写,这个是优化时间复杂度的
2.return pr<x.pr
这里写小于号相当于 大根堆,写大于号相当于 大根堆
3. priority_queue
重要的事情说三遍
考试不要忘记怎么写!!!
考试不要忘记怎么写!!!
考试不要忘记怎么写!!!
因为作者遭过,所以我要 予人玫瑰,手有余香
看一道例题:
#3350——看病
不会有人模版放在那儿都不会抄吧
参考答案:
C++ 复制 #include <bits/stdc++.h>
#define rep (z , x , y ) for (int (z) = (x); (z) <= (y); (z)++ )
#define drep (z , x , y ) for (int (z) = (x); (z) >= (y); (z)-- )
using namespace std ;
struct ly {
string name;
int pr;
bool operator < (const ly & x ) const { return pr < x.pr; }
} a[100001 ];
priority_queue< ly> q;
signed main () {
ios ::sync_with_stdio (0 );
cin.tie (0 );
int n;
cin >> n;
rep (i, 1 , n) {
string m;
cin >> m;
if (m == "pop" ) {
if (q.empty ()) {
cout << "none" << endl;
continue ;
}
cout << q.top ().name << " " << q.top ().pr << endl;
q.pop ();
}
if (m == "push" ) {
cin >> a[i].name >> a[i].pr;
q.push ({ a[i].name, a[i].pr });
}
}
return 0 ;
}
优先队列就讲到这,感兴趣的同学可以再去多多练习,我可以给几个例题
匆匆啊,这就讲完了,呜呜呜,被资本做局了
再见!!!