Skip to content

Commit 10d0e4e

Browse files
varopxndxpoyea
andauthored
docs: Fix quicksort & binary tree traversal doc (TheAlgorithms#4878)
* Fix quicksort doc * add binary tree traversals doc * Add link to the reference * Fix job * Change url * Update binary_tree_traversals.md * Update normal_distribution_quick_sort.md * Update normal_distribution_quick_sort.md Co-authored-by: John Law <johnlaw.po@gmail.com>
1 parent 1400cb8 commit 10d0e4e

File tree

2 files changed

+124
-32
lines changed

2 files changed

+124
-32
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Binary Tree Traversal
2+
3+
## Overview
4+
5+
The combination of binary trees being data structures and traversal being an algorithm relates to classic problems, either directly or indirectly.
6+
7+
> If you can grasp the traversal of binary trees, the traversal of other complicated trees will be easy for you.
8+
9+
The following are some common ways to traverse trees.
10+
11+
- Depth First Traversals (DFS): In-order, Pre-order, Post-order
12+
13+
- Level Order Traversal or Breadth First or Traversal (BFS)
14+
15+
There are applications for both DFS and BFS.
16+
17+
Stack can be used to simplify the process of DFS traversal. Besides, since tree is a recursive data structure, recursion and stack are two key points for DFS.
18+
19+
Graph for DFS:
20+
21+
![binary-tree-traversal-dfs](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhzhynsg30dw0dw3yl.gif)
22+
23+
The key point of BFS is how to determine whether the traversal of each level has been completed. The answer is to use a variable as a flag to represent the end of the traversal of current level.
24+
25+
## Pre-order Traversal
26+
27+
The traversal order of pre-order traversal is `root-left-right`.
28+
29+
Algorithm Pre-order
30+
31+
1. Visit the root node and push it into a stack.
32+
33+
2. Pop a node from the stack, and push its right and left child node into the stack respectively.
34+
35+
3. Repeat step 2.
36+
37+
Conclusion: This problem involves the classic recursive data structure (i.e. a binary tree), and the algorithm above demonstrates how a simplified solution can be reached by using a stack.
38+
39+
If you look at the bigger picture, you'll find that the process of traversal is as followed. `Visit the left subtrees respectively from top to bottom, and visit the right subtrees respectively from bottom to top`. If we are to implement it from this perspective, things will be somewhat different. For the `top to bottom` part we can simply use recursion, and for the `bottom to top` part we can turn to stack.
40+
41+
## In-order Traversal
42+
43+
The traversal order of in-order traversal is `left-root-right`.
44+
45+
So the root node is not printed first. Things are getting a bit complicated here.
46+
47+
Algorithm In-order
48+
49+
1. Visit the root and push it into a stack.
50+
51+
2. If there is a left child node, push it into the stack. Repeat this process until a leaf node reached.
52+
53+
> At this point the root node and all the left nodes are in the stack.
54+
55+
3. Start popping nodes from the stack. If a node has a right child node, push the child node into the stack. Repeat step 2.
56+
57+
It's worth pointing out that the in-order traversal of a binary search tree (BST) is a sorted array, which is helpful for coming up simplified solutions for some problems.
58+
59+
## Post-order Traversal
60+
61+
The traversal order of post-order traversal is `left-right-root`.
62+
63+
This one is a bit of a challenge. It deserves the `hard` tag of LeetCode.
64+
65+
In this case, the root node is printed not as the first but the last one. A cunning way to do it is to:
66+
67+
Record whether the current node has been visited. If 1) it's a leaf node or 2) both its left and right subtrees have been traversed, then it can be popped from the stack.
68+
69+
As for `1) it's a leaf node`, you can easily tell whether a node is a leaf if both its left and right are `null`.
70+
71+
As for `2) both its left and right subtrees have been traversed`, we only need a variable to record whether a node has been visited or not. In the worst case, we need to record the status for every single node and the space complexity is `O(n)`. But if you come to think about it, as we are using a stack and start printing the result from the leaf nodes, it makes sense that we only record the status for the current node popping from the stack, reducing the space complexity to `O(1)`.
72+
73+
## Level Order Traversal
74+
75+
The key point of level order traversal is how do we know whether the traversal of each level is done. The answer is that we use a variable as a flag representing the end of the traversal of the current level.
76+
77+
![binary-tree-traversal-bfs](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlui1tpoug30dw0dw3yl.gif)
78+
79+
Algorithm Level-order
80+
81+
1. Visit the root node, put it in a FIFO queue, put in the queue a special flag (we are using `null` here).
82+
83+
2. Dequeue a node.
84+
85+
3. If the node equals `null`, it means that all nodes of the current level have been visited. If the queue is empty, we do nothing. Or else we put in another `null`.
86+
87+
4. If the node is not `null`, meaning the traversal of current level has not finished yet, we enqueue its left subtree and right subtree respectively.
88+
89+
## Bi-color marking
90+
91+
We know that there is a tri-color marking in garbage collection algorithm, which works as described below.
92+
93+
- The white color represents "not visited".
94+
95+
- The gray color represents "not all child nodes visited".
96+
97+
- The black color represents "all child nodes visited".
98+
99+
Enlightened by tri-color marking, a bi-color marking method can be invented to solve all three traversal problems with one solution.
100+
101+
The core idea is as follow.
102+
103+
- Use a color to mark whether a node has been visited or not. Nodes yet to be visited are marked as white and visited nodes are marked as gray.
104+
105+
- If we are visiting a white node, turn it into gray, and push its right child node, itself, and it's left child node into the stack respectively.
106+
107+
- If we are visiting a gray node, print it.
108+
109+
Implementation of pre-order and post-order traversal algorithms can be easily done by changing the order of pushing the child nodes into the stack.
110+
111+
Reference: [LeetCode](https://github.com/azl397985856/leetcode/blob/master/thinkings/binary-tree-traversal.en.md)
+13-32
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
# Normal Distribution QuickSort
22

3+
QuickSort Algorithm where the pivot element is chosen randomly between first and last elements of the array, and the array elements are taken from Standard Normal Distribution.
34

4-
Algorithm implementing QuickSort Algorithm where the pivot element is chosen randomly between first and last elements of the array and the array elements are taken from a Standard Normal Distribution.
5-
This is different from the ordinary quicksort in the sense, that it applies more to real life problems , where elements usually follow a normal distribution. Also the pivot is randomized to make it a more generic one.
5+
## Array elements
66

7+
The array elements are taken from a Standard Normal Distribution, having mean = 0 and standard deviation = 1.
78

8-
## Array Elements
9-
10-
The array elements are taken from a Standard Normal Distribution , having mean = 0 and standard deviation 1.
11-
12-
#### The code
9+
### The code
1310

1411
```python
1512

@@ -27,49 +24,33 @@ The array elements are taken from a Standard Normal Distribution , having mean =
2724

2825
------
2926

30-
#### The Distribution of the Array elements.
27+
#### The distribution of the array elements
3128

3229
```python
3330
>>> mu, sigma = 0, 1 # mean and standard deviation
3431
>>> s = np.random.normal(mu, sigma, p)
3532
>>> count, bins, ignored = plt.hist(s, 30, normed=True)
3633
>>> plt.plot(bins , 1/(sigma * np.sqrt(2 * np.pi)) *np.exp( - (bins - mu)**2 / (2 * sigma**2) ),linewidth=2, color='r')
3734
>>> plt.show()
38-
3935
```
4036

37+
------
38+
![normal distribution large](https://upload.wikimedia.org/wikipedia/commons/thumb/2/25/The_Normal_Distribution.svg/1280px-The_Normal_Distribution.svg.png)
4139

42-
-----
43-
44-
45-
46-
47-
![](https://www.mathsisfun.com/data/images/normal-distrubution-large.gif)
48-
49-
---
50-
51-
---------------------
40+
------
5241

53-
--
42+
## Comparing the numbers of comparisons
5443

55-
## Plotting the function for Checking 'The Number of Comparisons' taking place between Normal Distribution QuickSort and Ordinary QuickSort
44+
We can plot the function for Checking 'The Number of Comparisons' taking place between Normal Distribution QuickSort and Ordinary QuickSort:
5645

5746
```python
58-
>>>import matplotlib.pyplot as plt
47+
>>> import matplotlib.pyplot as plt
5948

60-
61-
# Normal Disrtibution QuickSort is red
49+
# Normal Distribution QuickSort is red
6250
>>> plt.plot([1,2,4,16,32,64,128,256,512,1024,2048],[1,1,6,15,43,136,340,800,2156,6821,16325],linewidth=2, color='r')
6351

64-
#Ordinary QuickSort is green
52+
# Ordinary QuickSort is green
6553
>>> plt.plot([1,2,4,16,32,64,128,256,512,1024,2048],[1,1,4,16,67,122,362,949,2131,5086,12866],linewidth=2, color='g')
6654

6755
>>> plt.show()
68-
6956
```
70-
71-
72-
----
73-
74-
75-
------------------

0 commit comments

Comments
 (0)