mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-27 15:02:16 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -1,26 +1,26 @@
|
||||
# Binary Tree
|
||||
# Binary trees
|
||||
|
||||
A <u>binary tree</u> is a non-linear data structure that models the hierarchical relationship between "ancestors" and "descendants" and embodies a divide-and-conquer pattern in which each split branches 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.
|
||||
A <u>binary tree</u> is a nonlinear data structure representing relationships between ancestors and descendants. It embodies the divide-and-conquer idea of splitting in two. Like a linked list, it is built from nodes, each containing a value and references to its left and right children.
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title=""
|
||||
class TreeNode:
|
||||
"""Binary tree node"""
|
||||
"""Binary tree node class"""
|
||||
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
|
||||
self.left: TreeNode | None = None # Left child reference
|
||||
self.right: TreeNode | None = None # Right child reference
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
/* Binary tree node */
|
||||
/* Binary tree node struct */
|
||||
struct TreeNode {
|
||||
int val; // Node value
|
||||
TreeNode *left; // Pointer to left child node
|
||||
TreeNode *right; // Pointer to right child node
|
||||
TreeNode *left; // Left child pointer
|
||||
TreeNode *right; // Right child pointer
|
||||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
};
|
||||
```
|
||||
@@ -28,11 +28,11 @@ A <u>binary tree</u> is a non-linear data structure that models the hierarchical
|
||||
=== "Java"
|
||||
|
||||
```java title=""
|
||||
/* Binary tree node */
|
||||
/* Binary tree node class */
|
||||
class TreeNode {
|
||||
int val; // Node value
|
||||
TreeNode left; // Reference to left child node
|
||||
TreeNode right; // Reference to right child node
|
||||
TreeNode left; // Left child reference
|
||||
TreeNode right; // Right child reference
|
||||
TreeNode(int x) { val = x; }
|
||||
}
|
||||
```
|
||||
@@ -40,18 +40,18 @@ A <u>binary tree</u> is a non-linear data structure that models the hierarchical
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
/* Binary tree node */
|
||||
/* Binary tree node class */
|
||||
class TreeNode(int? x) {
|
||||
public int? val = x; // Node value
|
||||
public TreeNode? left; // Reference to left child node
|
||||
public TreeNode? right; // Reference to right child node
|
||||
public TreeNode? left; // Left child reference
|
||||
public TreeNode? right; // Right child reference
|
||||
}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title=""
|
||||
/* Binary tree node */
|
||||
/* Binary tree node struct */
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
@@ -60,8 +60,8 @@ A <u>binary tree</u> is a non-linear data structure that models the hierarchical
|
||||
/* Constructor */
|
||||
func NewTreeNode(v int) *TreeNode {
|
||||
return &TreeNode{
|
||||
Left: nil, // Pointer to left child node
|
||||
Right: nil, // Pointer to right child node
|
||||
Left: nil, // Left child pointer
|
||||
Right: nil, // Right child pointer
|
||||
Val: v, // Node value
|
||||
}
|
||||
}
|
||||
@@ -70,11 +70,11 @@ A <u>binary tree</u> is a non-linear data structure that models the hierarchical
|
||||
=== "Swift"
|
||||
|
||||
```swift title=""
|
||||
/* Binary tree node */
|
||||
/* Binary tree node class */
|
||||
class TreeNode {
|
||||
var val: Int // Node value
|
||||
var left: TreeNode? // Reference to left child node
|
||||
var right: TreeNode? // Reference to right child node
|
||||
var left: TreeNode? // Left child reference
|
||||
var right: TreeNode? // Right child reference
|
||||
|
||||
init(x: Int) {
|
||||
val = x
|
||||
@@ -85,11 +85,11 @@ A <u>binary tree</u> is a non-linear data structure that models the hierarchical
|
||||
=== "JS"
|
||||
|
||||
```javascript title=""
|
||||
/* Binary tree node */
|
||||
/* Binary tree node class */
|
||||
class TreeNode {
|
||||
val; // Node value
|
||||
left; // Pointer to left child node
|
||||
right; // Pointer to right child node
|
||||
left; // Left child pointer
|
||||
right; // Right child pointer
|
||||
constructor(val, left, right) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.left = left === undefined ? null : left;
|
||||
@@ -101,7 +101,7 @@ A <u>binary tree</u> is a non-linear data structure that models the hierarchical
|
||||
=== "TS"
|
||||
|
||||
```typescript title=""
|
||||
/* Binary tree node */
|
||||
/* Binary tree node class */
|
||||
class TreeNode {
|
||||
val: number;
|
||||
left: TreeNode | null;
|
||||
@@ -109,8 +109,8 @@ A <u>binary tree</u> is a non-linear data structure that models the hierarchical
|
||||
|
||||
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
|
||||
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
|
||||
this.left = left === undefined ? null : left; // Left child reference
|
||||
this.right = right === undefined ? null : right; // Right child reference
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -118,11 +118,11 @@ A <u>binary tree</u> is a non-linear data structure that models the hierarchical
|
||||
=== "Dart"
|
||||
|
||||
```dart title=""
|
||||
/* Binary tree node */
|
||||
/* Binary tree node class */
|
||||
class TreeNode {
|
||||
int val; // Node value
|
||||
TreeNode? left; // Reference to left child node
|
||||
TreeNode? right; // Reference to right child node
|
||||
TreeNode? left; // Left child reference
|
||||
TreeNode? right; // Right child reference
|
||||
TreeNode(this.val, [this.left, this.right]);
|
||||
}
|
||||
```
|
||||
@@ -133,11 +133,11 @@ A <u>binary tree</u> is a non-linear data structure that models the hierarchical
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
|
||||
/* Binary tree node */
|
||||
/* Binary tree node struct */
|
||||
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
|
||||
left: Option<Rc<RefCell<TreeNode>>>, // Left child reference
|
||||
right: Option<Rc<RefCell<TreeNode>>>, // Right child reference
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
@@ -155,12 +155,12 @@ A <u>binary tree</u> is a non-linear data structure that models the hierarchical
|
||||
=== "C"
|
||||
|
||||
```c title=""
|
||||
/* Binary tree node */
|
||||
/* Binary tree node struct */
|
||||
typedef struct TreeNode {
|
||||
int val; // Node value
|
||||
int height; // Node height
|
||||
struct TreeNode *left; // Pointer to left child node
|
||||
struct TreeNode *right; // Pointer to right child node
|
||||
struct TreeNode *left; // Left child pointer
|
||||
struct TreeNode *right; // Right child pointer
|
||||
} TreeNode;
|
||||
|
||||
/* Constructor */
|
||||
@@ -179,10 +179,10 @@ A <u>binary tree</u> is a non-linear data structure that models the hierarchical
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title=""
|
||||
/* Binary tree node */
|
||||
/* Binary tree node class */
|
||||
class TreeNode(val _val: Int) { // Node value
|
||||
var left: TreeNode? = null // Reference to left child node
|
||||
var right: TreeNode? = null // Reference to right child node
|
||||
var left: TreeNode? = null // Left child reference
|
||||
var right: TreeNode? = null // Right child reference
|
||||
}
|
||||
```
|
||||
|
||||
@@ -192,8 +192,8 @@ A <u>binary tree</u> is a non-linear data structure that models the hierarchical
|
||||
### Binary tree node class ###
|
||||
class TreeNode
|
||||
attr_accessor :val # Node value
|
||||
attr_accessor :left # Reference to left child node
|
||||
attr_accessor :right # Reference to right child node
|
||||
attr_accessor :left # Left child reference
|
||||
attr_accessor :right # Right child reference
|
||||
|
||||
def initialize(val)
|
||||
@val = val
|
||||
@@ -201,48 +201,48 @@ A <u>binary tree</u> is a non-linear data structure that models the hierarchical
|
||||
end
|
||||
```
|
||||
|
||||
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.
|
||||
Each node has two references or pointers, one to its <u>left child</u> and one to its <u>right child</u>. The node is their <u>parent</u>. Its left child and all nodes below that child form its <u>left subtree</u>; the <u>right subtree</u> is defined similarly.
|
||||
|
||||
**In a binary tree, every non-leaf node has child nodes and therefore 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.
|
||||
**Every non-leaf node has children and at least one nonempty subtree**. In the figure below, if node 2 is the parent, its left and right children are nodes 4 and 5. Its left subtree consists of node 4 and everything below it, and its right subtree consists of node 5 and everything below it.
|
||||
|
||||

|
||||

|
||||
|
||||
## Common Terminology of Binary Trees
|
||||
## Common terminology
|
||||
|
||||
The commonly used terminology of binary trees is shown in the figure below.
|
||||
The figure below illustrates common binary-tree terminology.
|
||||
|
||||
- <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.
|
||||
- <u>Root</u>: the topmost node, which has no parent.
|
||||
- <u>Leaf</u>: a node with no children; both of its child references point to `None`.
|
||||
- <u>Edge</u>: a connection between two nodes, represented by a reference or pointer.
|
||||
- A node's <u>level</u>: levels increase from top to bottom, with the root at level 1.
|
||||
- A node's <u>degree</u>: its number of children, which is 0, 1, or 2 in a binary tree.
|
||||
- A tree's <u>height</u>: the number of edges from the root to its farthest leaf.
|
||||
- A node's <u>depth</u>: the number of edges from the root to that node.
|
||||
- A node's <u>height</u>: the number of edges from that node to its farthest leaf.
|
||||
|
||||

|
||||

|
||||
|
||||
!!! tip
|
||||
|
||||
We usually define "height" and "depth" as the number of edges traversed, but some textbooks and problem statements define them as the number of nodes on the path. In that case, both values are larger by 1.
|
||||
Height and depth are usually measured in edges, but some problems or textbooks count nodes instead. Under that convention, add 1 to both height and depth.
|
||||
|
||||
## Basic Operations of Binary Trees
|
||||
## Basic operations
|
||||
|
||||
### Initializing a Binary Tree
|
||||
### Initializing a binary tree
|
||||
|
||||
Similar to a linked list, the initialization of a binary tree involves first creating the nodes and then establishing the references (pointers) between them.
|
||||
As with a linked list, first create the nodes, then connect them with references or pointers.
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree.py"
|
||||
# Initializing a binary tree
|
||||
# Initializing nodes
|
||||
# Initialize a binary tree
|
||||
# Initialize nodes
|
||||
n1 = TreeNode(val=1)
|
||||
n2 = TreeNode(val=2)
|
||||
n3 = TreeNode(val=3)
|
||||
n4 = TreeNode(val=4)
|
||||
n5 = TreeNode(val=5)
|
||||
# Linking references (pointers) between nodes
|
||||
# Connect nodes with references (pointers)
|
||||
n1.left = n2
|
||||
n1.right = n3
|
||||
n2.left = n4
|
||||
@@ -252,14 +252,14 @@ Similar to a linked list, the initialization of a binary tree involves first cre
|
||||
=== "C++"
|
||||
|
||||
```cpp title="binary_tree.cpp"
|
||||
/* Initializing a binary tree */
|
||||
// Initializing nodes
|
||||
/* Initialize a binary tree */
|
||||
// Initialize nodes
|
||||
TreeNode* n1 = new TreeNode(1);
|
||||
TreeNode* n2 = new TreeNode(2);
|
||||
TreeNode* n3 = new TreeNode(3);
|
||||
TreeNode* n4 = new TreeNode(4);
|
||||
TreeNode* n5 = new TreeNode(5);
|
||||
// Linking references (pointers) between nodes
|
||||
// Connect nodes with references (pointers)
|
||||
n1->left = n2;
|
||||
n1->right = n3;
|
||||
n2->left = n4;
|
||||
@@ -269,13 +269,13 @@ Similar to a linked list, the initialization of a binary tree involves first cre
|
||||
=== "Java"
|
||||
|
||||
```java title="binary_tree.java"
|
||||
// Initializing nodes
|
||||
// Initialize nodes
|
||||
TreeNode n1 = new TreeNode(1);
|
||||
TreeNode n2 = new TreeNode(2);
|
||||
TreeNode n3 = new TreeNode(3);
|
||||
TreeNode n4 = new TreeNode(4);
|
||||
TreeNode n5 = new TreeNode(5);
|
||||
// Linking references (pointers) between nodes
|
||||
// Connect nodes with references (pointers)
|
||||
n1.left = n2;
|
||||
n1.right = n3;
|
||||
n2.left = n4;
|
||||
@@ -285,14 +285,14 @@ Similar to a linked list, the initialization of a binary tree involves first cre
|
||||
=== "C#"
|
||||
|
||||
```csharp title="binary_tree.cs"
|
||||
/* Initializing a binary tree */
|
||||
// Initializing nodes
|
||||
/* Initialize a binary tree */
|
||||
// Initialize nodes
|
||||
TreeNode n1 = new(1);
|
||||
TreeNode n2 = new(2);
|
||||
TreeNode n3 = new(3);
|
||||
TreeNode n4 = new(4);
|
||||
TreeNode n5 = new(5);
|
||||
// Linking references (pointers) between nodes
|
||||
// Connect nodes with references (pointers)
|
||||
n1.left = n2;
|
||||
n1.right = n3;
|
||||
n2.left = n4;
|
||||
@@ -302,14 +302,14 @@ Similar to a linked list, the initialization of a binary tree involves first cre
|
||||
=== "Go"
|
||||
|
||||
```go title="binary_tree.go"
|
||||
/* Initializing a binary tree */
|
||||
// Initializing nodes
|
||||
/* Initialize a binary tree */
|
||||
// Initialize nodes
|
||||
n1 := NewTreeNode(1)
|
||||
n2 := NewTreeNode(2)
|
||||
n3 := NewTreeNode(3)
|
||||
n4 := NewTreeNode(4)
|
||||
n5 := NewTreeNode(5)
|
||||
// Linking references (pointers) between nodes
|
||||
// Connect nodes with references (pointers)
|
||||
n1.Left = n2
|
||||
n1.Right = n3
|
||||
n2.Left = n4
|
||||
@@ -319,13 +319,13 @@ Similar to a linked list, the initialization of a binary tree involves first cre
|
||||
=== "Swift"
|
||||
|
||||
```swift title="binary_tree.swift"
|
||||
// Initializing nodes
|
||||
// Initialize nodes
|
||||
let n1 = TreeNode(x: 1)
|
||||
let n2 = TreeNode(x: 2)
|
||||
let n3 = TreeNode(x: 3)
|
||||
let n4 = TreeNode(x: 4)
|
||||
let n5 = TreeNode(x: 5)
|
||||
// Linking references (pointers) between nodes
|
||||
// Connect nodes with references (pointers)
|
||||
n1.left = n2
|
||||
n1.right = n3
|
||||
n2.left = n4
|
||||
@@ -335,14 +335,14 @@ Similar to a linked list, the initialization of a binary tree involves first cre
|
||||
=== "JS"
|
||||
|
||||
```javascript title="binary_tree.js"
|
||||
/* Initializing a binary tree */
|
||||
// Initializing nodes
|
||||
/* Initialize a binary tree */
|
||||
// Initialize nodes
|
||||
let n1 = new TreeNode(1),
|
||||
n2 = new TreeNode(2),
|
||||
n3 = new TreeNode(3),
|
||||
n4 = new TreeNode(4),
|
||||
n5 = new TreeNode(5);
|
||||
// Linking references (pointers) between nodes
|
||||
// Connect nodes with references (pointers)
|
||||
n1.left = n2;
|
||||
n1.right = n3;
|
||||
n2.left = n4;
|
||||
@@ -352,14 +352,14 @@ Similar to a linked list, the initialization of a binary tree involves first cre
|
||||
=== "TS"
|
||||
|
||||
```typescript title="binary_tree.ts"
|
||||
/* Initializing a binary tree */
|
||||
// Initializing nodes
|
||||
/* Initialize a binary tree */
|
||||
// Initialize nodes
|
||||
let n1 = new TreeNode(1),
|
||||
n2 = new TreeNode(2),
|
||||
n3 = new TreeNode(3),
|
||||
n4 = new TreeNode(4),
|
||||
n5 = new TreeNode(5);
|
||||
// Linking references (pointers) between nodes
|
||||
// Connect nodes with references (pointers)
|
||||
n1.left = n2;
|
||||
n1.right = n3;
|
||||
n2.left = n4;
|
||||
@@ -369,14 +369,14 @@ Similar to a linked list, the initialization of a binary tree involves first cre
|
||||
=== "Dart"
|
||||
|
||||
```dart title="binary_tree.dart"
|
||||
/* Initializing a binary tree */
|
||||
// Initializing nodes
|
||||
/* Initialize a binary tree */
|
||||
// Initialize nodes
|
||||
TreeNode n1 = new TreeNode(1);
|
||||
TreeNode n2 = new TreeNode(2);
|
||||
TreeNode n3 = new TreeNode(3);
|
||||
TreeNode n4 = new TreeNode(4);
|
||||
TreeNode n5 = new TreeNode(5);
|
||||
// Linking references (pointers) between nodes
|
||||
// Connect nodes with references (pointers)
|
||||
n1.left = n2;
|
||||
n1.right = n3;
|
||||
n2.left = n4;
|
||||
@@ -386,13 +386,13 @@ Similar to a linked list, the initialization of a binary tree involves first cre
|
||||
=== "Rust"
|
||||
|
||||
```rust title="binary_tree.rs"
|
||||
// Initializing nodes
|
||||
// Initialize nodes
|
||||
let n1 = TreeNode::new(1);
|
||||
let n2 = TreeNode::new(2);
|
||||
let n3 = TreeNode::new(3);
|
||||
let n4 = TreeNode::new(4);
|
||||
let n5 = TreeNode::new(5);
|
||||
// Linking references (pointers) between nodes
|
||||
// Connect nodes with references (pointers)
|
||||
n1.borrow_mut().left = Some(n2.clone());
|
||||
n1.borrow_mut().right = Some(n3);
|
||||
n2.borrow_mut().left = Some(n4);
|
||||
@@ -402,14 +402,14 @@ Similar to a linked list, the initialization of a binary tree involves first cre
|
||||
=== "C"
|
||||
|
||||
```c title="binary_tree.c"
|
||||
/* Initializing a binary tree */
|
||||
// Initializing nodes
|
||||
/* Initialize a binary tree */
|
||||
// Initialize nodes
|
||||
TreeNode *n1 = newTreeNode(1);
|
||||
TreeNode *n2 = newTreeNode(2);
|
||||
TreeNode *n3 = newTreeNode(3);
|
||||
TreeNode *n4 = newTreeNode(4);
|
||||
TreeNode *n5 = newTreeNode(5);
|
||||
// Linking references (pointers) between nodes
|
||||
// Connect nodes with references (pointers)
|
||||
n1->left = n2;
|
||||
n1->right = n3;
|
||||
n2->left = n4;
|
||||
@@ -419,13 +419,13 @@ Similar to a linked list, the initialization of a binary tree involves first cre
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="binary_tree.kt"
|
||||
// Initializing nodes
|
||||
// Initialize nodes
|
||||
val n1 = TreeNode(1)
|
||||
val n2 = TreeNode(2)
|
||||
val n3 = TreeNode(3)
|
||||
val n4 = TreeNode(4)
|
||||
val n5 = TreeNode(5)
|
||||
// Linking references (pointers) between nodes
|
||||
// Connect nodes with references (pointers)
|
||||
n1.left = n2
|
||||
n1.right = n3
|
||||
n2.left = n4
|
||||
@@ -435,86 +435,88 @@ Similar to a linked list, the initialization of a binary tree involves first cre
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="binary_tree.rb"
|
||||
# Initializing a binary tree
|
||||
# Initializing nodes
|
||||
# Initialize a binary tree
|
||||
# Initialize nodes
|
||||
n1 = TreeNode.new(1)
|
||||
n2 = TreeNode.new(2)
|
||||
n3 = TreeNode.new(3)
|
||||
n4 = TreeNode.new(4)
|
||||
n5 = TreeNode.new(5)
|
||||
# Linking references (pointers) between nodes
|
||||
# Connect nodes with references (pointers)
|
||||
n1.left = n2
|
||||
n1.right = n3
|
||||
n2.left = n4
|
||||
n2.right = n5
|
||||
```
|
||||
|
||||
??? pythontutor "Code Visualization"
|
||||
??? pythontutor "Visualize execution"
|
||||
|
||||
https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%8A%82%E7%82%B9%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E9%92%88%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
|
||||
https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22Binary%20tree%20node%20class%22%22%22%0A%20%20%20%20def%20__init__%28self%2C%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Node%20value%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20Left%20child%20reference%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20Right%20child%20reference%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20Initialize%20a%20binary%20tree%0A%20%20%20%20%23%20Initialize%20nodes%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20Connect%20nodes%20with%20references%20%28pointers%29%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
|
||||
|
||||
### Inserting and Removing Nodes
|
||||
### Inserting and deleting nodes
|
||||
|
||||
Similar to a linked list, inserting and removing nodes in a binary tree can be achieved by modifying pointers. The figure below provides an example.
|
||||
As with linked lists, insertion and deletion can be implemented by changing pointers. An example is shown below.
|
||||
|
||||

|
||||

|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree.py"
|
||||
# Inserting and removing nodes
|
||||
# Insert and delete a node
|
||||
p = TreeNode(0)
|
||||
# Inserting node P between n1 -> n2
|
||||
# Insert node P between n1 and n2
|
||||
n1.left = p
|
||||
p.left = n2
|
||||
# Removing node P
|
||||
# Delete node P
|
||||
n1.left = n2
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="binary_tree.cpp"
|
||||
/* Inserting and removing nodes */
|
||||
/* Insert and delete a node */
|
||||
TreeNode* P = new TreeNode(0);
|
||||
// Inserting node P between n1 and n2
|
||||
// Insert node P between n1 and n2
|
||||
n1->left = P;
|
||||
P->left = n2;
|
||||
// Removing node P
|
||||
// Delete node P
|
||||
n1->left = n2;
|
||||
// Free the memory
|
||||
delete P;
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="binary_tree.java"
|
||||
TreeNode P = new TreeNode(0);
|
||||
// Inserting node P between n1 and n2
|
||||
// Insert node P between n1 and n2
|
||||
n1.left = P;
|
||||
P.left = n2;
|
||||
// Removing node P
|
||||
// Delete node P
|
||||
n1.left = n2;
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="binary_tree.cs"
|
||||
/* Inserting and removing nodes */
|
||||
/* Insert and delete a node */
|
||||
TreeNode P = new(0);
|
||||
// Inserting node P between n1 and n2
|
||||
// Insert node P between n1 and n2
|
||||
n1.left = P;
|
||||
P.left = n2;
|
||||
// Removing node P
|
||||
// Delete node P
|
||||
n1.left = n2;
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="binary_tree.go"
|
||||
/* Inserting and removing nodes */
|
||||
// Inserting node P between n1 and n2
|
||||
/* Insert and delete a node */
|
||||
// Insert node P between n1 and n2
|
||||
p := NewTreeNode(0)
|
||||
n1.Left = p
|
||||
p.Left = n2
|
||||
// Removing node P
|
||||
// Delete node P
|
||||
n1.Left = n2
|
||||
```
|
||||
|
||||
@@ -522,46 +524,46 @@ Similar to a linked list, inserting and removing nodes in a binary tree can be a
|
||||
|
||||
```swift title="binary_tree.swift"
|
||||
let P = TreeNode(x: 0)
|
||||
// Inserting node P between n1 and n2
|
||||
// Insert node P between n1 and n2
|
||||
n1.left = P
|
||||
P.left = n2
|
||||
// Removing node P
|
||||
// Delete node P
|
||||
n1.left = n2
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="binary_tree.js"
|
||||
/* Inserting and removing nodes */
|
||||
/* Insert and delete a node */
|
||||
let P = new TreeNode(0);
|
||||
// Inserting node P between n1 and n2
|
||||
// Insert node P between n1 and n2
|
||||
n1.left = P;
|
||||
P.left = n2;
|
||||
// Removing node P
|
||||
// Delete node P
|
||||
n1.left = n2;
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
|
||||
```typescript title="binary_tree.ts"
|
||||
/* Inserting and removing nodes */
|
||||
/* Insert and delete a node */
|
||||
const P = new TreeNode(0);
|
||||
// Inserting node P between n1 and n2
|
||||
// Insert node P between n1 and n2
|
||||
n1.left = P;
|
||||
P.left = n2;
|
||||
// Removing node P
|
||||
// Delete node P
|
||||
n1.left = n2;
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="binary_tree.dart"
|
||||
/* Inserting and removing nodes */
|
||||
/* Insert and delete a node */
|
||||
TreeNode P = new TreeNode(0);
|
||||
// Inserting node P between n1 and n2
|
||||
// Insert node P between n1 and n2
|
||||
n1.left = P;
|
||||
P.left = n2;
|
||||
// Removing node P
|
||||
// Delete node P
|
||||
n1.left = n2;
|
||||
```
|
||||
|
||||
@@ -569,102 +571,104 @@ Similar to a linked list, inserting and removing nodes in a binary tree can be a
|
||||
|
||||
```rust title="binary_tree.rs"
|
||||
let p = TreeNode::new(0);
|
||||
// Inserting node P between n1 and n2
|
||||
// Insert node P between n1 and n2
|
||||
n1.borrow_mut().left = Some(p.clone());
|
||||
p.borrow_mut().left = Some(n2.clone());
|
||||
// Removing node P
|
||||
// Delete node p
|
||||
n1.borrow_mut().left = Some(n2);
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="binary_tree.c"
|
||||
/* Inserting and removing nodes */
|
||||
/* Insert and delete a node */
|
||||
TreeNode *P = newTreeNode(0);
|
||||
// Inserting node P between n1 and n2
|
||||
// Insert node P between n1 and n2
|
||||
n1->left = P;
|
||||
P->left = n2;
|
||||
// Removing node P
|
||||
// Delete node P
|
||||
n1->left = n2;
|
||||
// Free the memory
|
||||
free(P);
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="binary_tree.kt"
|
||||
val P = TreeNode(0)
|
||||
// Inserting node P between n1 and n2
|
||||
// Insert node P between n1 and n2
|
||||
n1.left = P
|
||||
P.left = n2
|
||||
// Removing node P
|
||||
// Delete node P
|
||||
n1.left = n2
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="binary_tree.rb"
|
||||
# Inserting and removing nodes
|
||||
# Insert and delete a node
|
||||
_p = TreeNode.new(0)
|
||||
# Inserting node _p between n1 and n2
|
||||
# Insert node _p between n1 and n2
|
||||
n1.left = _p
|
||||
_p.left = n2
|
||||
# Removing node _p
|
||||
# Delete the node
|
||||
n1.left = n2
|
||||
```
|
||||
|
||||
??? pythontutor "Code Visualization"
|
||||
??? pythontutor "Visualize execution"
|
||||
|
||||
https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%8A%82%E7%82%B9%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E9%92%88%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E4%B8%8E%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%0A%20%20%20%20p%20%3D%20TreeNode%280%29%0A%20%20%20%20%23%20%E5%9C%A8%20n1%20-%3E%20n2%20%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20p%0A%20%20%20%20p.left%20%3D%20n2%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20n2&cumulative=false&curInstr=37&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
|
||||
https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22Binary%20tree%20node%20class%22%22%22%0A%20%20%20%20def%20__init__%28self%2C%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Node%20value%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20Left%20child%20reference%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20Right%20child%20reference%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20Initialize%20a%20binary%20tree%0A%20%20%20%20%23%20Initialize%20nodes%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20Connect%20nodes%20with%20references%20%28pointers%29%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5%0A%0A%20%20%20%20%23%20Insert%20and%20delete%20a%20node%0A%20%20%20%20p%20%3D%20TreeNode%280%29%0A%20%20%20%20%23%20Insert%20node%20P%20between%20n1%20and%20n2%0A%20%20%20%20n1.left%20%3D%20p%0A%20%20%20%20p.left%20%3D%20n2%0A%20%20%20%20%23%20Delete%20node%20P%0A%20%20%20%20n1.left%20%3D%20n2&cumulative=false&curInstr=37&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
|
||||
|
||||
!!! tip
|
||||
|
||||
Keep in mind that inserting a node can alter the original logical structure of a binary tree, while deleting a node usually entails removing that node together with its entire subtree. In practice, insertion and deletion in binary trees are therefore typically implemented as coordinated sequences of operations to achieve a meaningful result.
|
||||
Inserting a node may change the tree's logical structure, while deleting a node generally means deleting it and all its subtrees. Meaningful insertion and deletion operations therefore usually require several coordinated steps.
|
||||
|
||||
## Common Types of Binary Trees
|
||||
## Common types of binary trees
|
||||
|
||||
### Perfect Binary Tree
|
||||
### Perfect binary trees
|
||||
|
||||
As shown in the figure below, a <u>perfect binary tree</u> has every level completely filled. In a perfect binary tree, leaf nodes have a degree of $0$, while all other nodes have a degree of $2$. If the tree height is $h$, the total number of nodes is $2^{h+1} - 1$, following a standard exponential pattern that mirrors the common phenomenon of cell division in nature.
|
||||
In a <u>perfect binary tree</u>, every level is completely filled, as shown below. Leaves have degree $0$, and all other nodes have degree $2$. A tree of height $h$ has $2^{h+1} - 1$ nodes, an exponential relationship reminiscent of cell division in nature.
|
||||
|
||||
!!! tip
|
||||
|
||||
Please note that in the Chinese community, a perfect binary tree is often referred to as a <u>full binary tree</u>.
|
||||
In Chinese-language resources, a perfect binary tree is often called <u>满二叉树</u>.
|
||||
|
||||

|
||||

|
||||
|
||||
### Complete Binary Tree
|
||||
### Complete binary trees
|
||||
|
||||
As shown in the figure below, a <u>complete binary tree</u> only allows the bottom level to be incompletely filled, and the nodes at the bottom level must be filled continuously from left to right. Note that a perfect binary tree is also a complete binary tree.
|
||||
Only the lowest level of a <u>complete binary tree</u> may be incomplete, and its nodes must fill that level consecutively from left to right, as shown below. Every perfect binary tree is also complete.
|
||||
|
||||

|
||||

|
||||
|
||||
### Full Binary Tree
|
||||
### Full binary trees
|
||||
|
||||
As shown in the figure below, in a <u>full binary tree</u>, all nodes except leaf nodes have two child nodes.
|
||||
In a <u>full binary tree</u>, every node other than a leaf has exactly two children, as shown below.
|
||||
|
||||

|
||||

|
||||
|
||||
### Balanced Binary Tree
|
||||
### Balanced binary trees
|
||||
|
||||
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.
|
||||
In a <u>balanced binary tree</u>, the heights of the left and right subtrees of every node differ by at most 1, as shown below.
|
||||
|
||||

|
||||

|
||||
|
||||
## Degeneration of Binary Trees
|
||||
## Degeneration of a binary tree
|
||||
|
||||
The figure below contrasts the ideal and degenerate structures of binary trees. When every level is filled, the tree becomes a "perfect binary tree"; when all nodes skew to one side, the binary tree degenerates into a "linked list".
|
||||
The figure below shows ideal and degenerate tree structures. When every level is full, the tree is perfect. When every node lies along one side, the tree degenerates into a linked list.
|
||||
|
||||
- A perfect binary tree is the ideal case, fully leveraging the divide-and-conquer advantages of binary trees.
|
||||
- A linked list represents the other extreme, where all operations become linear operations with time complexity degrading to $O(n)$.
|
||||
- A perfect binary tree is the ideal case, making full use of divide and conquer.
|
||||
- A linked list is the opposite extreme: operations become linear, with time complexity degrading to $O(n)$.
|
||||
|
||||

|
||||

|
||||
|
||||
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.
|
||||
As the table below shows, quantities such as leaf count, total node count, and height reach their maximum or minimum values in these best and worst structures.
|
||||
|
||||
<p align="center"> Table <id> The Best and Worst Structures of Binary Trees </p>
|
||||
<p align="center"> Table <id> Best and worst binary-tree structures </p>
|
||||
|
||||
| | Perfect binary tree | Linked list |
|
||||
| ----------------------------------------------- | ------------------- | ----------- |
|
||||
| Number of nodes at level $i$ | $2^{i-1}$ | $1$ |
|
||||
| Number of leaf nodes in a tree with height $h$ | $2^h$ | $1$ |
|
||||
| Total number of nodes in a tree with height $h$ | $2^{h+1} - 1$ | $h + 1$ |
|
||||
| Height of a tree with $n$ total nodes | $\log_2 (n+1) - 1$ | $n - 1$ |
|
||||
| | Perfect binary tree | Linked list |
|
||||
| --------------------------- | ------------------ | ------- |
|
||||
| Nodes at level $i$ | $2^{i-1}$ | $1$ |
|
||||
| Leaves in a tree of height $h$ | $2^h$ | $1$ |
|
||||
| Total nodes in a tree of height $h$ | $2^{h+1} - 1$ | $h + 1$ |
|
||||
| Height of a tree with $n$ nodes | $\log_2 (n+1) - 1$ | $n - 1$ |
|
||||
|
||||
Reference in New Issue
Block a user