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.
=== "Python"
```python title=""
class TreeNode:
"""Binary tree node"""
def __init__(self, val: int):
self.val: int = val # Node value
self.left: TreeNode | None = None # Reference to left child node
self.right: TreeNode | None = None # Reference to right child node
this.val = val === undefined ? 0 : val; // Node value
this.left = left === undefined ? null : left; // Reference to left child node
this.right = right === undefined ? null : right; // Reference to right child node
}
}
```
=== "Dart"
```dart title=""
/* Binary tree node */
class TreeNode {
int val; // Node value
TreeNode? left; // Reference to left child node
TreeNode? right; // Reference to right child node
TreeNode(this.val, [this.left, this.right]);
}
```
=== "Rust"
```rust title=""
use std::rc::Rc;
use std::cell::RefCell;
/* Binary tree node */
struct TreeNode {
val: i32, // Node value
left: Option<Rc<RefCell<TreeNode>>>, // Reference to left child node
right: Option<Rc<RefCell<TreeNode>>>, // Reference to right child node
}
impl TreeNode {
/* 构造方法 */
fn new(val: i32) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self {
val,
left: None,
right: None
}))
}
}
```
=== "C"
```c title=""
/* Binary tree node */
typedef struct TreeNode {
int val; // Node value
int height; // 节点高度
struct TreeNode *left; // Pointer to left child node
struct TreeNode *right; // Pointer to right child node
} TreeNode;
/* 构造函数 */
TreeNode *newTreeNode(int val) {
TreeNode *node;
node = (TreeNode *)malloc(sizeof(TreeNode));
node->val = val;
node->height = 0;
node->left = NULL;
node->right = NULL;
return node;
}
```
=== "Kotlin"
```kotlin title=""
/* Binary tree node */
class TreeNode(val _val: Int) { // Node value
val left: TreeNode? = null // Reference to left child node
val right: TreeNode? = null // Reference to right child node
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""
```
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.
**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."
The commonly used terminology of binary trees is shown in the following figure.
- "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.
![Common Terminology of Binary Trees](binary_tree.assets/binary_tree_terminology.png)
!!! 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.
## Basic operations of binary trees
### Initializing a binary tree
Similar to a linked list, initialize nodes first, then construct references (pointers).
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.
## Common types of binary trees
### Perfect binary tree
As shown in the figure below, 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.
!!! tip
Please note that in the Chinese community, a perfect binary tree is often referred to as a "full binary tree."
As shown in the figure below, 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 the figure below, in a "balanced binary tree," the absolute difference in height between the left and right subtrees of any node does not exceed 1.
The figure below 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".
- 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)$.
![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.
<palign="center"> Table <id> The Best and Worst Structures of Binary Trees </p>