Skip to content

Commit d69174f

Browse files
StoneLyuazl397985856
authored andcommitted
feat: translation for binary-tree-traversal.md (azl397985856#106)
1 parent e5db0e0 commit d69174f

File tree

4 files changed

+170
-42
lines changed

4 files changed

+170
-42
lines changed

README.en.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
---
66

7+
![leetcode.jpeg](./assets/leetcode.jpeg)
8+
9+
This essay records the course of and my emotion to this project from initialisation to 10,000 stars.
10+
[Milestone for 10,000+ stars](./thanksGiving.md)
11+
12+
If you are interested in this project, do not mean your star. This project will be supported for a long enough time by the comminity. Thanks for every audience and contributor.
13+
714
## Introduction
815

916
![leetcode.jpeg](./assets/leetcode.jpeg)
@@ -24,7 +31,13 @@ This repository is divided into five parts for now:
2431

2532
> Only when having mastered the basic data structures and algorithms can you solve complex problems easily.
2633
34+
## About me
35+
36+
I, a programmer, am all passionate about technology.
37+
38+
Used to write `.net` and `Java`, I am a frontend engineer and focused on the engineering, optimization and standardlization for frontend.
2739

40+
If you want to do some contributions or collaborations, just feel free to contact me via [[email protected]].
2841

2942
## Usage Instructions
3043

@@ -218,7 +231,7 @@ The data structures mainly includes:
218231

219232
- [Data Structure](./thinkings/basic-data-structure-en.md) (Drafts)
220233
- [Basic Algorithm](./thinkings/basic-algorithm.md)Drafts
221-
- [Binary Tree Traversal](./thinkings/binary-tree-traversal.md)
234+
- [Binary Tree Traversal](./thinkings/binary-tree-traversal-en.md)
222235
- [Dynamic Programming](./thinkings/dynamic-programming-en.md)
223236
- [Huffman Encode and Run Length Encode](./thinkings/run-length-encode-and-huffman-encode.md)
224237
- [Bloom Filter](./thinkings/bloom-filter.md)

problems/301.remove-invalid-parentheses.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Output: [""]
3838
3939
## 关键点解析
4040

41-
- 广度有限遍历
41+
- 广度优先遍历
4242

4343
- 使用队列简化操作
4444

thinkings/binary-tree-traversal-en.md

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Binary Tree Traversal
2+
3+
## Overview
4+
5+
Binary tree is a fundamental data structure. Traversal is a fundamental algorithm. Implementing traversal on binary tree makes a classical solution to problems. Many problems can be solved by trversaling binary tree, directly or indirectly.
6+
7+
> If you have a good understanding of binary tree, it is not hard for you to understand other trees more complicated.
8+
9+
The traversal of binary tree is basically comprised of in-order, pre-order, post-order and level-order traversals.
10+
11+
A tree is typically traversed in two ways:
12+
13+
- BFS (Breadth First Search, or Level Order Search)
14+
- DFS (Depth First Search)
15+
- In-order (Left-Root-Right)
16+
- Pre-order (Root-Left-Right)
17+
- Post-order (Left-Right-Root)
18+
19+
Some problems can be solved with BFS and DFS, such as [301](../problems\301.remove-invalid-parentheses.md) and [609](../problems\609.find-duplicate-file-in-system.md)
20+
21+
DFS simplifies operations with stack. Meanwhile, a tree is a recursive data structure. It is important to grasp recursion and stack for understanding DFS.
22+
23+
Diagrammatic graph of DFS:
24+
25+
![binary-tree-traversal-dfs](../assets/thinkings/binary-tree-traversal-dfs.gif)
26+
27+
(source: https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/tree/depth-first-search)
28+
29+
The key point of BFS is whether the travesals of each level are completed or not. An identifier bit can be used to represent that.
30+
31+
Then, let's talk about the in-order, pre-oder and post-order traversals.
32+
33+
## Pre-order
34+
35+
example: [144.binary-tree-preorder-traversal](../problems/144.binary-tree-preorder-traversal.md)
36+
37+
order of traversal: `root-left-right`
38+
39+
Algorithm Preorder(tree):
40+
41+
1. visit the root.
42+
43+
2. traverse the left subtree, i.e., call Preorder(left-subtree)
44+
45+
3. traverse the right subtree, i.e., call Preorder(right-subtree)
46+
47+
Summary:
48+
49+
- typically recursive data structure.
50+
- typycally algorithm which simplifies operations by stack.
51+
52+
Actually, at the macro level, it can be described as `visit all left-side chain in sequence from top to bottom, and then, visit all right-side chain from bottom to top`
53+
54+
If we think from this point of view, the algorithm can be different. All nodes in the left-side chain can be visited recursively from top to bottom directly. And all nodes in the right-side chain can be visited with the help of stack.
55+
56+
The whole process is like this:
57+
58+
![binary-tree-traversal-preorder](../assets/thinkings/binary-tree-traversal-preorder.png)
59+
60+
This idea is something like the `backtrack`. It is important, because this idea can help us to `unify the understanding of three traversal methods`.
61+
62+
## In-order
63+
64+
example: [94.binary-tree-inorder-traversal](../problems/94.binary-tree-inorder-traversal.md)
65+
66+
Order of In-order traversal: `Left-Root-Right`
67+
68+
Algorithm Inorder(tree):
69+
70+
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
71+
72+
2. Visit the root
73+
74+
3. Traverse the right subtree, i.e., call Inorder(right-subtree)
75+
76+
It is worth noting that, the result of traversing a BST (Binary Search Tree) is an ordered array. With this property, some problems can be simplified, such as: [230.kth-smallest-element-in-a-bst](../problems/230.kth-smallest-element-in-a-bst.md) and [98.validate-binary-search-tree](../problems/98.validate-binary-search-tree.md)
77+
78+
## Post-order
79+
80+
example: [145.binary-tree-postorder-traversal](../problems/145.binary-tree-postorder-traversal.md)
81+
82+
Order of Post-order: `Left-Right-Root`
83+
84+
This one is a liitle difficult to understand.
85+
86+
But there is a clever trick to post-order traversal which is recording the trversal status of current node.
87+
If
88+
89+
1. current node is leaf node or
90+
2. both left and right subtrees have been traversed
91+
92+
the node can be popped out the stack.
93+
94+
For condition 1, a leaf node is the node with no children (both left and right children are null);
95+
For condition 2, variables are required for recording the traversal status for each node. Due to the stack, only one variable is indispensable bacause this variable can be used.
96+
97+
## Level-order
98+
99+
The key point of level-order is recording the traversal status of each level. An identifier bit can be used to represent the status of current level.
100+
101+
![binary-tree-traversal-bfs](../assets/thinkings/binary-tree-traversal-bfs.gif)
102+
103+
(source: https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/tree/breadth-first-search)
104+
105+
Algorithm Level-order(tree):
106+
107+
1. push root into a queue. And put an identifier node into the queue which is null here.
108+
109+
2. pop out one node from the queue.
110+
111+
3. If the popped out node is not null, which means the traversal of current level is not finished, push the left and right node into the queue orderly.
112+
113+
4. If the popped out node is null, which means the traversal of current level is finished. Now if the queue is null, the traversal is done. If the queue is not null, push a null node into the queue.
114+
115+
example: [102.binary-tree-level-order-traversal](../problems/102.binary-tree-level-order-traversal.md)
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,75 @@
1-
## 游程编码和哈夫曼编码
1+
# 游程编码和哈夫曼编码
22

3-
### huffman encode(哈夫曼编码)
3+
## Huffman encode(哈夫曼编码)
44

5-
huffman 编码的基本思想就是用短的编码表示长的字符序列,用长的编码来表示短的字符序列,从而实现压缩的目的。
6-
因此huffman编码被广泛地应用于无损压缩领域
5+
Huffman 编码的基本思想就是用短的编码表示出现频率高的字符,用长的编码来表示出现频率低的字符,这使得编码之后的字符串的平均长度、长度的期望值降低,从而实现压缩的目的。
6+
因此 Huffman 编码被广泛地应用于无损压缩领域
77

8-
上面提到了他的基本原理就是`用短的编码表示长的字符序列,用长的编码来表示短的字符序列`,
9-
因此首先要做的就是统一字符序列的出现频率,然后根据统计的频率来构建huffman树(又叫最优二叉树)。
8+
Huffman 编码的过程包含两个主要部分:
109

11-
![huffman-tree](../assets/thinkings/huffman-tree.webp)
10+
- 根据输入字符构建 Huffman 树
11+
- 遍历 Huffman 树,并将树的节点分配给字符
1212

13-
huffman 树就像是一个堆。 真正执行编码的时候,类似字典树,节点不用来编码,节点的路径用来编码
13+
上面提到了他的基本原理就是`用短的编码表示出现频率高的字符,用长的编码来表示出现频率低的字符`,
14+
因此首先要做的就是统计字符的出现频率,然后根据统计的频率来构建 Huffman 树(又叫最优二叉树)。
1415

15-
> 节点的值只是用来构建huffman 树
16+
![Huffman-tree](../assets/thinkings/huffman-tree.webp)
17+
18+
Huffman 树就像是一个堆。真正执行编码的时候,类似字典树,节点不用来编码,节点的路径用来编码.
19+
20+
> 节点的值只是用来构建 Huffman 树
1621
1722
eg:
1823

1924
我们统计的结果如下:
2025

21-
```
22-
character frequency
23-
a 5
24-
b 9
25-
c 12
26-
d 13
27-
e 16
28-
f 45
29-
30-
```
26+
|character|frequency|
27+
|:--:|:--:|
28+
|a|5|
29+
|b|9|
30+
|c|12|
31+
|d|13|
32+
|e|16|
33+
|f|45|
3134

3235
- 将每个元素构造成一个节点,即只有一个元素的树。并构建一个最小堆,包含所有的节点,该算法用了最小堆来作为优先队列。
3336

34-
- `选取两个权值最小的节点`,并添加一个权值为5+9=14的节点,作为他们的父节点。并`更新最小堆`
35-
现在最小堆包含5个节点,其中4个树还是原来的节点,权值为5和9的节点合并为一个。
37+
- `选取两个权值最小的节点`,并添加一个权值为5+9=14的节点,作为他们的父节点。并`更新最小堆`,现在最小堆包含5个节点,其中4个树还是原来的节点,权值为5和9的节点合并为一个。
3638

3739
结果是这样的:
3840

3941
![huffman-example](../assets/thinkings/huffman-example.png)
4042

41-
```
42-
character frequency encodeing
43-
a 5 1100
44-
b 9 1110
45-
c 12 100
46-
d 13 101
47-
e 16 111
48-
f 45 0
49-
50-
```
43+
|character|frequency|encoding|
44+
|:-:|:-:|:-:|
45+
|a|5|1100|
46+
|b|9|1110|
47+
|c|12|100|
48+
|d|13|101|
49+
|e|16|111|
50+
|f|45|0|
5151

52+
## run-length encode(游程编码)
5253

53-
### run-length encode(游程编码)
5454
游程编码是一种比较简单的压缩算法,其基本思想是将重复且连续出现多次的字符使用(连续出现次数,某个字符)来描述。
5555

5656
比如一个字符串:
5757

58-
```
58+
```text
5959
AAAAABBBBCCC
6060
```
61+
6162
使用游程编码可以将其描述为:
6263

63-
```
64+
```text
6465
5A4B3C
6566
```
66-
5A表示这个地方有5个连续的A,同理4B表示有4个连续的B,3C表示有3个连续的C,其它情况以此类推。
6767

68+
5A表示这个地方有5个连续的A,同理4B表示有4个连续的B,3C表示有3个连续的C,其它情况以此类推。
6869

69-
但是实际上情况可能会非常复杂, 如何提取自序列有时候没有看的那么简单,还是上面的例子,我们
70+
但是实际上情况可能会非常复杂, 如何提取子序列有时候没有看的那么简单,还是上面的例子,我们
7071
有时候可以把`AAAAABBBBCCC`整体看成一个子序列, 更复杂的情况还有很多,这里不做扩展。
7172

72-
7373
对文件进行压缩比较适合的情况是文件内的二进制有大量的连续重复,
7474
一个经典的例子就是具有大面积色块的BMP图像,BMP因为没有压缩,
7575
所以看到的是什么样子存储的时候二进制就是什么样子
@@ -79,10 +79,10 @@ AAAAABBBBCCC
7979
> 思考一个问题, 如果我们在CDN上存储两个图片,这两个图片几乎完全一样,我们是否可以进行优化呢?
8080
这虽然是CDN厂商更应该关心的问题,但是这个问题对我们影响依然很大,值得思考
8181

82-
### 总结
82+
## 总结
8383

84-
实际情况,我们先用游程编码一遍,然后再用huffman 再次编码一次。
84+
实际情况,我们先用游程编码一遍,然后再用 Huffman 再次编码一次。
8585

86-
### 相关题目
86+
## 相关题目
8787

8888
[900.rle-iterator](../problems/900.rle-iterator.md)

0 commit comments

Comments
 (0)