Compare commits

...

8 commits

Author SHA1 Message Date
砖吐筷筷
de20d32ed6
Merge bc0e32af57 into 894e3d536b 2024-11-25 00:43:05 -03:00
sslmj2020
894e3d536b
Update array_binary_tree.cpp (#1568)
Some checks failed
C++ / build (Release, clang, clang++, ubuntu-latest) (push) Failing after 16s
C++ / build (Release, gcc, g++, ubuntu-latest) (push) Failing after 14s
C++ / build (Release, cl, cl, windows-latest) (push) Has been cancelled
打印的小错误
2024-11-24 00:39:33 +08:00
Yudong Jin
14608d4e92
Update CONTRIBUTING.md (#1565) 2024-11-20 20:38:48 +08:00
Yudong Jin
954169d618
Update CONTRIBUTING.md (#1564) 2024-11-20 19:45:03 +08:00
qinmu
05e0e1d244
translation: chapter_tree/binary_tree.md (#1502)
* doc: translate chapter_tree/binary_tree.md to English

* Update binary_tree.md

---------

Co-authored-by: Yudong Jin <krahets@163.com>
2024-11-17 20:49:36 +08:00
K3v123
fb04ff6535
translation update: Update summary.md (#1549)
* translation update: Update summary.md

added and refined some parts.
I feel like that this chapter is already pretty well translated.

* translation: Update summary.md

rewritten some parts of the sentence as per Thomas suggested.
please note that some parts I re-wrote it a bit differently.

* translation update: summary.md

Added what Ymmma suggested
2024-11-17 20:49:26 +08:00
K3v123
9a2c1355ec
translation: Update index.md (#1548)
* translation: Update index.md

refined some parts of the sentences.

changed 'in' to 'on' as it conveys a sense of movement along a path

* translation: Update index.md

changed some words per Thomas's suggestions.
2024-11-17 20:47:41 +08:00
ztkuaikuai
bc0e32af57 fix(graph): enhance the judgment of boundary conditions for removeEdge functions 2024-07-07 18:40:23 +08:00
9 changed files with 44 additions and 35 deletions

View file

@ -115,9 +115,9 @@ int main() {
int i = 1;
int l = abt.left(i), r = abt.right(i), p = abt.parent(i);
cout << "\n当前节点的索引为 " << i << ",值为 " << abt.val(i) << "\n";
cout << "其左子节点的索引为 " << l << ",值为 " << (l != INT_MAX ? to_string(abt.val(l)) : "nullptr") << "\n";
cout << "其右子节点的索引为 " << r << ",值为 " << (r != INT_MAX ? to_string(abt.val(r)) : "nullptr") << "\n";
cout << "其父节点的索引为 " << p << ",值为 " << (p != INT_MAX ? to_string(abt.val(p)) : "nullptr") << "\n";
cout << "其左子节点的索引为 " << l << ",值为 " << (abt.val(l) != INT_MAX ? to_string(abt.val(l)) : "nullptr") << "\n";
cout << "其右子节点的索引为 " << r << ",值为 " << (abt.val(r) != INT_MAX ? to_string(abt.val(r)) : "nullptr") << "\n";
cout << "其父节点的索引为 " << p << ",值为 " << (abt.val(p) != INT_MAX ? to_string(abt.val(p)) : "nullptr") << "\n";
// 遍历树
vector<int> res = abt.levelOrder();

View file

@ -46,7 +46,8 @@ class GraphAdjList {
if (
!this.adjList.has(vet1) ||
!this.adjList.has(vet2) ||
vet1 === vet2
vet1 === vet2 ||
this.adjList.get(vet1).indexOf(vet2) === -1
) {
throw new Error('Illegal Argument Exception');
}

View file

@ -46,7 +46,8 @@ class GraphAdjList {
if (
!this.adjList.has(vet1) ||
!this.adjList.has(vet2) ||
vet1 === vet2
vet1 === vet2 ||
this.adjList.get(vet1).indexOf(vet2) === -1
) {
throw new Error('Illegal Argument Exception');
}

View file

@ -7,6 +7,8 @@ We are working on translating "Hello Algo" from Chinese to English with the foll
3. **Pull request review**: The optimized translation will be double checked by the reviewers through GitHub pull request workflow.
4. Repeat steps `2.` and `3.` for further improvements.
<img width="650" alt="translation_pipeline" src="https://github.com/user-attachments/assets/201930ef-723e-4179-b670-e5a084a8211e">
## Join us
We're seeking contributors who meet the following criteria.
@ -20,7 +22,10 @@ That is, our contributors are computer scientists, engineers, and students from
- **Native Chinese with professional working English**: Ensuring translation accuracy and consistency between CN and EN versions.
- **Native English**: Enhance the authenticity and fluency of the English content to flow naturally and to be engaging.
Don't hesitate to join us via WeChat `krahets-jyd` or on [Discord](https://discord.gg/nvspS56295)!
> [!note]
> If you are interested in joining us, don't hesitate to contact me via krahetx@gmail.com or WeChat `krahets-jyd`.
>
> We use this [Notion page](https://hello-algo.notion.site/chinese-to-english) to track progress and assign tasks. Please visit it for more details.
## Translation process

View file

@ -4,6 +4,6 @@
!!! abstract
Searching is an unknown adventure, where we may need to traverse every corner of a mysterious space, or perhaps quickly pinpoint our target.
Searching is an adventure into the unknown; where we may need to traverse every corner of a mysterious space, or perhaps well quickly locate our target.
In this journey of discovery, each exploration may yield an unexpected answer.
On this journey of discovery, each exploration may end up with an unexpected answer.

View file

@ -1,8 +1,8 @@
# Summary
- Binary search depends on the order of data and performs the search by iteratively halving the search interval. It requires the input data to be sorted and is only applicable to arrays or array-based data structures.
- Brute force search locates data by traversing the data structure. Linear search is suitable for arrays and linked lists, while breadth-first search and depth-first search are suitable for graphs and trees. These algorithms are highly versatile, requiring no preprocessing of data, but have a higher time complexity of $O(n)$.
- Hash search, tree search, and binary search are efficient searching methods, capable of quickly locating target elements in specific data structures. These algorithms are highly efficient, with time complexities reaching $O(\log n)$ or even $O(1)$, but they usually require additional data structures.
- In practice, we need to analyze factors such as data volume, search performance requirements, data query and update frequencies, etc., to choose the appropriate search method.
- Linear search is suitable for small or frequently updated data; binary search is suitable for large, sorted data; hash search is suitable for scenarios requiring high query efficiency without the need for range queries; tree search is appropriate for large dynamic data that needs to maintain order and support range queries.
- Replacing linear search with hash search is a common strategy to optimize runtime, reducing the time complexity from $O(n)$ to $O(1)$.
- Brute force search may be required to locate an entry in an unordered dataset. Different search algorithms can be applied based on the data structure: Linear search is suitable for arrays and linked lists, while breadth-first search (BFS) and depth-first search (DFS) are suitable for graphs and trees. These algorithms are highly versatile, requiring no preprocessing of data, but they have a higher time complexity of $O(n)$.
- Hash search, tree search, and binary search are efficient search methods that can quickly locate target elements within specific data structures. These algorithms are highly efficient, with time complexities reaching $O(\log n)$ or even $O(1)$, but they usually require extra space to accommodate additional data structures.
- In practice, we need to analyze factors such as data volume, search performance requirements, data query and update frequencies, etc., to choose an appropriate search method.
- Linear search is ideal for small or frequently updated (volatile) data. Binary search works well for large and sorted data. Hash search is suitable for data that requires high query efficiency and does not need range queries. Tree search is best suited for large dynamic data that require maintaining order and need to support range queries.
- Replacing linear search with hash search is a common strategy to optimize runtime performance, reducing the time complexity from $O(n)$ to $O(1)$.

View file

@ -1,6 +1,6 @@
# Binary tree
A <u>binary tree</u> is a non-linear data structure that represents the hierarchical relationship between ancestors and descendants, embodying the divide-and-conquer logic of "splitting into two". Similar to a linked list, the basic unit of a binary tree is a node, each containing a value, a reference to the left child node, and a reference to the right child node.
A <u>binary tree</u> is a non-linear data structure that represents the hierarchical relationship between ancestors and descendants and embodies the divide-and-conquer logic of "splitting into two". Similar to a linked list, the basic unit of a binary tree is a node, and each node contains a value, a reference to its left child node, and a reference to its right child node.
=== "Python"
@ -198,9 +198,9 @@ A <u>binary tree</u> is a non-linear data structure that represents the hierarch
```
Each node has two references (pointers), pointing to the <u>left-child node</u> and <u>right-child node</u>, respectively. This node is called the <u>parent node</u> of these two child nodes. When given a node of a binary tree, we call the tree formed by this node's left child and all nodes under it the <u>left subtree</u> of this node. Similarly, the <u>right subtree</u> can be defined.
Each node has two references (pointers), pointing respectively to the <u>left-child node</u> and <u>right-child node</u>. This node is called the <u>parent node</u> of these two child nodes. When given a node of a binary tree, we call the tree formed by this node's left child and all nodes below it the <u>left subtree</u> of this node. Similarly, the <u>right subtree</u> can be defined.
**In a binary tree, except for leaf nodes, all other nodes contain child nodes and non-empty subtrees.** As shown in the figure below, if "Node 2" is considered as the parent node, then its left and right child nodes are "Node 4" and "Node 5," respectively. The left subtree is "the tree formed by Node 4 and all nodes under it," and the right subtree is "the tree formed by Node 5 and all nodes under it."
**In a binary tree, except leaf nodes, all other nodes contain child nodes and non-empty subtrees.** As shown in the figure below, if "Node 2" is regarded as a parent node, its left and right child nodes are "Node 4" and "Node 5" respectively. The left subtree is formed by "Node 4" and all nodes beneath it, while the right subtree is formed by "Node 5" and all nodes beneath it.
![Parent Node, child Node, subtree](binary_tree.assets/binary_tree_definition.png)
@ -208,26 +208,26 @@ Each node has two references (pointers), pointing to the <u>left-child node</u>
The commonly used terminology of binary trees is shown in the figure below.
- <u>Root node</u>: The node at the top level of the binary tree, which has no parent node.
- <u>Leaf node</u>: A node with no children, both of its pointers point to `None`.
- <u>Edge</u>: The line segment connecting two nodes, i.e., node reference (pointer).
- The <u>level</u> of a node: Incrementing from top to bottom, with the root node's level being 1.
- The <u>degree</u> of a node: The number of children a node has. In a binary tree, the degree can be 0, 1, or 2.
- The <u>height</u> of a binary tree: The number of edges passed from the root node to the farthest leaf node.
- The <u>depth</u> of a node: The number of edges passed from the root node to the node.
- <u>Root node</u>: The node at the top level of a binary tree, which does not have a parent node.
- <u>Leaf node</u>: A node that does not have any child nodes, with both of its pointers pointing to `None`.
- <u>Edge</u>: A line segment that connects two nodes, representing a reference (pointer) between the nodes.
- The <u>level</u> of a node: It increases from top to bottom, with the root node being at level 1.
- The <u>degree</u> of a node: The number of child nodes that a node has. In a binary tree, the degree can be 0, 1, or 2.
- The <u>height</u> of a binary tree: The number of edges from the root node to the farthest leaf node.
- The <u>depth</u> of a node: The number of edges from the root node to the node.
- The <u>height</u> of a node: The number of edges from the farthest leaf node to the node.
![Common Terminology of Binary Trees](binary_tree.assets/binary_tree_terminology.png)
!!! tip
Please note that we typically define "height" and "depth" as "the number of edges traversed", but some problems or textbooks may define them as "the number of nodes traversed". In such cases, both height and depth need to be incremented by 1.
Please note that we usually define "height" and "depth" as "the number of edges traversed", but some questions or textbooks may define them as "the number of nodes traversed". In this case, both height and depth need to be incremented by 1.
## Basic operations of binary trees
### Initializing a binary tree
Similar to a linked list, begin by initialize nodes, then construct references (pointers).
Similar to a linked list, the initialization of a binary tree involves first creating the nodes and then establishing the references (pointers) between them.
=== "Python"
@ -609,13 +609,13 @@ Similar to a linked list, inserting and removing nodes in a binary tree can be a
!!! tip
It's important to note that inserting nodes may change the original logical structure of the binary tree, while removing nodes typically involves removing the node and all its subtrees. Therefore, in a binary tree, insertion and removal are usually performed through a coordinated set of operations to achieve meaningful outcomes.
It should be noted that inserting nodes may change the original logical structure of the binary tree, while removing nodes typically involves removing the node and all its subtrees. Therefore, in a binary tree, insertion and removal are usually performed through a set of operations to achieve meaningful outcomes.
## Common types of binary trees
### Perfect binary tree
As shown in the figure below, in a <u>perfect binary tree</u>, all levels of nodes are fully filled. In a perfect binary tree, the degree of leaf nodes is $0$, while the degree of all other nodes is $2$; if the tree's height is $h$, then the total number of nodes is $2^{h+1} - 1$, showing a standard exponential relationship, reflecting the common phenomenon of cell division in nature.
As shown in the figure below, in a <u>perfect binary tree</u>, all levels are completely filled with nodes. In a perfect binary tree, leaf nodes have a degree of $0$, while all other nodes have a degree of $2$. The total number of nodes can be calculated as $2^{h+1} - 1$, where $h$ is the height of the tree. This exhibits a standard exponential relationship, reflecting the common phenomenon of cell division in nature.
!!! tip
@ -625,19 +625,19 @@ As shown in the figure below, in a <u>perfect binary tree</u>, all levels of nod
### Complete binary tree
As shown in the figure below, a <u>complete binary tree</u> has only the bottom level nodes not fully filled, and the bottom level nodes are filled as far left as possible.
As shown in the figure below, a <u>complete binary tree</u> is a binary tree where only the nodes in the bottom level are not completely filled, and the nodes in the bottom level are filled from left to right as much as possible. Please note that a perfect binary tree is also a complete binary tree.
![Complete binary tree](binary_tree.assets/complete_binary_tree.png)
### Full binary tree
As shown in the figure below, a <u>full binary tree</u> has all nodes except leaf nodes having two children.
As shown in the figure below, a <u>full binary tree</u>, except for the leaf nodes, has two child nodes for all other nodes.
![Full binary tree](binary_tree.assets/full_binary_tree.png)
### Balanced binary tree
As shown in the figure below, in a <u>balanced binary tree</u>, the absolute difference in height between the left and right subtrees of any node does not exceed 1.
As shown in the figure below, in a <u>balanced binary tree</u>, the absolute difference between the height of the left and right subtrees of any node does not exceed 1.
![Balanced binary tree](binary_tree.assets/balanced_binary_tree.png)
@ -645,12 +645,12 @@ As shown in the figure below, in a <u>balanced binary tree</u>, the absolute dif
The figure below shows the ideal and degenerate structures of binary trees. A binary tree becomes a "perfect binary tree" when every level is filled; while it degenerates into a "linked list" when all nodes are biased toward one side.
- The perfect binary tree is the ideal situation, fully leveraging the "divide and conquer" advantage of binary trees.
- A linked list is another extreme, where operations become linear, degrading the time complexity to $O(n)$.
- A perfect binary tree is an ideal scenario where the "divide and conquer" advantage of a binary tree can be fully utilized.
- On the other hand, a linked list represents another extreme where all operations become linear, resulting in a time complexity of $O(n)$.
![The Best and Worst Structures of Binary Trees](binary_tree.assets/binary_tree_best_worst_cases.png)
As shown in the table below, in the best and worst structures, the number of leaf nodes, total number of nodes, and height of the binary tree reach their maximum or minimum values.
As shown in the table below, in the best and worst structures, the binary tree achieves either maximum or minimum values for leaf node count, total number of nodes, and height.
<p align="center"> Table <id> &nbsp; The Best and Worst Structures of Binary Trees </p>

View file

@ -46,7 +46,8 @@ class GraphAdjList {
if (
!this.adjList.has(vet1) ||
!this.adjList.has(vet2) ||
vet1 === vet2
vet1 === vet2 ||
this.adjList.get(vet1).indexOf(vet2) === -1
) {
throw new Error('Illegal Argument Exception');
}

View file

@ -46,7 +46,8 @@ class GraphAdjList {
if (
!this.adjList.has(vet1) ||
!this.adjList.has(vet2) ||
vet1 === vet2
vet1 === vet2 ||
this.adjList.get(vet1).indexOf(vet2) === -1
) {
throw new Error('Illegal Argument Exception');
}