二元樹的四種遍歷
二元樹的遍歷把每個結點訪問一遍,常用四種順序:前序(根左右)、中序(左根右)、後序(左右根),區別只在根放在哪;還有層序,用佇列一層一層訪問。
陣列從頭走到尾,訪問順序只有一種。樹不一樣:在一個結點上,是先處理自己,還是先去左邊、再去右邊?順序不同,得到的序列完全不同,而考試幾乎每年都會給一棵樹,讓你寫出它的遍歷序列。
前序、中序、後序:只差「自己」放在哪
三種遞迴遍歷都是「先左子樹,再右子樹」,區別只在訪問根的時機:
- 前序(根左右):先訪問自己,再左、再右。
- 中序(左根右):先左,再訪問自己,再右。
- 後序(左右根):先左、再右,最後訪問自己。
1void preorder(Node *t) {2 if (t == NULL) return;3 printf("%d ", t->val); // 根4 preorder(t->left); // 左5 preorder(t->right); // 右6}
中序和後序只是把 printf 那一行挪到兩次遞迴中間或者最後。
同一棵樹的另外兩種:中序 4 2 5 1 3 6(每個結點都在它左子樹的後面、右子樹的前面),後序 4 5 2 6 3 1(根永遠在最後)。
寫出這棵樹的前序遍歷。
1 A2 / \3 B C4 / \ \5 D E F6 /7 G
還是這棵樹,寫出它的中序遍歷。
1 A2 / \3 B C4 / \ \5 D E F6 /7 G
寫出這棵樹的後序遍歷。
1 A2 / \3 B C4 \ /5 D E6 /7 F
把兩個空格填好,讓 in 成為中序遍歷:先左、再自己、再右。樹是第一關圖裡那棵,程式要顯示 4 2 5 1 3 6。
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int val;6 struct Node *left, *right;7} Node;8 9Node *node(int v, Node *l, Node *r) {10 Node *n = malloc(sizeof(Node));11 n->val = v; n->left = l; n->right = r;12 return n;13}14 15void in(Node *t) {16 if (t == NULL) return;17 ;18 printf("%d ", t->val);19 ;20}21 22int main(void) {23 Node *t = node(1, node(2, node(4, NULL, NULL), node(5, NULL, NULL)),24 node(3, NULL, node(6, NULL, NULL)));25 in(t);26 printf("\n");27 return 0;28}
printf 夾在兩次遞迴中間,前面去左邊,後面去右邊。兩個空格對調就變成「右根左」,輸出 6 3 1 5 2 4。寫後序遍歷
✓補全 post:左、右、最後印出自己。
1 6 3 14 10 8
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int val;6 struct Node *left, *right;7} Node;8 9Node *node(int v, Node *l, Node *r) {10 Node *n = malloc(sizeof(Node));11 n->val = v; n->left = l; n->right = r;12 return n;13}14 15void post(Node *t) {16 // 空樹直接返回;否則先左、再右,最後 printf("%d ", t->val)17}18 19int main(void) {20 Node *t = node(8, node(3, node(1, NULL, NULL), node(6, NULL, NULL)),21 node(10, NULL, node(14, NULL, NULL)));22 post(t);23 printf("\n");24 return 0;25}
表示式樹:三種遍歷就是三種寫法
算式 (3 + 4) * 5 可以存成一棵樹:運算子在內部結點,數字在葉子。對它做三種遍歷,正好得到字首、中綴和字尾表示式,字尾表示式就是第 2 章用堆疊求值的那種。
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 char val;6 struct Node *left, *right;7} Node;8 9Node *node(char v, Node *l, Node *r) {10 Node *n = malloc(sizeof(Node));11 n->val = v;12 n->left = l;13 n->right = r;14 return n;15}16 17void pre(Node *t) {18 if (!t) return;19 printf("%c ", t->val);20 pre(t->left);21 pre(t->right);22}23 24void in(Node *t) {25 if (!t) return;26 in(t->left);27 printf("%c ", t->val);28 in(t->right);29}30 31void post(Node *t) {32 if (!t) return;33 post(t->left);34 post(t->right);35 printf("%c ", t->val);36}37 38int main(void) {39 // (3 + 4) * 540 Node *t = node('*', node('+', node('3', NULL, NULL), node('4', NULL, NULL)), node('5', NULL, NULL));41 printf("前序:");42 pre(t);43 printf("\n中序:");44 in(t);45 printf("\n後序:");46 post(t);47 printf("\n");48 return 0;49}
前序:* + 3 4 5 中序:3 + 4 * 5 後序:3 4 + 5 *
注意中序打印出來是 3 + 4 * 5:括號丟了,按習慣會先算 4 * 5,意思變了。所以中綴表示式要靠括號,而字首和字尾不需要括號也不會有歧義,這就是計算機喜歡字尾表示式的原因。
中序遍歷這棵樹(B 只有左孩子 D),印出什麼?
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 char val;6 struct Node *left, *right;7} Node;8 9Node *node(char v, Node *l, Node *r) {10 Node *n = malloc(sizeof(Node));11 n->val = v; n->left = l; n->right = r;12 return n;13}14 15void in(Node *t) {16 if (!t) return;17 in(t->left);18 printf("%c", t->val);19 in(t->right);20}21 22int main(void) {23 // A24 // B C25 // D E F26 Node *t = node('A', node('B', node('D', NULL, NULL), NULL),27 node('C', node('E', NULL, NULL), node('F', NULL, NULL)));28 in(t);29 printf("\n");30 return 0;31}
這棵表示式樹的後序遍歷(也就是字尾表示式)是什麼?
1 *2 / \3 3 +4 / \5 4 5
* 3 + 4 5 是前序(字首),3 * 4 + 5 是中序,括號丟了以後意思就變成 (3 × 4) + 5。3 4 + 5 * 是 (3 + 4) × 5 那棵樹。字尾表示式 2 3 4 * + 的值是多少?(從左往右讀,數字入堆疊,遇到運算子就彈出兩個數算完再壓回去)
* 彈出 4 和 3,壓回 12;+ 彈出 12 和 2,壓回 14。它對應的表示式樹是根 +、左邊 2、右邊 3 * 4,中綴寫法 2 + 3 × 4。層序:一層一層,用佇列
層序遍歷從根開始一層一層、每層從左往右訪問。它不是遞迴,而是用佇列:根先入隊;每次出隊一個結點,訪問它,再把它的左右孩子(不空的話)入隊。先進來的先處理,所以上一層總是比下一層先出來。
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int val;6 struct Node *left, *right;7} Node;8 9Node *node(int v, Node *l, Node *r) {10 Node *n = malloc(sizeof(Node));11 n->val = v; n->left = l; n->right = r;12 return n;13}14 15void level(Node *t) {16 Node *q[100];17 int head = 0, tail = 0;18 if (t) q[tail++] = t;19 while (head < tail) {20 Node *x = q[head++]; // 出隊21 printf("%d ", x->val);22 if (x->left) q[tail++] = x->left; // 孩子入隊23 if (x->right) q[tail++] = x->right;24 }25 printf("\n");26}27 28int main(void) {29 Node *t = node(1, node(2, node(4, NULL, NULL), node(5, NULL, NULL)),30 node(3, NULL, node(6, NULL, NULL)));31 level(t);32 return 0;33}
1 2 3 4 5 6
| 遍歷 | 順序 | 寫法 | 這棵樹的結果 |
|---|---|---|---|
| 前序 | 根 左 右 | 遞迴,先訪問 | 1 2 4 5 3 6 |
| 中序 | 左 根 右 | 遞迴,中間訪問 | 4 2 5 1 3 6 |
| 後序 | 左 右 根 | 遞迴,最後訪問 | 4 5 2 6 3 1 |
| 層序 | 一層一層 | 佇列 | 1 2 3 4 5 6 |
四種都是每個結點訪問一次,O(n)。遞迴的三種額外用的是呼叫堆疊,深度等於樹高;層序用的是佇列,最多同時放一層的結點。
考試裡: 手推前序的快辦法:從根出發,沿著樹的外圍逆時針繞一圈,第一次經過結點時寫下它;中序是從下面經過時寫,後序是最後一次經過時寫。後序遍歷的最後一個、前序遍歷的第一個,一定是根。
寫出這棵樹的層序遍歷。
1 A2 / \3 B C4 / \ \5 D E F6 /7 G
把 while 裡的這四行排好,讓層序遍歷顯示 1 2 3 4 5 6。樹是第一關圖裡那棵。
if (x->right) q[tail++] = x->right;Node *x = q[head++];if (x->left) q[tail++] = x->left;printf("%d ", x->val);
1 2 3 4 5 6
x,才能訪問它、把它的孩子入隊;左孩子要比右孩子先入隊,同一層才是從左往右。訪問和入隊誰先都可以:輸出一樣。用這一關的佇列做層序遍歷,樹是 4 層的滿二元樹(15 個結點)。佇列裡最多同時有幾個結點?
每層有幾個結點
✓在層序遍歷的基礎上,按層印出每一層的結點個數。辦法:每一輪開始時,佇列裡正好是一整層,先記下 tail - head。
1 2 3 1
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int val;6 struct Node *left, *right;7} Node;8 9Node *node(int v, Node *l, Node *r) {10 Node *n = malloc(sizeof(Node));11 n->val = v; n->left = l; n->right = r;12 return n;13}14 15void widths(Node *t) {16 Node *q[100];17 int head = 0, tail = 0;18 q[tail++] = t;19 while (head < tail) {20 int k = tail - head; // 這一層有 k 個21 printf("%d ", k);22 // 把這 k 個都出隊,並把它們的孩子入隊23 }24 printf("\n");25}26 27int main(void) {28 Node *t = node(1, node(2, node(4, NULL, NULL), node(5, NULL, NULL)),29 node(3, NULL, node(6, node(7, NULL, NULL), NULL)));30 widths(t);31 return 0;32}
小結
四種遍歷:根在哪,或一層一層
- 前序 根左右,中序 左根右,後序 左右根:只差 printf 放在哪
- 表示式樹:前序 = 字首,中序 = 中綴(要括號),後序 = 字尾
- 層序:佇列,出隊一個、孩子入隊
- 都是 O(n);前序第一個、後序最後一個是根
1void in(Node *t) {2 if (!t) return;3 in(t->left);4 printf("%d ", t->val);5 in(t->right);6}