This commit is contained in:
krahets
2024-05-02 01:46:14 +08:00
parent a08cd961b3
commit 6d966b8b5d
30 changed files with 97 additions and 97 deletions
+15 -15
View File
@@ -4,7 +4,7 @@ comments: true
# 7.1   Binary tree
A "binary tree" 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 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.
=== "Python"
@@ -202,7 +202,7 @@ A "binary tree" is a non-linear data structure that represents the ancestral and
```
Each node has two references (pointers), pointing to the "left-child node" and "right-child node," respectively. This node is called the "parent node" 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 "left subtree" of this node. Similarly, the "right subtree" can be defined.
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.
**In a binary tree, except for leaf nodes, all other nodes contain child nodes and non-empty subtrees.** As shown in Figure 7-1, 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."
@@ -214,14 +214,14 @@ Each node has two references (pointers), pointing to the "left-child node" and "
The commonly used terminology of binary trees is shown in Figure 7-2.
- "Root node": The node at the top level of the binary tree, which has no parent node.
- "Leaf node": A node with no children, both of its pointers point to `None`.
- "Edge": The line segment connecting two nodes, i.e., node reference (pointer).
- The "level" of a node: Incrementing from top to bottom, with the root node's level being 1.
- The "degree" of a node: The number of a node's children. In a binary tree, the degree can be 0, 1, or 2.
- The "height" of a binary tree: The number of edges passed from the root node to the farthest leaf node.
- The "depth" of a node: The number of edges passed from the root node to the node.
- The "height" of a node: The number of edges from the farthest leaf node to the node.
- <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 a node's children. 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.
![Common Terminology of Binary Trees](binary_tree.assets/binary_tree_terminology.png){ class="animation-figure" }
@@ -625,11 +625,11 @@ Similar to a linked list, inserting and removing nodes in a binary tree can be a
### 1. &nbsp; Perfect binary tree
As shown in Figure 7-4, in a "perfect binary tree," 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$, 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.
!!! tip
Please note that in the Chinese community, a perfect binary tree is often referred to as a "full binary tree."
Please note that in the Chinese community, a perfect binary tree is often referred to as a <u>full binary tree</u>.
![Perfect binary tree](binary_tree.assets/perfect_binary_tree.png){ class="animation-figure" }
@@ -637,7 +637,7 @@ As shown in Figure 7-4, in a "perfect binary tree," all levels of nodes are full
### 2. &nbsp; Complete binary tree
As shown in Figure 7-5, a "complete binary tree" has only the bottom level nodes not fully filled, and the bottom level nodes are filled as far left as possible.
As shown in Figure 7-5, 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.
![Complete binary tree](binary_tree.assets/complete_binary_tree.png){ class="animation-figure" }
@@ -645,7 +645,7 @@ As shown in Figure 7-5, a "complete binary tree" has only the bottom level nodes
### 3. &nbsp; Full binary tree
As shown in Figure 7-6, a "full binary tree" has all nodes except leaf nodes having two children.
As shown in Figure 7-6, a <u>full binary tree</u> has all nodes except leaf nodes having two children.
![Full binary tree](binary_tree.assets/full_binary_tree.png){ class="animation-figure" }
@@ -653,7 +653,7 @@ As shown in Figure 7-6, a "full binary tree" has all nodes except leaf nodes hav
### 4. &nbsp; Balanced binary tree
As shown in Figure 7-7, in a "balanced binary tree," the absolute difference in height between the left and right subtrees of any node does not exceed 1.
As shown in Figure 7-7, 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.
![Balanced binary tree](binary_tree.assets/balanced_binary_tree.png){ class="animation-figure" }