@ -24,7 +24,7 @@ BFS is usually implemented with the help of a queue, as shown in the code below.
2. In each iteration of the loop, pop the vertex at the front of the queue and record it as visited, then add all adjacent vertices of that vertex to the back of the queue.
3. Repeat step `2.` until all vertices have been visited.
To prevent revisiting vertices, we use a hash table`visited` to record which nodes have been visited.
To prevent revisiting vertices, we use a hash set`visited` to record which nodes have been visited.
=== "Python"
@ -528,7 +528,7 @@ The code is relatively abstract, it is suggested to compare with Figure 9-10 to
**Time complexity**: All vertices will be enqueued and dequeued once, using $O(|V|)$ time; in the process of traversing adjacent vertices, since it is an undirected graph, all edges will be visited $2$ times, using $O(2|E|)$ time; overall using $O(|V| + |E|)$ time.
**Space complexity**: The maximum number of vertices in list `res`, hash table`visited`, and queue `que` is $|V|$, using $O(|V|)$ space.
**Space complexity**: The maximum number of vertices in list `res`, hash set`visited`, and queue `que` is $|V|$, using $O(|V|)$ space.
## 9.3.2 Depth-first search
@ -540,7 +540,7 @@ The code is relatively abstract, it is suggested to compare with Figure 9-10 to
### 1. Algorithm implementation
This "go as far as possible and then return" algorithm paradigm is usually implemented based on recursion. Similar to breadth-first search, in depth-first search, we also need the help of a hash table`visited` to record the visited vertices to avoid revisiting.
This "go as far as possible and then return" algorithm paradigm is usually implemented based on recursion. Similar to breadth-first search, in depth-first search, we also need the help of a hash set`visited` to record the visited vertices to avoid revisiting.
=== "Python"
@ -1003,4 +1003,4 @@ To deepen the understanding, it is suggested to combine Figure 9-12 with the cod
**Time complexity**: All vertices will be visited once, using $O(|V|)$ time; all edges will be visited twice, using $O(2|E|)$ time; overall using $O(|V| + |E|)$ time.
**Space complexity**: The maximum number of vertices in list `res`, hash table`visited` is $|V|$, and the maximum recursion depth is $|V|$, therefore using $O(|V|)$ space.
**Space complexity**: The maximum number of vertices in list `res`, hash set`visited` is $|V|$, and the maximum recursion depth is $|V|$, therefore using $O(|V|)$ space.
A <u>binary tree</u> is a non-linear data structure that represents the ancestral and descendent relationships, embodying the "divide and conquer" logic. 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, 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.
=== "Python"
@ -218,7 +218,7 @@ The commonly used terminology of binary trees is shown in Figure 7-2.
- <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 a node's children. In a binary tree, the degree can be 0, 1, or 2.
- 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.
- The <u>height</u> of a node: The number of edges from the farthest leaf node to the node.
@ -229,13 +229,13 @@ The commonly used terminology of binary trees is shown in Figure 7-2.
!!! tip
Please note that we usually define "height" and "depth" as "the number of edges passed," but some problems or textbooks may define them as "the number of nodes passed." In this case, both height and depth need to be incremented by 1.
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.
## 7.1.2 Basic operations of binary trees
### 1. Initializing a binary tree
Similar to a linked list, initialize nodes first, then construct references (pointers).
Similar to a linked list, begin by initialize nodes, then construct references (pointers).
=== "Python"
@ -619,13 +619,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 usually means 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 actions.
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.
## 7.1.3 Common types of binary trees
### 1. Perfect binary tree
As shown in Figure 7-4, 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$, and 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 Figure 7-4, 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.
!!! tip
@ -661,7 +661,7 @@ As shown in Figure 7-7, in a <u>balanced binary tree</u>, the absolute differenc
## 7.1.4 Degeneration of binary trees
Figure 7-8 shows the ideal and degenerate structures of binary trees. When every level of a binary tree is filled, it reaches the "perfect binary tree"; when all nodes are biased towards one side, the binary tree degenerates into a "linked list".
Figure 7-8 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)$.