> {
+ Rc::new(RefCell::new(Self {
+ val,
+ height: 0,
+ left: None,
+ right: None
+ }))
+ }
+ }
+ ```
+
+=== "C"
+
+ ```c title=""
+ /* AVL tree node */
+ TreeNode struct TreeNode {
+ int val;
+ int height;
+ struct TreeNode *left;
+ struct TreeNode *right;
+ } TreeNode;
+
+ /* Constructor */
+ 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=""
+ /* AVL tree node */
+ class TreeNode(val _val: Int) { // Node value
+ val height: Int = 0 // Node height
+ val left: TreeNode? = null // Left child
+ val right: TreeNode? = null // Right child
+ }
+ ```
+
+=== "Ruby"
+
+ ```ruby title=""
+
+ ```
+
+=== "Zig"
+
+ ```zig title=""
+
+ ```
+
+The "node height" refers to the distance from that node to its farthest leaf node, i.e., the number of "edges" passed. It is important to note that the height of a leaf node is $0$, and the height of a null node is $-1$. We will create two utility functions for getting and updating the height of a node:
+
+=== "Python"
+
+ ```python title="avl_tree.py"
+ def height(self, node: TreeNode | None) -> int:
+ """获取节点高度"""
+ # 空节点高度为 -1 ,叶节点高度为 0
+ if node is not None:
+ return node.height
+ return -1
+
+ def update_height(self, node: TreeNode | None):
+ """更新节点高度"""
+ # 节点高度等于最高子树高度 + 1
+ node.height = max([self.height(node.left), self.height(node.right)]) + 1
+ ```
+
+=== "C++"
+
+ ```cpp title="avl_tree.cpp"
+ /* 获取节点高度 */
+ int height(TreeNode *node) {
+ // 空节点高度为 -1 ,叶节点高度为 0
+ return node == nullptr ? -1 : node->height;
+ }
+
+ /* 更新节点高度 */
+ void updateHeight(TreeNode *node) {
+ // 节点高度等于最高子树高度 + 1
+ node->height = max(height(node->left), height(node->right)) + 1;
+ }
+ ```
+
+=== "Java"
+
+ ```java title="avl_tree.java"
+ /* 获取节点高度 */
+ int height(TreeNode node) {
+ // 空节点高度为 -1 ,叶节点高度为 0
+ return node == null ? -1 : node.height;
+ }
+
+ /* 更新节点高度 */
+ void updateHeight(TreeNode node) {
+ // 节点高度等于最高子树高度 + 1
+ node.height = Math.max(height(node.left), height(node.right)) + 1;
+ }
+ ```
+
+=== "C#"
+
+ ```csharp title="avl_tree.cs"
+ /* 获取节点高度 */
+ int Height(TreeNode? node) {
+ // 空节点高度为 -1 ,叶节点高度为 0
+ return node == null ? -1 : node.height;
+ }
+
+ /* 更新节点高度 */
+ void UpdateHeight(TreeNode node) {
+ // 节点高度等于最高子树高度 + 1
+ node.height = Math.Max(Height(node.left), Height(node.right)) + 1;
+ }
+ ```
+
+=== "Go"
+
+ ```go title="avl_tree.go"
+ /* 获取节点高度 */
+ func (t *aVLTree) height(node *TreeNode) int {
+ // 空节点高度为 -1 ,叶节点高度为 0
+ if node != nil {
+ return node.Height
+ }
+ return -1
+ }
+
+ /* 更新节点高度 */
+ func (t *aVLTree) updateHeight(node *TreeNode) {
+ lh := t.height(node.Left)
+ rh := t.height(node.Right)
+ // 节点高度等于最高子树高度 + 1
+ if lh > rh {
+ node.Height = lh + 1
+ } else {
+ node.Height = rh + 1
+ }
+ }
+ ```
+
+=== "Swift"
+
+ ```swift title="avl_tree.swift"
+ /* 获取节点高度 */
+ func height(node: TreeNode?) -> Int {
+ // 空节点高度为 -1 ,叶节点高度为 0
+ node?.height ?? -1
+ }
+
+ /* 更新节点高度 */
+ func updateHeight(node: TreeNode?) {
+ // 节点高度等于最高子树高度 + 1
+ node?.height = max(height(node: node?.left), height(node: node?.right)) + 1
+ }
+ ```
+
+=== "JS"
+
+ ```javascript title="avl_tree.js"
+ /* 获取节点高度 */
+ height(node) {
+ // 空节点高度为 -1 ,叶节点高度为 0
+ return node === null ? -1 : node.height;
+ }
+
+ /* 更新节点高度 */
+ #updateHeight(node) {
+ // 节点高度等于最高子树高度 + 1
+ node.height =
+ Math.max(this.height(node.left), this.height(node.right)) + 1;
+ }
+ ```
+
+=== "TS"
+
+ ```typescript title="avl_tree.ts"
+ /* 获取节点高度 */
+ height(node: TreeNode): number {
+ // 空节点高度为 -1 ,叶节点高度为 0
+ return node === null ? -1 : node.height;
+ }
+
+ /* 更新节点高度 */
+ updateHeight(node: TreeNode): void {
+ // 节点高度等于最高子树高度 + 1
+ node.height =
+ Math.max(this.height(node.left), this.height(node.right)) + 1;
+ }
+ ```
+
+=== "Dart"
+
+ ```dart title="avl_tree.dart"
+ /* 获取节点高度 */
+ int height(TreeNode? node) {
+ // 空节点高度为 -1 ,叶节点高度为 0
+ return node == null ? -1 : node.height;
+ }
+
+ /* 更新节点高度 */
+ void updateHeight(TreeNode? node) {
+ // 节点高度等于最高子树高度 + 1
+ node!.height = max(height(node.left), height(node.right)) + 1;
+ }
+ ```
+
+=== "Rust"
+
+ ```rust title="avl_tree.rs"
+ /* 获取节点高度 */
+ fn height(node: OptionTreeNodeRc) -> i32 {
+ // 空节点高度为 -1 ,叶节点高度为 0
+ match node {
+ Some(node) => node.borrow().height,
+ None => -1,
+ }
+ }
+
+ /* 更新节点高度 */
+ fn update_height(node: OptionTreeNodeRc) {
+ if let Some(node) = node {
+ let left = node.borrow().left.clone();
+ let right = node.borrow().right.clone();
+ // 节点高度等于最高子树高度 + 1
+ node.borrow_mut().height = std::cmp::max(Self::height(left), Self::height(right)) + 1;
+ }
+ }
+ ```
+
+=== "C"
+
+ ```c title="avl_tree.c"
+ /* 获取节点高度 */
+ int height(TreeNode *node) {
+ // 空节点高度为 -1 ,叶节点高度为 0
+ if (node != NULL) {
+ return node->height;
+ }
+ return -1;
+ }
+
+ /* 更新节点高度 */
+ void updateHeight(TreeNode *node) {
+ int lh = height(node->left);
+ int rh = height(node->right);
+ // 节点高度等于最高子树高度 + 1
+ if (lh > rh) {
+ node->height = lh + 1;
+ } else {
+ node->height = rh + 1;
+ }
+ }
+ ```
+
+=== "Kotlin"
+
+ ```kotlin title="avl_tree.kt"
+ /* 获取节点高度 */
+ fun height(node: TreeNode?): Int {
+ // 空节点高度为 -1 ,叶节点高度为 0
+ return node?.height ?: -1
+ }
+
+ /* 更新节点高度 */
+ fun updateHeight(node: TreeNode?) {
+ // 节点高度等于最高子树高度 + 1
+ node?.height = (max(height(node?.left).toDouble(), height(node?.right).toDouble()) + 1).toInt()
+ }
+ ```
+
+=== "Ruby"
+
+ ```ruby title="avl_tree.rb"
+ [class]{AVLTree}-[func]{height}
+
+ [class]{AVLTree}-[func]{update_height}
+ ```
+
+=== "Zig"
+
+ ```zig title="avl_tree.zig"
+ // 获取节点高度
+ fn height(self: *Self, node: ?*inc.TreeNode(T)) i32 {
+ _ = self;
+ // 空节点高度为 -1 ,叶节点高度为 0
+ return if (node == null) -1 else node.?.height;
+ }
+
+ // 更新节点高度
+ fn updateHeight(self: *Self, node: ?*inc.TreeNode(T)) void {
+ // 节点高度等于最高子树高度 + 1
+ node.?.height = @max(self.height(node.?.left), self.height(node.?.right)) + 1;
+ }
+ ```
+
+### 2. Node balance factor
+
+The "balance factor" of a node is defined as the height of the node's left subtree minus the height of its right subtree, with the balance factor of a null node defined as $0$. We will also encapsulate the functionality of obtaining the node balance factor into a function for easy use later on:
+
+=== "Python"
+
+ ```python title="avl_tree.py"
+ def balance_factor(self, node: TreeNode | None) -> int:
+ """获取平衡因子"""
+ # 空节点平衡因子为 0
+ if node is None:
+ return 0
+ # 节点平衡因子 = 左子树高度 - 右子树高度
+ return self.height(node.left) - self.height(node.right)
+ ```
+
+=== "C++"
+
+ ```cpp title="avl_tree.cpp"
+ /* 获取平衡因子 */
+ int balanceFactor(TreeNode *node) {
+ // 空节点平衡因子为 0
+ if (node == nullptr)
+ return 0;
+ // 节点平衡因子 = 左子树高度 - 右子树高度
+ return height(node->left) - height(node->right);
+ }
+ ```
+
+=== "Java"
+
+ ```java title="avl_tree.java"
+ /* 获取平衡因子 */
+ int balanceFactor(TreeNode node) {
+ // 空节点平衡因子为 0
+ if (node == null)
+ return 0;
+ // 节点平衡因子 = 左子树高度 - 右子树高度
+ return height(node.left) - height(node.right);
+ }
+ ```
+
+=== "C#"
+
+ ```csharp title="avl_tree.cs"
+ /* 获取平衡因子 */
+ int BalanceFactor(TreeNode? node) {
+ // 空节点平衡因子为 0
+ if (node == null) return 0;
+ // 节点平衡因子 = 左子树高度 - 右子树高度
+ return Height(node.left) - Height(node.right);
+ }
+ ```
+
+=== "Go"
+
+ ```go title="avl_tree.go"
+ /* 获取平衡因子 */
+ func (t *aVLTree) balanceFactor(node *TreeNode) int {
+ // 空节点平衡因子为 0
+ if node == nil {
+ return 0
+ }
+ // 节点平衡因子 = 左子树高度 - 右子树高度
+ return t.height(node.Left) - t.height(node.Right)
+ }
+ ```
+
+=== "Swift"
+
+ ```swift title="avl_tree.swift"
+ /* 获取平衡因子 */
+ func balanceFactor(node: TreeNode?) -> Int {
+ // 空节点平衡因子为 0
+ guard let node = node else { return 0 }
+ // 节点平衡因子 = 左子树高度 - 右子树高度
+ return height(node: node.left) - height(node: node.right)
+ }
+ ```
+
+=== "JS"
+
+ ```javascript title="avl_tree.js"
+ /* 获取平衡因子 */
+ balanceFactor(node) {
+ // 空节点平衡因子为 0
+ if (node === null) return 0;
+ // 节点平衡因子 = 左子树高度 - 右子树高度
+ return this.height(node.left) - this.height(node.right);
+ }
+ ```
+
+=== "TS"
+
+ ```typescript title="avl_tree.ts"
+ /* 获取平衡因子 */
+ balanceFactor(node: TreeNode): number {
+ // 空节点平衡因子为 0
+ if (node === null) return 0;
+ // 节点平衡因子 = 左子树高度 - 右子树高度
+ return this.height(node.left) - this.height(node.right);
+ }
+ ```
+
+=== "Dart"
+
+ ```dart title="avl_tree.dart"
+ /* 获取平衡因子 */
+ int balanceFactor(TreeNode? node) {
+ // 空节点平衡因子为 0
+ if (node == null) return 0;
+ // 节点平衡因子 = 左子树高度 - 右子树高度
+ return height(node.left) - height(node.right);
+ }
+ ```
+
+=== "Rust"
+
+ ```rust title="avl_tree.rs"
+ /* 获取平衡因子 */
+ fn balance_factor(node: OptionTreeNodeRc) -> i32 {
+ match node {
+ // 空节点平衡因子为 0
+ None => 0,
+ // 节点平衡因子 = 左子树高度 - 右子树高度
+ Some(node) => {
+ Self::height(node.borrow().left.clone()) - Self::height(node.borrow().right.clone())
+ }
+ }
+ }
+ ```
+
+=== "C"
+
+ ```c title="avl_tree.c"
+ /* 获取平衡因子 */
+ int balanceFactor(TreeNode *node) {
+ // 空节点平衡因子为 0
+ if (node == NULL) {
+ return 0;
+ }
+ // 节点平衡因子 = 左子树高度 - 右子树高度
+ return height(node->left) - height(node->right);
+ }
+ ```
+
+=== "Kotlin"
+
+ ```kotlin title="avl_tree.kt"
+ /* 获取平衡因子 */
+ fun balanceFactor(node: TreeNode?): Int {
+ // 空节点平衡因子为 0
+ if (node == null) return 0
+ // 节点平衡因子 = 左子树高度 - 右子树高度
+ return height(node.left) - height(node.right)
+ }
+ ```
+
+=== "Ruby"
+
+ ```ruby title="avl_tree.rb"
+ [class]{AVLTree}-[func]{balance_factor}
+ ```
+
+=== "Zig"
+
+ ```zig title="avl_tree.zig"
+ // 获取平衡因子
+ fn balanceFactor(self: *Self, node: ?*inc.TreeNode(T)) i32 {
+ // 空节点平衡因子为 0
+ if (node == null) return 0;
+ // 节点平衡因子 = 左子树高度 - 右子树高度
+ return self.height(node.?.left) - self.height(node.?.right);
+ }
+ ```
+
+!!! note
+
+ Let the balance factor be $f$, then the balance factor of any node in an AVL tree satisfies $-1 \le f \le 1$.
+
+## 7.5.2 Rotations in AVL trees
+
+The characteristic feature of an AVL tree is the "rotation" operation, which can restore balance to an unbalanced node without affecting the in-order traversal sequence of the binary tree. In other words, **the rotation operation can maintain the property of a "binary search tree" while also turning the tree back into a "balanced binary tree"**.
+
+We call nodes with an absolute balance factor $> 1$ "unbalanced nodes". Depending on the type of imbalance, there are four kinds of rotations: right rotation, left rotation, right-left rotation, and left-right rotation. Below, we detail these rotation operations.
+
+### 1. Right rotation
+
+As shown in the Figure 7-26 , the first unbalanced node from the bottom up in the binary tree is "node 3". Focusing on the subtree with this unbalanced node as the root, denoted as `node`, and its left child as `child`, perform a "right rotation". After the right rotation, the subtree is balanced again while still maintaining the properties of a binary search tree.
+
+=== "<1>"
+ ![Steps of right rotation](avl_tree.assets/avltree_right_rotate_step1.png){ class="animation-figure" }
+
+=== "<2>"
+ ![avltree_right_rotate_step2](avl_tree.assets/avltree_right_rotate_step2.png){ class="animation-figure" }
+
+=== "<3>"
+ ![avltree_right_rotate_step3](avl_tree.assets/avltree_right_rotate_step3.png){ class="animation-figure" }
+
+=== "<4>"
+ ![avltree_right_rotate_step4](avl_tree.assets/avltree_right_rotate_step4.png){ class="animation-figure" }
+
+ Figure 7-26 Steps of right rotation
+
+As shown in the Figure 7-27 , when the `child` node has a right child (denoted as `grand_child`), a step needs to be added in the right rotation: set `grand_child` as the left child of `node`.
+
+![Right rotation with grand_child](avl_tree.assets/avltree_right_rotate_with_grandchild.png){ class="animation-figure" }
+
+ Figure 7-27 Right rotation with grand_child
+
+"Right rotation" is a figurative term; in practice, it is achieved by modifying node pointers, as shown in the following code:
+
+=== "Python"
+
+ ```python title="avl_tree.py"
+ def right_rotate(self, node: TreeNode | None) -> TreeNode | None:
+ """右旋操作"""
+ child = node.left
+ grand_child = child.right
+ # 以 child 为原点,将 node 向右旋转
+ child.right = node
+ node.left = grand_child
+ # 更新节点高度
+ self.update_height(node)
+ self.update_height(child)
+ # 返回旋转后子树的根节点
+ return child
+ ```
+
+=== "C++"
+
+ ```cpp title="avl_tree.cpp"
+ /* 右旋操作 */
+ TreeNode *rightRotate(TreeNode *node) {
+ TreeNode *child = node->left;
+ TreeNode *grandChild = child->right;
+ // 以 child 为原点,将 node 向右旋转
+ child->right = node;
+ node->left = grandChild;
+ // 更新节点高度
+ updateHeight(node);
+ updateHeight(child);
+ // 返回旋转后子树的根节点
+ return child;
+ }
+ ```
+
+=== "Java"
+
+ ```java title="avl_tree.java"
+ /* 右旋操作 */
+ TreeNode rightRotate(TreeNode node) {
+ TreeNode child = node.left;
+ TreeNode grandChild = child.right;
+ // 以 child 为原点,将 node 向右旋转
+ child.right = node;
+ node.left = grandChild;
+ // 更新节点高度
+ updateHeight(node);
+ updateHeight(child);
+ // 返回旋转后子树的根节点
+ return child;
+ }
+ ```
+
+=== "C#"
+
+ ```csharp title="avl_tree.cs"
+ /* 右旋操作 */
+ TreeNode? RightRotate(TreeNode? node) {
+ TreeNode? child = node?.left;
+ TreeNode? grandChild = child?.right;
+ // 以 child 为原点,将 node 向右旋转
+ child.right = node;
+ node.left = grandChild;
+ // 更新节点高度
+ UpdateHeight(node);
+ UpdateHeight(child);
+ // 返回旋转后子树的根节点
+ return child;
+ }
+ ```
+
+=== "Go"
+
+ ```go title="avl_tree.go"
+ /* 右旋操作 */
+ func (t *aVLTree) rightRotate(node *TreeNode) *TreeNode {
+ child := node.Left
+ grandChild := child.Right
+ // 以 child 为原点,将 node 向右旋转
+ child.Right = node
+ node.Left = grandChild
+ // 更新节点高度
+ t.updateHeight(node)
+ t.updateHeight(child)
+ // 返回旋转后子树的根节点
+ return child
+ }
+ ```
+
+=== "Swift"
+
+ ```swift title="avl_tree.swift"
+ /* 右旋操作 */
+ func rightRotate(node: TreeNode?) -> TreeNode? {
+ let child = node?.left
+ let grandChild = child?.right
+ // 以 child 为原点,将 node 向右旋转
+ child?.right = node
+ node?.left = grandChild
+ // 更新节点高度
+ updateHeight(node: node)
+ updateHeight(node: child)
+ // 返回旋转后子树的根节点
+ return child
+ }
+ ```
+
+=== "JS"
+
+ ```javascript title="avl_tree.js"
+ /* 右旋操作 */
+ #rightRotate(node) {
+ const child = node.left;
+ const grandChild = child.right;
+ // 以 child 为原点,将 node 向右旋转
+ child.right = node;
+ node.left = grandChild;
+ // 更新节点高度
+ this.#updateHeight(node);
+ this.#updateHeight(child);
+ // 返回旋转后子树的根节点
+ return child;
+ }
+ ```
+
+=== "TS"
+
+ ```typescript title="avl_tree.ts"
+ /* 右旋操作 */
+ rightRotate(node: TreeNode): TreeNode {
+ const child = node.left;
+ const grandChild = child.right;
+ // 以 child 为原点,将 node 向右旋转
+ child.right = node;
+ node.left = grandChild;
+ // 更新节点高度
+ this.updateHeight(node);
+ this.updateHeight(child);
+ // 返回旋转后子树的根节点
+ return child;
+ }
+ ```
+
+=== "Dart"
+
+ ```dart title="avl_tree.dart"
+ /* 右旋操作 */
+ TreeNode? rightRotate(TreeNode? node) {
+ TreeNode? child = node!.left;
+ TreeNode? grandChild = child!.right;
+ // 以 child 为原点,将 node 向右旋转
+ child.right = node;
+ node.left = grandChild;
+ // 更新节点高度
+ updateHeight(node);
+ updateHeight(child);
+ // 返回旋转后子树的根节点
+ return child;
+ }
+ ```
+
+=== "Rust"
+
+ ```rust title="avl_tree.rs"
+ /* 右旋操作 */
+ fn right_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {
+ match node {
+ Some(node) => {
+ let child = node.borrow().left.clone().unwrap();
+ let grand_child = child.borrow().right.clone();
+ // 以 child 为原点,将 node 向右旋转
+ child.borrow_mut().right = Some(node.clone());
+ node.borrow_mut().left = grand_child;
+ // 更新节点高度
+ Self::update_height(Some(node));
+ Self::update_height(Some(child.clone()));
+ // 返回旋转后子树的根节点
+ Some(child)
+ }
+ None => None,
+ }
+ }
+ ```
+
+=== "C"
+
+ ```c title="avl_tree.c"
+ /* 右旋操作 */
+ TreeNode *rightRotate(TreeNode *node) {
+ TreeNode *child, *grandChild;
+ child = node->left;
+ grandChild = child->right;
+ // 以 child 为原点,将 node 向右旋转
+ child->right = node;
+ node->left = grandChild;
+ // 更新节点高度
+ updateHeight(node);
+ updateHeight(child);
+ // 返回旋转后子树的根节点
+ return child;
+ }
+ ```
+
+=== "Kotlin"
+
+ ```kotlin title="avl_tree.kt"
+ /* 右旋操作 */
+ fun rightRotate(node: TreeNode?): TreeNode {
+ val child = node!!.left
+ val grandChild = child!!.right
+ // 以 child 为原点,将 node 向右旋转
+ child.right = node
+ node.left = grandChild
+ // 更新节点高度
+ updateHeight(node)
+ updateHeight(child)
+ // 返回旋转后子树的根节点
+ return child
+ }
+ ```
+
+=== "Ruby"
+
+ ```ruby title="avl_tree.rb"
+ [class]{AVLTree}-[func]{right_rotate}
+ ```
+
+=== "Zig"
+
+ ```zig title="avl_tree.zig"
+ // 右旋操作
+ fn rightRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {
+ var child = node.?.left;
+ var grandChild = child.?.right;
+ // 以 child 为原点,将 node 向右旋转
+ child.?.right = node;
+ node.?.left = grandChild;
+ // 更新节点高度
+ self.updateHeight(node);
+ self.updateHeight(child);
+ // 返回旋转后子树的根节点
+ return child;
+ }
+ ```
+
+### 2. Left rotation
+
+Correspondingly, if considering the "mirror" of the above unbalanced binary tree, the "left rotation" operation shown in the Figure 7-28 needs to be performed.
+
+![Left rotation operation](avl_tree.assets/avltree_left_rotate.png){ class="animation-figure" }
+
+ Figure 7-28 Left rotation operation
+
+Similarly, as shown in the Figure 7-29 , when the `child` node has a left child (denoted as `grand_child`), a step needs to be added in the left rotation: set `grand_child` as the right child of `node`.
+
+![Left rotation with grand_child](avl_tree.assets/avltree_left_rotate_with_grandchild.png){ class="animation-figure" }
+
+ Figure 7-29 Left rotation with grand_child
+
+It can be observed that **the right and left rotation operations are logically symmetrical, and they solve two symmetrical types of imbalance**. Based on symmetry, by replacing all `left` with `right`, and all `right` with `left` in the implementation code of right rotation, we can get the implementation code for left rotation:
+
+=== "Python"
+
+ ```python title="avl_tree.py"
+ def left_rotate(self, node: TreeNode | None) -> TreeNode | None:
+ """左旋操作"""
+ child = node.right
+ grand_child = child.left
+ # 以 child 为原点,将 node 向左旋转
+ child.left = node
+ node.right = grand_child
+ # 更新节点高度
+ self.update_height(node)
+ self.update_height(child)
+ # 返回旋转后子树的根节点
+ return child
+ ```
+
+=== "C++"
+
+ ```cpp title="avl_tree.cpp"
+ /* 左旋操作 */
+ TreeNode *leftRotate(TreeNode *node) {
+ TreeNode *child = node->right;
+ TreeNode *grandChild = child->left;
+ // 以 child 为原点,将 node 向左旋转
+ child->left = node;
+ node->right = grandChild;
+ // 更新节点高度
+ updateHeight(node);
+ updateHeight(child);
+ // 返回旋转后子树的根节点
+ return child;
+ }
+ ```
+
+=== "Java"
+
+ ```java title="avl_tree.java"
+ /* 左旋操作 */
+ TreeNode leftRotate(TreeNode node) {
+ TreeNode child = node.right;
+ TreeNode grandChild = child.left;
+ // 以 child 为原点,将 node 向左旋转
+ child.left = node;
+ node.right = grandChild;
+ // 更新节点高度
+ updateHeight(node);
+ updateHeight(child);
+ // 返回旋转后子树的根节点
+ return child;
+ }
+ ```
+
+=== "C#"
+
+ ```csharp title="avl_tree.cs"
+ /* 左旋操作 */
+ TreeNode? LeftRotate(TreeNode? node) {
+ TreeNode? child = node?.right;
+ TreeNode? grandChild = child?.left;
+ // 以 child 为原点,将 node 向左旋转
+ child.left = node;
+ node.right = grandChild;
+ // 更新节点高度
+ UpdateHeight(node);
+ UpdateHeight(child);
+ // 返回旋转后子树的根节点
+ return child;
+ }
+ ```
+
+=== "Go"
+
+ ```go title="avl_tree.go"
+ /* 左旋操作 */
+ func (t *aVLTree) leftRotate(node *TreeNode) *TreeNode {
+ child := node.Right
+ grandChild := child.Left
+ // 以 child 为原点,将 node 向左旋转
+ child.Left = node
+ node.Right = grandChild
+ // 更新节点高度
+ t.updateHeight(node)
+ t.updateHeight(child)
+ // 返回旋转后子树的根节点
+ return child
+ }
+ ```
+
+=== "Swift"
+
+ ```swift title="avl_tree.swift"
+ /* 左旋操作 */
+ func leftRotate(node: TreeNode?) -> TreeNode? {
+ let child = node?.right
+ let grandChild = child?.left
+ // 以 child 为原点,将 node 向左旋转
+ child?.left = node
+ node?.right = grandChild
+ // 更新节点高度
+ updateHeight(node: node)
+ updateHeight(node: child)
+ // 返回旋转后子树的根节点
+ return child
+ }
+ ```
+
+=== "JS"
+
+ ```javascript title="avl_tree.js"
+ /* 左旋操作 */
+ #leftRotate(node) {
+ const child = node.right;
+ const grandChild = child.left;
+ // 以 child 为原点,将 node 向左旋转
+ child.left = node;
+ node.right = grandChild;
+ // 更新节点高度
+ this.#updateHeight(node);
+ this.#updateHeight(child);
+ // 返回旋转后子树的根节点
+ return child;
+ }
+ ```
+
+=== "TS"
+
+ ```typescript title="avl_tree.ts"
+ /* 左旋操作 */
+ leftRotate(node: TreeNode): TreeNode {
+ const child = node.right;
+ const grandChild = child.left;
+ // 以 child 为原点,将 node 向左旋转
+ child.left = node;
+ node.right = grandChild;
+ // 更新节点高度
+ this.updateHeight(node);
+ this.updateHeight(child);
+ // 返回旋转后子树的根节点
+ return child;
+ }
+ ```
+
+=== "Dart"
+
+ ```dart title="avl_tree.dart"
+ /* 左旋操作 */
+ TreeNode? leftRotate(TreeNode? node) {
+ TreeNode? child = node!.right;
+ TreeNode? grandChild = child!.left;
+ // 以 child 为原点,将 node 向左旋转
+ child.left = node;
+ node.right = grandChild;
+ // 更新节点高度
+ updateHeight(node);
+ updateHeight(child);
+ // 返回旋转后子树的根节点
+ return child;
+ }
+ ```
+
+=== "Rust"
+
+ ```rust title="avl_tree.rs"
+ /* 左旋操作 */
+ fn left_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {
+ match node {
+ Some(node) => {
+ let child = node.borrow().right.clone().unwrap();
+ let grand_child = child.borrow().left.clone();
+ // 以 child 为原点,将 node 向左旋转
+ child.borrow_mut().left = Some(node.clone());
+ node.borrow_mut().right = grand_child;
+ // 更新节点高度
+ Self::update_height(Some(node));
+ Self::update_height(Some(child.clone()));
+ // 返回旋转后子树的根节点
+ Some(child)
+ }
+ None => None,
+ }
+ }
+ ```
+
+=== "C"
+
+ ```c title="avl_tree.c"
+ /* 左旋操作 */
+ TreeNode *leftRotate(TreeNode *node) {
+ TreeNode *child, *grandChild;
+ child = node->right;
+ grandChild = child->left;
+ // 以 child 为原点,将 node 向左旋转
+ child->left = node;
+ node->right = grandChild;
+ // 更新节点高度
+ updateHeight(node);
+ updateHeight(child);
+ // 返回旋转后子树的根节点
+ return child;
+ }
+ ```
+
+=== "Kotlin"
+
+ ```kotlin title="avl_tree.kt"
+ /* 左旋操作 */
+ fun leftRotate(node: TreeNode?): TreeNode {
+ val child = node!!.right
+ val grandChild = child!!.left
+ // 以 child 为原点,将 node 向左旋转
+ child.left = node
+ node.right = grandChild
+ // 更新节点高度
+ updateHeight(node)
+ updateHeight(child)
+ // 返回旋转后子树的根节点
+ return child
+ }
+ ```
+
+=== "Ruby"
+
+ ```ruby title="avl_tree.rb"
+ [class]{AVLTree}-[func]{left_rotate}
+ ```
+
+=== "Zig"
+
+ ```zig title="avl_tree.zig"
+ // 左旋操作
+ fn leftRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {
+ var child = node.?.right;
+ var grandChild = child.?.left;
+ // 以 child 为原点,将 node 向左旋转
+ child.?.left = node;
+ node.?.right = grandChild;
+ // 更新节点高度
+ self.updateHeight(node);
+ self.updateHeight(child);
+ // 返回旋转后子树的根节点
+ return child;
+ }
+ ```
+
+### 3. Right-left rotation
+
+For the unbalanced node 3 shown in the Figure 7-30 , using either left or right rotation alone cannot restore balance to the subtree. In this case, a "left rotation" needs to be performed on `child` first, followed by a "right rotation" on `node`.
+
+![Right-left rotation](avl_tree.assets/avltree_left_right_rotate.png){ class="animation-figure" }
+
+ Figure 7-30 Right-left rotation
+
+### 4. Left-right rotation
+
+As shown in the Figure 7-31 , for the mirror case of the above unbalanced binary tree, a "right rotation" needs to be performed on `child` first, followed by a "left rotation" on `node`.
+
+![Left-right rotation](avl_tree.assets/avltree_right_left_rotate.png){ class="animation-figure" }
+
+ Figure 7-31 Left-right rotation
+
+### 5. Choice of rotation
+
+The four kinds of imbalances shown in the Figure 7-32 correspond to the cases described above, respectively requiring right rotation, left-right rotation, right-left rotation, and left rotation.
+
+![The four rotation cases of AVL tree](avl_tree.assets/avltree_rotation_cases.png){ class="animation-figure" }
+
+ Figure 7-32 The four rotation cases of AVL tree
+
+As shown in the Table 7-3 , we determine which of the above cases an unbalanced node belongs to by judging the sign of the balance factor of the unbalanced node and its higher-side child's balance factor.
+
+ Table 7-3 Conditions for Choosing Among the Four Rotation Cases
+
+
+
+| Balance factor of unbalanced node | Balance factor of child node | Rotation method to use |
+| --------------------------------- | ---------------------------- | --------------------------------- |
+| $> 1$ (Left-leaning tree) | $\geq 0$ | Right rotation |
+| $> 1$ (Left-leaning tree) | $<0$ | Left rotation then right rotation |
+| $< -1$ (Right-leaning tree) | $\leq 0$ | Left rotation |
+| $< -1$ (Right-leaning tree) | $>0$ | Right rotation then left rotation |
+
+
+
+For convenience, we encapsulate the rotation operations into a function. **With this function, we can perform rotations on various kinds of imbalances, restoring balance to unbalanced nodes**. The code is as follows:
+
+=== "Python"
+
+ ```python title="avl_tree.py"
+ def rotate(self, node: TreeNode | None) -> TreeNode | None:
+ """执行旋转操作,使该子树重新恢复平衡"""
+ # 获取节点 node 的平衡因子
+ balance_factor = self.balance_factor(node)
+ # 左偏树
+ if balance_factor > 1:
+ if self.balance_factor(node.left) >= 0:
+ # 右旋
+ return self.right_rotate(node)
+ else:
+ # 先左旋后右旋
+ node.left = self.left_rotate(node.left)
+ return self.right_rotate(node)
+ # 右偏树
+ elif balance_factor < -1:
+ if self.balance_factor(node.right) <= 0:
+ # 左旋
+ return self.left_rotate(node)
+ else:
+ # 先右旋后左旋
+ node.right = self.right_rotate(node.right)
+ return self.left_rotate(node)
+ # 平衡树,无须旋转,直接返回
+ return node
+ ```
+
+=== "C++"
+
+ ```cpp title="avl_tree.cpp"
+ /* 执行旋转操作,使该子树重新恢复平衡 */
+ TreeNode *rotate(TreeNode *node) {
+ // 获取节点 node 的平衡因子
+ int _balanceFactor = balanceFactor(node);
+ // 左偏树
+ if (_balanceFactor > 1) {
+ if (balanceFactor(node->left) >= 0) {
+ // 右旋
+ return rightRotate(node);
+ } else {
+ // 先左旋后右旋
+ node->left = leftRotate(node->left);
+ return rightRotate(node);
+ }
+ }
+ // 右偏树
+ if (_balanceFactor < -1) {
+ if (balanceFactor(node->right) <= 0) {
+ // 左旋
+ return leftRotate(node);
+ } else {
+ // 先右旋后左旋
+ node->right = rightRotate(node->right);
+ return leftRotate(node);
+ }
+ }
+ // 平衡树,无须旋转,直接返回
+ return node;
+ }
+ ```
+
+=== "Java"
+
+ ```java title="avl_tree.java"
+ /* 执行旋转操作,使该子树重新恢复平衡 */
+ TreeNode rotate(TreeNode node) {
+ // 获取节点 node 的平衡因子
+ int balanceFactor = balanceFactor(node);
+ // 左偏树
+ if (balanceFactor > 1) {
+ if (balanceFactor(node.left) >= 0) {
+ // 右旋
+ return rightRotate(node);
+ } else {
+ // 先左旋后右旋
+ node.left = leftRotate(node.left);
+ return rightRotate(node);
+ }
+ }
+ // 右偏树
+ if (balanceFactor < -1) {
+ if (balanceFactor(node.right) <= 0) {
+ // 左旋
+ return leftRotate(node);
+ } else {
+ // 先右旋后左旋
+ node.right = rightRotate(node.right);
+ return leftRotate(node);
+ }
+ }
+ // 平衡树,无须旋转,直接返回
+ return node;
+ }
+ ```
+
+=== "C#"
+
+ ```csharp title="avl_tree.cs"
+ /* 执行旋转操作,使该子树重新恢复平衡 */
+ TreeNode? Rotate(TreeNode? node) {
+ // 获取节点 node 的平衡因子
+ int balanceFactorInt = BalanceFactor(node);
+ // 左偏树
+ if (balanceFactorInt > 1) {
+ if (BalanceFactor(node?.left) >= 0) {
+ // 右旋
+ return RightRotate(node);
+ } else {
+ // 先左旋后右旋
+ node!.left = LeftRotate(node!.left);
+ return RightRotate(node);
+ }
+ }
+ // 右偏树
+ if (balanceFactorInt < -1) {
+ if (BalanceFactor(node?.right) <= 0) {
+ // 左旋
+ return LeftRotate(node);
+ } else {
+ // 先右旋后左旋
+ node!.right = RightRotate(node!.right);
+ return LeftRotate(node);
+ }
+ }
+ // 平衡树,无须旋转,直接返回
+ return node;
+ }
+ ```
+
+=== "Go"
+
+ ```go title="avl_tree.go"
+ /* 执行旋转操作,使该子树重新恢复平衡 */
+ func (t *aVLTree) rotate(node *TreeNode) *TreeNode {
+ // 获取节点 node 的平衡因子
+ // Go 推荐短变量,这里 bf 指代 t.balanceFactor
+ bf := t.balanceFactor(node)
+ // 左偏树
+ if bf > 1 {
+ if t.balanceFactor(node.Left) >= 0 {
+ // 右旋
+ return t.rightRotate(node)
+ } else {
+ // 先左旋后右旋
+ node.Left = t.leftRotate(node.Left)
+ return t.rightRotate(node)
+ }
+ }
+ // 右偏树
+ if bf < -1 {
+ if t.balanceFactor(node.Right) <= 0 {
+ // 左旋
+ return t.leftRotate(node)
+ } else {
+ // 先右旋后左旋
+ node.Right = t.rightRotate(node.Right)
+ return t.leftRotate(node)
+ }
+ }
+ // 平衡树,无须旋转,直接返回
+ return node
+ }
+ ```
+
+=== "Swift"
+
+ ```swift title="avl_tree.swift"
+ /* 执行旋转操作,使该子树重新恢复平衡 */
+ func rotate(node: TreeNode?) -> TreeNode? {
+ // 获取节点 node 的平衡因子
+ let balanceFactor = balanceFactor(node: node)
+ // 左偏树
+ if balanceFactor > 1 {
+ if self.balanceFactor(node: node?.left) >= 0 {
+ // 右旋
+ return rightRotate(node: node)
+ } else {
+ // 先左旋后右旋
+ node?.left = leftRotate(node: node?.left)
+ return rightRotate(node: node)
+ }
+ }
+ // 右偏树
+ if balanceFactor < -1 {
+ if self.balanceFactor(node: node?.right) <= 0 {
+ // 左旋
+ return leftRotate(node: node)
+ } else {
+ // 先右旋后左旋
+ node?.right = rightRotate(node: node?.right)
+ return leftRotate(node: node)
+ }
+ }
+ // 平衡树,无须旋转,直接返回
+ return node
+ }
+ ```
+
+=== "JS"
+
+ ```javascript title="avl_tree.js"
+ /* 执行旋转操作,使该子树重新恢复平衡 */
+ #rotate(node) {
+ // 获取节点 node 的平衡因子
+ const balanceFactor = this.balanceFactor(node);
+ // 左偏树
+ if (balanceFactor > 1) {
+ if (this.balanceFactor(node.left) >= 0) {
+ // 右旋
+ return this.#rightRotate(node);
+ } else {
+ // 先左旋后右旋
+ node.left = this.#leftRotate(node.left);
+ return this.#rightRotate(node);
+ }
+ }
+ // 右偏树
+ if (balanceFactor < -1) {
+ if (this.balanceFactor(node.right) <= 0) {
+ // 左旋
+ return this.#leftRotate(node);
+ } else {
+ // 先右旋后左旋
+ node.right = this.#rightRotate(node.right);
+ return this.#leftRotate(node);
+ }
+ }
+ // 平衡树,无须旋转,直接返回
+ return node;
+ }
+ ```
+
+=== "TS"
+
+ ```typescript title="avl_tree.ts"
+ /* 执行旋转操作,使该子树重新恢复平衡 */
+ rotate(node: TreeNode): TreeNode {
+ // 获取节点 node 的平衡因子
+ const balanceFactor = this.balanceFactor(node);
+ // 左偏树
+ if (balanceFactor > 1) {
+ if (this.balanceFactor(node.left) >= 0) {
+ // 右旋
+ return this.rightRotate(node);
+ } else {
+ // 先左旋后右旋
+ node.left = this.leftRotate(node.left);
+ return this.rightRotate(node);
+ }
+ }
+ // 右偏树
+ if (balanceFactor < -1) {
+ if (this.balanceFactor(node.right) <= 0) {
+ // 左旋
+ return this.leftRotate(node);
+ } else {
+ // 先右旋后左旋
+ node.right = this.rightRotate(node.right);
+ return this.leftRotate(node);
+ }
+ }
+ // 平衡树,无须旋转,直接返回
+ return node;
+ }
+ ```
+
+=== "Dart"
+
+ ```dart title="avl_tree.dart"
+ /* 执行旋转操作,使该子树重新恢复平衡 */
+ TreeNode? rotate(TreeNode? node) {
+ // 获取节点 node 的平衡因子
+ int factor = balanceFactor(node);
+ // 左偏树
+ if (factor > 1) {
+ if (balanceFactor(node!.left) >= 0) {
+ // 右旋
+ return rightRotate(node);
+ } else {
+ // 先左旋后右旋
+ node.left = leftRotate(node.left);
+ return rightRotate(node);
+ }
+ }
+ // 右偏树
+ if (factor < -1) {
+ if (balanceFactor(node!.right) <= 0) {
+ // 左旋
+ return leftRotate(node);
+ } else {
+ // 先右旋后左旋
+ node.right = rightRotate(node.right);
+ return leftRotate(node);
+ }
+ }
+ // 平衡树,无须旋转,直接返回
+ return node;
+ }
+ ```
+
+=== "Rust"
+
+ ```rust title="avl_tree.rs"
+ /* 执行旋转操作,使该子树重新恢复平衡 */
+ fn rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {
+ // 获取节点 node 的平衡因子
+ let balance_factor = Self::balance_factor(node.clone());
+ // 左偏树
+ if balance_factor > 1 {
+ let node = node.unwrap();
+ if Self::balance_factor(node.borrow().left.clone()) >= 0 {
+ // 右旋
+ Self::right_rotate(Some(node))
+ } else {
+ // 先左旋后右旋
+ let left = node.borrow().left.clone();
+ node.borrow_mut().left = Self::left_rotate(left);
+ Self::right_rotate(Some(node))
+ }
+ }
+ // 右偏树
+ else if balance_factor < -1 {
+ let node = node.unwrap();
+ if Self::balance_factor(node.borrow().right.clone()) <= 0 {
+ // 左旋
+ Self::left_rotate(Some(node))
+ } else {
+ // 先右旋后左旋
+ let right = node.borrow().right.clone();
+ node.borrow_mut().right = Self::right_rotate(right);
+ Self::left_rotate(Some(node))
+ }
+ } else {
+ // 平衡树,无须旋转,直接返回
+ node
+ }
+ }
+ ```
+
+=== "C"
+
+ ```c title="avl_tree.c"
+ /* 执行旋转操作,使该子树重新恢复平衡 */
+ TreeNode *rotate(TreeNode *node) {
+ // 获取节点 node 的平衡因子
+ int bf = balanceFactor(node);
+ // 左偏树
+ if (bf > 1) {
+ if (balanceFactor(node->left) >= 0) {
+ // 右旋
+ return rightRotate(node);
+ } else {
+ // 先左旋后右旋
+ node->left = leftRotate(node->left);
+ return rightRotate(node);
+ }
+ }
+ // 右偏树
+ if (bf < -1) {
+ if (balanceFactor(node->right) <= 0) {
+ // 左旋
+ return leftRotate(node);
+ } else {
+ // 先右旋后左旋
+ node->right = rightRotate(node->right);
+ return leftRotate(node);
+ }
+ }
+ // 平衡树,无须旋转,直接返回
+ return node;
+ }
+ ```
+
+=== "Kotlin"
+
+ ```kotlin title="avl_tree.kt"
+ /* 执行旋转操作,使该子树重新恢复平衡 */
+ fun rotate(node: TreeNode): TreeNode {
+ // 获取节点 node 的平衡因子
+ val balanceFactor = balanceFactor(node)
+ // 左偏树
+ if (balanceFactor > 1) {
+ if (balanceFactor(node.left) >= 0) {
+ // 右旋
+ return rightRotate(node)
+ } else {
+ // 先左旋后右旋
+ node.left = leftRotate(node.left)
+ return rightRotate(node)
+ }
+ }
+ // 右偏树
+ if (balanceFactor < -1) {
+ if (balanceFactor(node.right) <= 0) {
+ // 左旋
+ return leftRotate(node)
+ } else {
+ // 先右旋后左旋
+ node.right = rightRotate(node.right)
+ return leftRotate(node)
+ }
+ }
+ // 平衡树,无须旋转,直接返回
+ return node
+ }
+ ```
+
+=== "Ruby"
+
+ ```ruby title="avl_tree.rb"
+ [class]{AVLTree}-[func]{rotate}
+ ```
+
+=== "Zig"
+
+ ```zig title="avl_tree.zig"
+ // 执行旋转操作,使该子树重新恢复平衡
+ fn rotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {
+ // 获取节点 node 的平衡因子
+ var balance_factor = self.balanceFactor(node);
+ // 左偏树
+ if (balance_factor > 1) {
+ if (self.balanceFactor(node.?.left) >= 0) {
+ // 右旋
+ return self.rightRotate(node);
+ } else {
+ // 先左旋后右旋
+ node.?.left = self.leftRotate(node.?.left);
+ return self.rightRotate(node);
+ }
+ }
+ // 右偏树
+ if (balance_factor < -1) {
+ if (self.balanceFactor(node.?.right) <= 0) {
+ // 左旋
+ return self.leftRotate(node);
+ } else {
+ // 先右旋后左旋
+ node.?.right = self.rightRotate(node.?.right);
+ return self.leftRotate(node);
+ }
+ }
+ // 平衡树,无须旋转,直接返回
+ return node;
+ }
+ ```
+
+## 7.5.3 Common operations in AVL trees
+
+### 1. Node insertion
+
+The node insertion operation in AVL trees is similar to that in binary search trees. The only difference is that after inserting a node in an AVL tree, a series of unbalanced nodes may appear along the path from that node to the root node. Therefore, **we need to start from this node and perform rotation operations upwards to restore balance to all unbalanced nodes**. The code is as follows:
+
+=== "Python"
+
+ ```python title="avl_tree.py"
+ def insert(self, val):
+ """插入节点"""
+ self._root = self.insert_helper(self._root, val)
+
+ def insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:
+ """递归插入节点(辅助方法)"""
+ if node is None:
+ return TreeNode(val)
+ # 1. 查找插入位置并插入节点
+ if val < node.val:
+ node.left = self.insert_helper(node.left, val)
+ elif val > node.val:
+ node.right = self.insert_helper(node.right, val)
+ else:
+ # 重复节点不插入,直接返回
+ return node
+ # 更新节点高度
+ self.update_height(node)
+ # 2. 执行旋转操作,使该子树重新恢复平衡
+ return self.rotate(node)
+ ```
+
+=== "C++"
+
+ ```cpp title="avl_tree.cpp"
+ /* 插入节点 */
+ void insert(int val) {
+ root = insertHelper(root, val);
+ }
+
+ /* 递归插入节点(辅助方法) */
+ TreeNode *insertHelper(TreeNode *node, int val) {
+ if (node == nullptr)
+ return new TreeNode(val);
+ /* 1. 查找插入位置并插入节点 */
+ if (val < node->val)
+ node->left = insertHelper(node->left, val);
+ else if (val > node->val)
+ node->right = insertHelper(node->right, val);
+ else
+ return node; // 重复节点不插入,直接返回
+ updateHeight(node); // 更新节点高度
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = rotate(node);
+ // 返回子树的根节点
+ return node;
+ }
+ ```
+
+=== "Java"
+
+ ```java title="avl_tree.java"
+ /* 插入节点 */
+ void insert(int val) {
+ root = insertHelper(root, val);
+ }
+
+ /* 递归插入节点(辅助方法) */
+ TreeNode insertHelper(TreeNode node, int val) {
+ if (node == null)
+ return new TreeNode(val);
+ /* 1. 查找插入位置并插入节点 */
+ if (val < node.val)
+ node.left = insertHelper(node.left, val);
+ else if (val > node.val)
+ node.right = insertHelper(node.right, val);
+ else
+ return node; // 重复节点不插入,直接返回
+ updateHeight(node); // 更新节点高度
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = rotate(node);
+ // 返回子树的根节点
+ return node;
+ }
+ ```
+
+=== "C#"
+
+ ```csharp title="avl_tree.cs"
+ /* 插入节点 */
+ void Insert(int val) {
+ root = InsertHelper(root, val);
+ }
+
+ /* 递归插入节点(辅助方法) */
+ TreeNode? InsertHelper(TreeNode? node, int val) {
+ if (node == null) return new TreeNode(val);
+ /* 1. 查找插入位置并插入节点 */
+ if (val < node.val)
+ node.left = InsertHelper(node.left, val);
+ else if (val > node.val)
+ node.right = InsertHelper(node.right, val);
+ else
+ return node; // 重复节点不插入,直接返回
+ UpdateHeight(node); // 更新节点高度
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = Rotate(node);
+ // 返回子树的根节点
+ return node;
+ }
+ ```
+
+=== "Go"
+
+ ```go title="avl_tree.go"
+ /* 插入节点 */
+ func (t *aVLTree) insert(val int) {
+ t.root = t.insertHelper(t.root, val)
+ }
+
+ /* 递归插入节点(辅助函数) */
+ func (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {
+ if node == nil {
+ return NewTreeNode(val)
+ }
+ /* 1. 查找插入位置并插入节点 */
+ if val < node.Val.(int) {
+ node.Left = t.insertHelper(node.Left, val)
+ } else if val > node.Val.(int) {
+ node.Right = t.insertHelper(node.Right, val)
+ } else {
+ // 重复节点不插入,直接返回
+ return node
+ }
+ // 更新节点高度
+ t.updateHeight(node)
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = t.rotate(node)
+ // 返回子树的根节点
+ return node
+ }
+ ```
+
+=== "Swift"
+
+ ```swift title="avl_tree.swift"
+ /* 插入节点 */
+ func insert(val: Int) {
+ root = insertHelper(node: root, val: val)
+ }
+
+ /* 递归插入节点(辅助方法) */
+ func insertHelper(node: TreeNode?, val: Int) -> TreeNode? {
+ var node = node
+ if node == nil {
+ return TreeNode(x: val)
+ }
+ /* 1. 查找插入位置并插入节点 */
+ if val < node!.val {
+ node?.left = insertHelper(node: node?.left, val: val)
+ } else if val > node!.val {
+ node?.right = insertHelper(node: node?.right, val: val)
+ } else {
+ return node // 重复节点不插入,直接返回
+ }
+ updateHeight(node: node) // 更新节点高度
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = rotate(node: node)
+ // 返回子树的根节点
+ return node
+ }
+ ```
+
+=== "JS"
+
+ ```javascript title="avl_tree.js"
+ /* 插入节点 */
+ insert(val) {
+ this.root = this.#insertHelper(this.root, val);
+ }
+
+ /* 递归插入节点(辅助方法) */
+ #insertHelper(node, val) {
+ if (node === null) return new TreeNode(val);
+ /* 1. 查找插入位置并插入节点 */
+ if (val < node.val) node.left = this.#insertHelper(node.left, val);
+ else if (val > node.val)
+ node.right = this.#insertHelper(node.right, val);
+ else return node; // 重复节点不插入,直接返回
+ this.#updateHeight(node); // 更新节点高度
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = this.#rotate(node);
+ // 返回子树的根节点
+ return node;
+ }
+ ```
+
+=== "TS"
+
+ ```typescript title="avl_tree.ts"
+ /* 插入节点 */
+ insert(val: number): void {
+ this.root = this.insertHelper(this.root, val);
+ }
+
+ /* 递归插入节点(辅助方法) */
+ insertHelper(node: TreeNode, val: number): TreeNode {
+ if (node === null) return new TreeNode(val);
+ /* 1. 查找插入位置并插入节点 */
+ if (val < node.val) {
+ node.left = this.insertHelper(node.left, val);
+ } else if (val > node.val) {
+ node.right = this.insertHelper(node.right, val);
+ } else {
+ return node; // 重复节点不插入,直接返回
+ }
+ this.updateHeight(node); // 更新节点高度
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = this.rotate(node);
+ // 返回子树的根节点
+ return node;
+ }
+ ```
+
+=== "Dart"
+
+ ```dart title="avl_tree.dart"
+ /* 插入节点 */
+ void insert(int val) {
+ root = insertHelper(root, val);
+ }
+
+ /* 递归插入节点(辅助方法) */
+ TreeNode? insertHelper(TreeNode? node, int val) {
+ if (node == null) return TreeNode(val);
+ /* 1. 查找插入位置并插入节点 */
+ if (val < node.val)
+ node.left = insertHelper(node.left, val);
+ else if (val > node.val)
+ node.right = insertHelper(node.right, val);
+ else
+ return node; // 重复节点不插入,直接返回
+ updateHeight(node); // 更新节点高度
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = rotate(node);
+ // 返回子树的根节点
+ return node;
+ }
+ ```
+
+=== "Rust"
+
+ ```rust title="avl_tree.rs"
+ /* 插入节点 */
+ fn insert(&mut self, val: i32) {
+ self.root = Self::insert_helper(self.root.clone(), val);
+ }
+
+ /* 递归插入节点(辅助方法) */
+ fn insert_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {
+ match node {
+ Some(mut node) => {
+ /* 1. 查找插入位置并插入节点 */
+ match {
+ let node_val = node.borrow().val;
+ node_val
+ }
+ .cmp(&val)
+ {
+ Ordering::Greater => {
+ let left = node.borrow().left.clone();
+ node.borrow_mut().left = Self::insert_helper(left, val);
+ }
+ Ordering::Less => {
+ let right = node.borrow().right.clone();
+ node.borrow_mut().right = Self::insert_helper(right, val);
+ }
+ Ordering::Equal => {
+ return Some(node); // 重复节点不插入,直接返回
+ }
+ }
+ Self::update_height(Some(node.clone())); // 更新节点高度
+
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = Self::rotate(Some(node)).unwrap();
+ // 返回子树的根节点
+ Some(node)
+ }
+ None => Some(TreeNode::new(val)),
+ }
+ }
+ ```
+
+=== "C"
+
+ ```c title="avl_tree.c"
+ /* 插入节点 */
+ void insert(AVLTree *tree, int val) {
+ tree->root = insertHelper(tree->root, val);
+ }
+
+ /* 递归插入节点(辅助函数) */
+ TreeNode *insertHelper(TreeNode *node, int val) {
+ if (node == NULL) {
+ return newTreeNode(val);
+ }
+ /* 1. 查找插入位置并插入节点 */
+ if (val < node->val) {
+ node->left = insertHelper(node->left, val);
+ } else if (val > node->val) {
+ node->right = insertHelper(node->right, val);
+ } else {
+ // 重复节点不插入,直接返回
+ return node;
+ }
+ // 更新节点高度
+ updateHeight(node);
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = rotate(node);
+ // 返回子树的根节点
+ return node;
+ }
+ ```
+
+=== "Kotlin"
+
+ ```kotlin title="avl_tree.kt"
+ /* 插入节点 */
+ fun insert(value: Int) {
+ root = insertHelper(root, value)
+ }
+
+ /* 递归插入节点(辅助方法) */
+ fun insertHelper(n: TreeNode?, value: Int): TreeNode {
+ if (n == null)
+ return TreeNode(value)
+ var node = n
+ /* 1. 查找插入位置并插入节点 */
+ if (value < node.value) node.left = insertHelper(node.left, value)
+ else if (value > node.value) node.right = insertHelper(node.right, value)
+ else return node // 重复节点不插入,直接返回
+
+ updateHeight(node) // 更新节点高度
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = rotate(node)
+ // 返回子树的根节点
+ return node
+ }
+ ```
+
+=== "Ruby"
+
+ ```ruby title="avl_tree.rb"
+ [class]{AVLTree}-[func]{insert}
+
+ [class]{AVLTree}-[func]{insert_helper}
+ ```
+
+=== "Zig"
+
+ ```zig title="avl_tree.zig"
+ // 插入节点
+ fn insert(self: *Self, val: T) !void {
+ self.root = (try self.insertHelper(self.root, val)).?;
+ }
+
+ // 递归插入节点(辅助方法)
+ fn insertHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) !?*inc.TreeNode(T) {
+ var node = node_;
+ if (node == null) {
+ var tmp_node = try self.mem_allocator.create(inc.TreeNode(T));
+ tmp_node.init(val);
+ return tmp_node;
+ }
+ // 1. 查找插入位置并插入节点
+ if (val < node.?.val) {
+ node.?.left = try self.insertHelper(node.?.left, val);
+ } else if (val > node.?.val) {
+ node.?.right = try self.insertHelper(node.?.right, val);
+ } else {
+ return node; // 重复节点不插入,直接返回
+ }
+ self.updateHeight(node); // 更新节点高度
+ // 2. 执行旋转操作,使该子树重新恢复平衡
+ node = self.rotate(node);
+ // 返回子树的根节点
+ return node;
+ }
+ ```
+
+### 2. Node removal
+
+Similarly, based on the method of removing nodes in binary search trees, rotation operations need to be performed from the bottom up to restore balance to all unbalanced nodes. The code is as follows:
+
+=== "Python"
+
+ ```python title="avl_tree.py"
+ def remove(self, val: int):
+ """删除节点"""
+ self._root = self.remove_helper(self._root, val)
+
+ def remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:
+ """递归删除节点(辅助方法)"""
+ if node is None:
+ return None
+ # 1. 查找节点并删除
+ if val < node.val:
+ node.left = self.remove_helper(node.left, val)
+ elif val > node.val:
+ node.right = self.remove_helper(node.right, val)
+ else:
+ if node.left is None or node.right is None:
+ child = node.left or node.right
+ # 子节点数量 = 0 ,直接删除 node 并返回
+ if child is None:
+ return None
+ # 子节点数量 = 1 ,直接删除 node
+ else:
+ node = child
+ else:
+ # 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
+ temp = node.right
+ while temp.left is not None:
+ temp = temp.left
+ node.right = self.remove_helper(node.right, temp.val)
+ node.val = temp.val
+ # 更新节点高度
+ self.update_height(node)
+ # 2. 执行旋转操作,使该子树重新恢复平衡
+ return self.rotate(node)
+ ```
+
+=== "C++"
+
+ ```cpp title="avl_tree.cpp"
+ /* 删除节点 */
+ void remove(int val) {
+ root = removeHelper(root, val);
+ }
+
+ /* 递归删除节点(辅助方法) */
+ TreeNode *removeHelper(TreeNode *node, int val) {
+ if (node == nullptr)
+ return nullptr;
+ /* 1. 查找节点并删除 */
+ if (val < node->val)
+ node->left = removeHelper(node->left, val);
+ else if (val > node->val)
+ node->right = removeHelper(node->right, val);
+ else {
+ if (node->left == nullptr || node->right == nullptr) {
+ TreeNode *child = node->left != nullptr ? node->left : node->right;
+ // 子节点数量 = 0 ,直接删除 node 并返回
+ if (child == nullptr) {
+ delete node;
+ return nullptr;
+ }
+ // 子节点数量 = 1 ,直接删除 node
+ else {
+ delete node;
+ node = child;
+ }
+ } else {
+ // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
+ TreeNode *temp = node->right;
+ while (temp->left != nullptr) {
+ temp = temp->left;
+ }
+ int tempVal = temp->val;
+ node->right = removeHelper(node->right, temp->val);
+ node->val = tempVal;
+ }
+ }
+ updateHeight(node); // 更新节点高度
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = rotate(node);
+ // 返回子树的根节点
+ return node;
+ }
+ ```
+
+=== "Java"
+
+ ```java title="avl_tree.java"
+ /* 删除节点 */
+ void remove(int val) {
+ root = removeHelper(root, val);
+ }
+
+ /* 递归删除节点(辅助方法) */
+ TreeNode removeHelper(TreeNode node, int val) {
+ if (node == null)
+ return null;
+ /* 1. 查找节点并删除 */
+ if (val < node.val)
+ node.left = removeHelper(node.left, val);
+ else if (val > node.val)
+ node.right = removeHelper(node.right, val);
+ else {
+ if (node.left == null || node.right == null) {
+ TreeNode child = node.left != null ? node.left : node.right;
+ // 子节点数量 = 0 ,直接删除 node 并返回
+ if (child == null)
+ return null;
+ // 子节点数量 = 1 ,直接删除 node
+ else
+ node = child;
+ } else {
+ // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
+ TreeNode temp = node.right;
+ while (temp.left != null) {
+ temp = temp.left;
+ }
+ node.right = removeHelper(node.right, temp.val);
+ node.val = temp.val;
+ }
+ }
+ updateHeight(node); // 更新节点高度
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = rotate(node);
+ // 返回子树的根节点
+ return node;
+ }
+ ```
+
+=== "C#"
+
+ ```csharp title="avl_tree.cs"
+ /* 删除节点 */
+ void Remove(int val) {
+ root = RemoveHelper(root, val);
+ }
+
+ /* 递归删除节点(辅助方法) */
+ TreeNode? RemoveHelper(TreeNode? node, int val) {
+ if (node == null) return null;
+ /* 1. 查找节点并删除 */
+ if (val < node.val)
+ node.left = RemoveHelper(node.left, val);
+ else if (val > node.val)
+ node.right = RemoveHelper(node.right, val);
+ else {
+ if (node.left == null || node.right == null) {
+ TreeNode? child = node.left ?? node.right;
+ // 子节点数量 = 0 ,直接删除 node 并返回
+ if (child == null)
+ return null;
+ // 子节点数量 = 1 ,直接删除 node
+ else
+ node = child;
+ } else {
+ // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
+ TreeNode? temp = node.right;
+ while (temp.left != null) {
+ temp = temp.left;
+ }
+ node.right = RemoveHelper(node.right, temp.val!.Value);
+ node.val = temp.val;
+ }
+ }
+ UpdateHeight(node); // 更新节点高度
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = Rotate(node);
+ // 返回子树的根节点
+ return node;
+ }
+ ```
+
+=== "Go"
+
+ ```go title="avl_tree.go"
+ /* 删除节点 */
+ func (t *aVLTree) remove(val int) {
+ t.root = t.removeHelper(t.root, val)
+ }
+
+ /* 递归删除节点(辅助函数) */
+ func (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {
+ if node == nil {
+ return nil
+ }
+ /* 1. 查找节点并删除 */
+ if val < node.Val.(int) {
+ node.Left = t.removeHelper(node.Left, val)
+ } else if val > node.Val.(int) {
+ node.Right = t.removeHelper(node.Right, val)
+ } else {
+ if node.Left == nil || node.Right == nil {
+ child := node.Left
+ if node.Right != nil {
+ child = node.Right
+ }
+ if child == nil {
+ // 子节点数量 = 0 ,直接删除 node 并返回
+ return nil
+ } else {
+ // 子节点数量 = 1 ,直接删除 node
+ node = child
+ }
+ } else {
+ // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
+ temp := node.Right
+ for temp.Left != nil {
+ temp = temp.Left
+ }
+ node.Right = t.removeHelper(node.Right, temp.Val.(int))
+ node.Val = temp.Val
+ }
+ }
+ // 更新节点高度
+ t.updateHeight(node)
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = t.rotate(node)
+ // 返回子树的根节点
+ return node
+ }
+ ```
+
+=== "Swift"
+
+ ```swift title="avl_tree.swift"
+ /* 删除节点 */
+ func remove(val: Int) {
+ root = removeHelper(node: root, val: val)
+ }
+
+ /* 递归删除节点(辅助方法) */
+ func removeHelper(node: TreeNode?, val: Int) -> TreeNode? {
+ var node = node
+ if node == nil {
+ return nil
+ }
+ /* 1. 查找节点并删除 */
+ if val < node!.val {
+ node?.left = removeHelper(node: node?.left, val: val)
+ } else if val > node!.val {
+ node?.right = removeHelper(node: node?.right, val: val)
+ } else {
+ if node?.left == nil || node?.right == nil {
+ let child = node?.left ?? node?.right
+ // 子节点数量 = 0 ,直接删除 node 并返回
+ if child == nil {
+ return nil
+ }
+ // 子节点数量 = 1 ,直接删除 node
+ else {
+ node = child
+ }
+ } else {
+ // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
+ var temp = node?.right
+ while temp?.left != nil {
+ temp = temp?.left
+ }
+ node?.right = removeHelper(node: node?.right, val: temp!.val)
+ node?.val = temp!.val
+ }
+ }
+ updateHeight(node: node) // 更新节点高度
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = rotate(node: node)
+ // 返回子树的根节点
+ return node
+ }
+ ```
+
+=== "JS"
+
+ ```javascript title="avl_tree.js"
+ /* 删除节点 */
+ remove(val) {
+ this.root = this.#removeHelper(this.root, val);
+ }
+
+ /* 递归删除节点(辅助方法) */
+ #removeHelper(node, val) {
+ if (node === null) return null;
+ /* 1. 查找节点并删除 */
+ if (val < node.val) node.left = this.#removeHelper(node.left, val);
+ else if (val > node.val)
+ node.right = this.#removeHelper(node.right, val);
+ else {
+ if (node.left === null || node.right === null) {
+ const child = node.left !== null ? node.left : node.right;
+ // 子节点数量 = 0 ,直接删除 node 并返回
+ if (child === null) return null;
+ // 子节点数量 = 1 ,直接删除 node
+ else node = child;
+ } else {
+ // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
+ let temp = node.right;
+ while (temp.left !== null) {
+ temp = temp.left;
+ }
+ node.right = this.#removeHelper(node.right, temp.val);
+ node.val = temp.val;
+ }
+ }
+ this.#updateHeight(node); // 更新节点高度
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = this.#rotate(node);
+ // 返回子树的根节点
+ return node;
+ }
+ ```
+
+=== "TS"
+
+ ```typescript title="avl_tree.ts"
+ /* 删除节点 */
+ remove(val: number): void {
+ this.root = this.removeHelper(this.root, val);
+ }
+
+ /* 递归删除节点(辅助方法) */
+ removeHelper(node: TreeNode, val: number): TreeNode {
+ if (node === null) return null;
+ /* 1. 查找节点并删除 */
+ if (val < node.val) {
+ node.left = this.removeHelper(node.left, val);
+ } else if (val > node.val) {
+ node.right = this.removeHelper(node.right, val);
+ } else {
+ if (node.left === null || node.right === null) {
+ const child = node.left !== null ? node.left : node.right;
+ // 子节点数量 = 0 ,直接删除 node 并返回
+ if (child === null) {
+ return null;
+ } else {
+ // 子节点数量 = 1 ,直接删除 node
+ node = child;
+ }
+ } else {
+ // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
+ let temp = node.right;
+ while (temp.left !== null) {
+ temp = temp.left;
+ }
+ node.right = this.removeHelper(node.right, temp.val);
+ node.val = temp.val;
+ }
+ }
+ this.updateHeight(node); // 更新节点高度
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = this.rotate(node);
+ // 返回子树的根节点
+ return node;
+ }
+ ```
+
+=== "Dart"
+
+ ```dart title="avl_tree.dart"
+ /* 删除节点 */
+ void remove(int val) {
+ root = removeHelper(root, val);
+ }
+
+ /* 递归删除节点(辅助方法) */
+ TreeNode? removeHelper(TreeNode? node, int val) {
+ if (node == null) return null;
+ /* 1. 查找节点并删除 */
+ if (val < node.val)
+ node.left = removeHelper(node.left, val);
+ else if (val > node.val)
+ node.right = removeHelper(node.right, val);
+ else {
+ if (node.left == null || node.right == null) {
+ TreeNode? child = node.left ?? node.right;
+ // 子节点数量 = 0 ,直接删除 node 并返回
+ if (child == null)
+ return null;
+ // 子节点数量 = 1 ,直接删除 node
+ else
+ node = child;
+ } else {
+ // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
+ TreeNode? temp = node.right;
+ while (temp!.left != null) {
+ temp = temp.left;
+ }
+ node.right = removeHelper(node.right, temp.val);
+ node.val = temp.val;
+ }
+ }
+ updateHeight(node); // 更新节点高度
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = rotate(node);
+ // 返回子树的根节点
+ return node;
+ }
+ ```
+
+=== "Rust"
+
+ ```rust title="avl_tree.rs"
+ /* 删除节点 */
+ fn remove(&self, val: i32) {
+ Self::remove_helper(self.root.clone(), val);
+ }
+
+ /* 递归删除节点(辅助方法) */
+ fn remove_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {
+ match node {
+ Some(mut node) => {
+ /* 1. 查找节点并删除 */
+ if val < node.borrow().val {
+ let left = node.borrow().left.clone();
+ node.borrow_mut().left = Self::remove_helper(left, val);
+ } else if val > node.borrow().val {
+ let right = node.borrow().right.clone();
+ node.borrow_mut().right = Self::remove_helper(right, val);
+ } else if node.borrow().left.is_none() || node.borrow().right.is_none() {
+ let child = if node.borrow().left.is_some() {
+ node.borrow().left.clone()
+ } else {
+ node.borrow().right.clone()
+ };
+ match child {
+ // 子节点数量 = 0 ,直接删除 node 并返回
+ None => {
+ return None;
+ }
+ // 子节点数量 = 1 ,直接删除 node
+ Some(child) => node = child,
+ }
+ } else {
+ // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
+ let mut temp = node.borrow().right.clone().unwrap();
+ loop {
+ let temp_left = temp.borrow().left.clone();
+ if temp_left.is_none() {
+ break;
+ }
+ temp = temp_left.unwrap();
+ }
+ let right = node.borrow().right.clone();
+ node.borrow_mut().right = Self::remove_helper(right, temp.borrow().val);
+ node.borrow_mut().val = temp.borrow().val;
+ }
+ Self::update_height(Some(node.clone())); // 更新节点高度
+
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = Self::rotate(Some(node)).unwrap();
+ // 返回子树的根节点
+ Some(node)
+ }
+ None => None,
+ }
+ }
+ ```
+
+=== "C"
+
+ ```c title="avl_tree.c"
+ /* 删除节点 */
+ // 由于引入了 stdio.h ,此处无法使用 remove 关键词
+ void removeItem(AVLTree *tree, int val) {
+ TreeNode *root = removeHelper(tree->root, val);
+ }
+
+ /* 递归删除节点(辅助函数) */
+ TreeNode *removeHelper(TreeNode *node, int val) {
+ TreeNode *child, *grandChild;
+ if (node == NULL) {
+ return NULL;
+ }
+ /* 1. 查找节点并删除 */
+ if (val < node->val) {
+ node->left = removeHelper(node->left, val);
+ } else if (val > node->val) {
+ node->right = removeHelper(node->right, val);
+ } else {
+ if (node->left == NULL || node->right == NULL) {
+ child = node->left;
+ if (node->right != NULL) {
+ child = node->right;
+ }
+ // 子节点数量 = 0 ,直接删除 node 并返回
+ if (child == NULL) {
+ return NULL;
+ } else {
+ // 子节点数量 = 1 ,直接删除 node
+ node = child;
+ }
+ } else {
+ // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
+ TreeNode *temp = node->right;
+ while (temp->left != NULL) {
+ temp = temp->left;
+ }
+ int tempVal = temp->val;
+ node->right = removeHelper(node->right, temp->val);
+ node->val = tempVal;
+ }
+ }
+ // 更新节点高度
+ updateHeight(node);
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = rotate(node);
+ // 返回子树的根节点
+ return node;
+ }
+ ```
+
+=== "Kotlin"
+
+ ```kotlin title="avl_tree.kt"
+ /* 删除节点 */
+ fun remove(value: Int) {
+ root = removeHelper(root, value)
+ }
+
+ /* 递归删除节点(辅助方法) */
+ fun removeHelper(n: TreeNode?, value: Int): TreeNode? {
+ var node = n ?: return null
+ /* 1. 查找节点并删除 */
+ if (value < node.value) node.left = removeHelper(node.left, value)
+ else if (value > node.value) node.right = removeHelper(node.right, value)
+ else {
+ if (node.left == null || node.right == null) {
+ val child = if (node.left != null) node.left else node.right
+ // 子节点数量 = 0 ,直接删除 node 并返回
+ if (child == null) return null
+ else node = child
+ } else {
+ // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
+ var temp = node.right
+ while (temp!!.left != null) {
+ temp = temp.left
+ }
+ node.right = removeHelper(node.right, temp.value)
+ node.value = temp.value
+ }
+ }
+ updateHeight(node) // 更新节点高度
+ /* 2. 执行旋转操作,使该子树重新恢复平衡 */
+ node = rotate(node)
+ // 返回子树的根节点
+ return node
+ }
+ ```
+
+=== "Ruby"
+
+ ```ruby title="avl_tree.rb"
+ [class]{AVLTree}-[func]{remove}
+
+ [class]{AVLTree}-[func]{remove_helper}
+ ```
+
+=== "Zig"
+
+ ```zig title="avl_tree.zig"
+ // 删除节点
+ fn remove(self: *Self, val: T) void {
+ self.root = self.removeHelper(self.root, val).?;
+ }
+
+ // 递归删除节点(辅助方法)
+ fn removeHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) ?*inc.TreeNode(T) {
+ var node = node_;
+ if (node == null) return null;
+ // 1. 查找节点并删除
+ if (val < node.?.val) {
+ node.?.left = self.removeHelper(node.?.left, val);
+ } else if (val > node.?.val) {
+ node.?.right = self.removeHelper(node.?.right, val);
+ } else {
+ if (node.?.left == null or node.?.right == null) {
+ var child = if (node.?.left != null) node.?.left else node.?.right;
+ // 子节点数量 = 0 ,直接删除 node 并返回
+ if (child == null) {
+ return null;
+ // 子节点数量 = 1 ,直接删除 node
+ } else {
+ node = child;
+ }
+ } else {
+ // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
+ var temp = node.?.right;
+ while (temp.?.left != null) {
+ temp = temp.?.left;
+ }
+ node.?.right = self.removeHelper(node.?.right, temp.?.val);
+ node.?.val = temp.?.val;
+ }
+ }
+ self.updateHeight(node); // 更新节点高度
+ // 2. 执行旋转操作,使该子树重新恢复平衡
+ node = self.rotate(node);
+ // 返回子树的根节点
+ return node;
+ }
+ ```
+
+### 3. Node search
+
+The node search operation in AVL trees is consistent with that in binary search trees and will not be detailed here.
+
+## 7.5.4 Typical applications of AVL trees
+
+- Organizing and storing large amounts of data, suitable for scenarios with high-frequency searches and low-frequency intertions and removals.
+- Used to build index systems in databases.
+- Red-black trees are also a common type of balanced binary search tree. Compared to AVL trees, red-black trees have more relaxed balancing conditions, require fewer rotations for node insertion and removal, and have a higher average efficiency for node addition and removal operations.
diff --git a/en/docs/chapter_tree/binary_search_tree.md b/en/docs/chapter_tree/binary_search_tree.md
new file mode 100755
index 000000000..6032cec08
--- /dev/null
+++ b/en/docs/chapter_tree/binary_search_tree.md
@@ -0,0 +1,1628 @@
+---
+comments: true
+---
+
+# 7.4 Binary search tree
+
+As shown in the Figure 7-16 , a "binary search tree" satisfies the following conditions.
+
+1. For the root node, the value of all nodes in the left subtree < the value of the root node < the value of all nodes in the right subtree.
+2. The left and right subtrees of any node are also binary search trees, i.e., they satisfy condition `1.` as well.
+
+![Binary search tree](binary_search_tree.assets/binary_search_tree.png){ class="animation-figure" }
+
+ Figure 7-16 Binary search tree
+
+## 7.4.1 Operations on a binary search tree
+
+We encapsulate the binary search tree as a class `BinarySearchTree` and declare a member variable `root`, pointing to the tree's root node.
+
+### 1. Searching for a node
+
+Given a target node value `num`, one can search according to the properties of the binary search tree. As shown in the Figure 7-17 , we declare a node `cur` and start from the binary tree's root node `root`, looping to compare the size relationship between the node value `cur.val` and `num`.
+
+- If `cur.val < num`, it means the target node is in `cur`'s right subtree, thus execute `cur = cur.right`.
+- If `cur.val > num`, it means the target node is in `cur`'s left subtree, thus execute `cur = cur.left`.
+- If `cur.val = num`, it means the target node is found, exit the loop and return the node.
+
+=== "<1>"
+ ![Example of searching for a node in a binary search tree](binary_search_tree.assets/bst_search_step1.png){ class="animation-figure" }
+
+=== "<2>"
+ ![bst_search_step2](binary_search_tree.assets/bst_search_step2.png){ class="animation-figure" }
+
+=== "<3>"
+ ![bst_search_step3](binary_search_tree.assets/bst_search_step3.png){ class="animation-figure" }
+
+=== "<4>"
+ ![bst_search_step4](binary_search_tree.assets/bst_search_step4.png){ class="animation-figure" }
+
+ Figure 7-17 Example of searching for a node in a binary search tree
+
+The search operation in a binary search tree works on the same principle as the binary search algorithm, eliminating half of the possibilities in each round. The number of loops is at most the height of the binary tree. When the binary tree is balanced, it uses $O(\log n)$ time. Example code is as follows:
+
+=== "Python"
+
+ ```python title="binary_search_tree.py"
+ def search(self, num: int) -> TreeNode | None:
+ """查找节点"""
+ cur = self._root
+ # 循环查找,越过叶节点后跳出
+ while cur is not None:
+ # 目标节点在 cur 的右子树中
+ if cur.val < num:
+ cur = cur.right
+ # 目标节点在 cur 的左子树中
+ elif cur.val > num:
+ cur = cur.left
+ # 找到目标节点,跳出循环
+ else:
+ break
+ return cur
+ ```
+
+=== "C++"
+
+ ```cpp title="binary_search_tree.cpp"
+ /* 查找节点 */
+ TreeNode *search(int num) {
+ TreeNode *cur = root;
+ // 循环查找,越过叶节点后跳出
+ while (cur != nullptr) {
+ // 目标节点在 cur 的右子树中
+ if (cur->val < num)
+ cur = cur->right;
+ // 目标节点在 cur 的左子树中
+ else if (cur->val > num)
+ cur = cur->left;
+ // 找到目标节点,跳出循环
+ else
+ break;
+ }
+ // 返回目标节点
+ return cur;
+ }
+ ```
+
+=== "Java"
+
+ ```java title="binary_search_tree.java"
+ /* 查找节点 */
+ TreeNode search(int num) {
+ TreeNode cur = root;
+ // 循环查找,越过叶节点后跳出
+ while (cur != null) {
+ // 目标节点在 cur 的右子树中
+ if (cur.val < num)
+ cur = cur.right;
+ // 目标节点在 cur 的左子树中
+ else if (cur.val > num)
+ cur = cur.left;
+ // 找到目标节点,跳出循环
+ else
+ break;
+ }
+ // 返回目标节点
+ return cur;
+ }
+ ```
+
+=== "C#"
+
+ ```csharp title="binary_search_tree.cs"
+ /* 查找节点 */
+ TreeNode? Search(int num) {
+ TreeNode? cur = root;
+ // 循环查找,越过叶节点后跳出
+ while (cur != null) {
+ // 目标节点在 cur 的右子树中
+ if (cur.val < num) cur =
+ cur.right;
+ // 目标节点在 cur 的左子树中
+ else if (cur.val > num)
+ cur = cur.left;
+ // 找到目标节点,跳出循环
+ else
+ break;
+ }
+ // 返回目标节点
+ return cur;
+ }
+ ```
+
+=== "Go"
+
+ ```go title="binary_search_tree.go"
+ /* 查找节点 */
+ func (bst *binarySearchTree) search(num int) *TreeNode {
+ node := bst.root
+ // 循环查找,越过叶节点后跳出
+ for node != nil {
+ if node.Val.(int) < num {
+ // 目标节点在 cur 的右子树中
+ node = node.Right
+ } else if node.Val.(int) > num {
+ // 目标节点在 cur 的左子树中
+ node = node.Left
+ } else {
+ // 找到目标节点,跳出循环
+ break
+ }
+ }
+ // 返回目标节点
+ return node
+ }
+ ```
+
+=== "Swift"
+
+ ```swift title="binary_search_tree.swift"
+ /* 查找节点 */
+ func search(num: Int) -> TreeNode? {
+ var cur = root
+ // 循环查找,越过叶节点后跳出
+ while cur != nil {
+ // 目标节点在 cur 的右子树中
+ if cur!.val < num {
+ cur = cur?.right
+ }
+ // 目标节点在 cur 的左子树中
+ else if cur!.val > num {
+ cur = cur?.left
+ }
+ // 找到目标节点,跳出循环
+ else {
+ break
+ }
+ }
+ // 返回目标节点
+ return cur
+ }
+ ```
+
+=== "JS"
+
+ ```javascript title="binary_search_tree.js"
+ /* 查找节点 */
+ search(num) {
+ let cur = this.root;
+ // 循环查找,越过叶节点后跳出
+ while (cur !== null) {
+ // 目标节点在 cur 的右子树中
+ if (cur.val < num) cur = cur.right;
+ // 目标节点在 cur 的左子树中
+ else if (cur.val > num) cur = cur.left;
+ // 找到目标节点,跳出循环
+ else break;
+ }
+ // 返回目标节点
+ return cur;
+ }
+ ```
+
+=== "TS"
+
+ ```typescript title="binary_search_tree.ts"
+ /* 查找节点 */
+ search(num: number): TreeNode | null {
+ let cur = this.root;
+ // 循环查找,越过叶节点后跳出
+ while (cur !== null) {
+ // 目标节点在 cur 的右子树中
+ if (cur.val < num) cur = cur.right;
+ // 目标节点在 cur 的左子树中
+ else if (cur.val > num) cur = cur.left;
+ // 找到目标节点,跳出循环
+ else break;
+ }
+ // 返回目标节点
+ return cur;
+ }
+ ```
+
+=== "Dart"
+
+ ```dart title="binary_search_tree.dart"
+ /* 查找节点 */
+ TreeNode? search(int _num) {
+ TreeNode? cur = _root;
+ // 循环查找,越过叶节点后跳出
+ while (cur != null) {
+ // 目标节点在 cur 的右子树中
+ if (cur.val < _num)
+ cur = cur.right;
+ // 目标节点在 cur 的左子树中
+ else if (cur.val > _num)
+ cur = cur.left;
+ // 找到目标节点,跳出循环
+ else
+ break;
+ }
+ // 返回目标节点
+ return cur;
+ }
+ ```
+
+=== "Rust"
+
+ ```rust title="binary_search_tree.rs"
+ /* 查找节点 */
+ pub fn search(&self, num: i32) -> OptionTreeNodeRc {
+ let mut cur = self.root.clone();
+ // 循环查找,越过叶节点后跳出
+ while let Some(node) = cur.clone() {
+ match num.cmp(&node.borrow().val) {
+ // 目标节点在 cur 的右子树中
+ Ordering::Greater => cur = node.borrow().right.clone(),
+ // 目标节点在 cur 的左子树中
+ Ordering::Less => cur = node.borrow().left.clone(),
+ // 找到目标节点,跳出循环
+ Ordering::Equal => break,
+ }
+ }
+
+ // 返回目标节点
+ cur
+ }
+ ```
+
+=== "C"
+
+ ```c title="binary_search_tree.c"
+ /* 查找节点 */
+ TreeNode *search(BinarySearchTree *bst, int num) {
+ TreeNode *cur = bst->root;
+ // 循环查找,越过叶节点后跳出
+ while (cur != NULL) {
+ if (cur->val < num) {
+ // 目标节点在 cur 的右子树中
+ cur = cur->right;
+ } else if (cur->val > num) {
+ // 目标节点在 cur 的左子树中
+ cur = cur->left;
+ } else {
+ // 找到目标节点,跳出循环
+ break;
+ }
+ }
+ // 返回目标节点
+ return cur;
+ }
+ ```
+
+=== "Kotlin"
+
+ ```kotlin title="binary_search_tree.kt"
+ /* 查找节点 */
+ fun search(num: Int): TreeNode? {
+ var cur = root
+ // 循环查找,越过叶节点后跳出
+ while (cur != null) {
+ // 目标节点在 cur 的右子树中
+ cur = if (cur.value < num) cur.right
+ // 目标节点在 cur 的左子树中
+ else if (cur.value > num) cur.left
+ // 找到目标节点,跳出循环
+ else break
+ }
+ // 返回目标节点
+ return cur
+ }
+ ```
+
+=== "Ruby"
+
+ ```ruby title="binary_search_tree.rb"
+ [class]{BinarySearchTree}-[func]{search}
+ ```
+
+=== "Zig"
+
+ ```zig title="binary_search_tree.zig"
+ // 查找节点
+ fn search(self: *Self, num: T) ?*inc.TreeNode(T) {
+ var cur = self.root;
+ // 循环查找,越过叶节点后跳出
+ while (cur != null) {
+ // 目标节点在 cur 的右子树中
+ if (cur.?.val < num) {
+ cur = cur.?.right;
+ // 目标节点在 cur 的左子树中
+ } else if (cur.?.val > num) {
+ cur = cur.?.left;
+ // 找到目标节点,跳出循环
+ } else {
+ break;
+ }
+ }
+ // 返回目标节点
+ return cur;
+ }
+ ```
+
+??? pythontutor "Code Visualization"
+
+
+
+
+### 2. Inserting a node
+
+Given an element `num` to be inserted, to maintain the property of the binary search tree "left subtree < root node < right subtree," the insertion operation proceeds as shown in the Figure 7-18 .
+
+1. **Finding the insertion position**: Similar to the search operation, start from the root node and loop downwards according to the size relationship between the current node value and `num` until passing through the leaf node (traversing to `None`) then exit the loop.
+2. **Insert the node at that position**: Initialize the node `num` and place it where `None` was.
+
+![Inserting a node into a binary search tree](binary_search_tree.assets/bst_insert.png){ class="animation-figure" }
+
+ Figure 7-18 Inserting a node into a binary search tree
+
+In the code implementation, note the following two points.
+
+- The binary search tree does not allow duplicate nodes; otherwise, it will violate its definition. Therefore, if the node to be inserted already exists in the tree, the insertion is not performed, and it directly returns.
+- To perform the insertion operation, we need to use the node `pre` to save the node from the last loop. This way, when traversing to `None`, we can get its parent node, thus completing the node insertion operation.
+
+=== "Python"
+
+ ```python title="binary_search_tree.py"
+ def insert(self, num: int):
+ """插入节点"""
+ # 若树为空,则初始化根节点
+ if self._root is None:
+ self._root = TreeNode(num)
+ return
+ # 循环查找,越过叶节点后跳出
+ cur, pre = self._root, None
+ while cur is not None:
+ # 找到重复节点,直接返回
+ if cur.val == num:
+ return
+ pre = cur
+ # 插入位置在 cur 的右子树中
+ if cur.val < num:
+ cur = cur.right
+ # 插入位置在 cur 的左子树中
+ else:
+ cur = cur.left
+ # 插入节点
+ node = TreeNode(num)
+ if pre.val < num:
+ pre.right = node
+ else:
+ pre.left = node
+ ```
+
+=== "C++"
+
+ ```cpp title="binary_search_tree.cpp"
+ /* 插入节点 */
+ void insert(int num) {
+ // 若树为空,则初始化根节点
+ if (root == nullptr) {
+ root = new TreeNode(num);
+ return;
+ }
+ TreeNode *cur = root, *pre = nullptr;
+ // 循环查找,越过叶节点后跳出
+ while (cur != nullptr) {
+ // 找到重复节点,直接返回
+ if (cur->val == num)
+ return;
+ pre = cur;
+ // 插入位置在 cur 的右子树中
+ if (cur->val < num)
+ cur = cur->right;
+ // 插入位置在 cur 的左子树中
+ else
+ cur = cur->left;
+ }
+ // 插入节点
+ TreeNode *node = new TreeNode(num);
+ if (pre->val < num)
+ pre->right = node;
+ else
+ pre->left = node;
+ }
+ ```
+
+=== "Java"
+
+ ```java title="binary_search_tree.java"
+ /* 插入节点 */
+ void insert(int num) {
+ // 若树为空,则初始化根节点
+ if (root == null) {
+ root = new TreeNode(num);
+ return;
+ }
+ TreeNode cur = root, pre = null;
+ // 循环查找,越过叶节点后跳出
+ while (cur != null) {
+ // 找到重复节点,直接返回
+ if (cur.val == num)
+ return;
+ pre = cur;
+ // 插入位置在 cur 的右子树中
+ if (cur.val < num)
+ cur = cur.right;
+ // 插入位置在 cur 的左子树中
+ else
+ cur = cur.left;
+ }
+ // 插入节点
+ TreeNode node = new TreeNode(num);
+ if (pre.val < num)
+ pre.right = node;
+ else
+ pre.left = node;
+ }
+ ```
+
+=== "C#"
+
+ ```csharp title="binary_search_tree.cs"
+ /* 插入节点 */
+ void Insert(int num) {
+ // 若树为空,则初始化根节点
+ if (root == null) {
+ root = new TreeNode(num);
+ return;
+ }
+ TreeNode? cur = root, pre = null;
+ // 循环查找,越过叶节点后跳出
+ while (cur != null) {
+ // 找到重复节点,直接返回
+ if (cur.val == num)
+ return;
+ pre = cur;
+ // 插入位置在 cur 的右子树中
+ if (cur.val < num)
+ cur = cur.right;
+ // 插入位置在 cur 的左子树中
+ else
+ cur = cur.left;
+ }
+
+ // 插入节点
+ TreeNode node = new(num);
+ if (pre != null) {
+ if (pre.val < num)
+ pre.right = node;
+ else
+ pre.left = node;
+ }
+ }
+ ```
+
+=== "Go"
+
+ ```go title="binary_search_tree.go"
+ /* 插入节点 */
+ func (bst *binarySearchTree) insert(num int) {
+ cur := bst.root
+ // 若树为空,则初始化根节点
+ if cur == nil {
+ bst.root = NewTreeNode(num)
+ return
+ }
+ // 待插入节点之前的节点位置
+ var pre *TreeNode = nil
+ // 循环查找,越过叶节点后跳出
+ for cur != nil {
+ if cur.Val == num {
+ return
+ }
+ pre = cur
+ if cur.Val.(int) < num {
+ cur = cur.Right
+ } else {
+ cur = cur.Left
+ }
+ }
+ // 插入节点
+ node := NewTreeNode(num)
+ if pre.Val.(int) < num {
+ pre.Right = node
+ } else {
+ pre.Left = node
+ }
+ }
+ ```
+
+=== "Swift"
+
+ ```swift title="binary_search_tree.swift"
+ /* 插入节点 */
+ func insert(num: Int) {
+ // 若树为空,则初始化根节点
+ if root == nil {
+ root = TreeNode(x: num)
+ return
+ }
+ var cur = root
+ var pre: TreeNode?
+ // 循环查找,越过叶节点后跳出
+ while cur != nil {
+ // 找到重复节点,直接返回
+ if cur!.val == num {
+ return
+ }
+ pre = cur
+ // 插入位置在 cur 的右子树中
+ if cur!.val < num {
+ cur = cur?.right
+ }
+ // 插入位置在 cur 的左子树中
+ else {
+ cur = cur?.left
+ }
+ }
+ // 插入节点
+ let node = TreeNode(x: num)
+ if pre!.val < num {
+ pre?.right = node
+ } else {
+ pre?.left = node
+ }
+ }
+ ```
+
+=== "JS"
+
+ ```javascript title="binary_search_tree.js"
+ /* 插入节点 */
+ insert(num) {
+ // 若树为空,则初始化根节点
+ if (this.root === null) {
+ this.root = new TreeNode(num);
+ return;
+ }
+ let cur = this.root,
+ pre = null;
+ // 循环查找,越过叶节点后跳出
+ while (cur !== null) {
+ // 找到重复节点,直接返回
+ if (cur.val === num) return;
+ pre = cur;
+ // 插入位置在 cur 的右子树中
+ if (cur.val < num) cur = cur.right;
+ // 插入位置在 cur 的左子树中
+ else cur = cur.left;
+ }
+ // 插入节点
+ const node = new TreeNode(num);
+ if (pre.val < num) pre.right = node;
+ else pre.left = node;
+ }
+ ```
+
+=== "TS"
+
+ ```typescript title="binary_search_tree.ts"
+ /* 插入节点 */
+ insert(num: number): void {
+ // 若树为空,则初始化根节点
+ if (this.root === null) {
+ this.root = new TreeNode(num);
+ return;
+ }
+ let cur: TreeNode | null = this.root,
+ pre: TreeNode | null = null;
+ // 循环查找,越过叶节点后跳出
+ while (cur !== null) {
+ // 找到重复节点,直接返回
+ if (cur.val === num) return;
+ pre = cur;
+ // 插入位置在 cur 的右子树中
+ if (cur.val < num) cur = cur.right;
+ // 插入位置在 cur 的左子树中
+ else cur = cur.left;
+ }
+ // 插入节点
+ const node = new TreeNode(num);
+ if (pre!.val < num) pre!.right = node;
+ else pre!.left = node;
+ }
+ ```
+
+=== "Dart"
+
+ ```dart title="binary_search_tree.dart"
+ /* 插入节点 */
+ void insert(int _num) {
+ // 若树为空,则初始化根节点
+ if (_root == null) {
+ _root = TreeNode(_num);
+ return;
+ }
+ TreeNode? cur = _root;
+ TreeNode? pre = null;
+ // 循环查找,越过叶节点后跳出
+ while (cur != null) {
+ // 找到重复节点,直接返回
+ if (cur.val == _num) return;
+ pre = cur;
+ // 插入位置在 cur 的右子树中
+ if (cur.val < _num)
+ cur = cur.right;
+ // 插入位置在 cur 的左子树中
+ else
+ cur = cur.left;
+ }
+ // 插入节点
+ TreeNode? node = TreeNode(_num);
+ if (pre!.val < _num)
+ pre.right = node;
+ else
+ pre.left = node;
+ }
+ ```
+
+=== "Rust"
+
+ ```rust title="binary_search_tree.rs"
+ /* 插入节点 */
+ pub fn insert(&mut self, num: i32) {
+ // 若树为空,则初始化根节点
+ if self.root.is_none() {
+ self.root = Some(TreeNode::new(num));
+ return;
+ }
+ let mut cur = self.root.clone();
+ let mut pre = None;
+ // 循环查找,越过叶节点后跳出
+ while let Some(node) = cur.clone() {
+ match num.cmp(&node.borrow().val) {
+ // 找到重复节点,直接返回
+ Ordering::Equal => return,
+ // 插入位置在 cur 的右子树中
+ Ordering::Greater => {
+ pre = cur.clone();
+ cur = node.borrow().right.clone();
+ }
+ // 插入位置在 cur 的左子树中
+ Ordering::Less => {
+ pre = cur.clone();
+ cur = node.borrow().left.clone();
+ }
+ }
+ }
+ // 插入节点
+ let pre = pre.unwrap();
+ let node = Some(TreeNode::new(num));
+ if num > pre.borrow().val {
+ pre.borrow_mut().right = node;
+ } else {
+ pre.borrow_mut().left = node;
+ }
+ }
+ ```
+
+=== "C"
+
+ ```c title="binary_search_tree.c"
+ /* 插入节点 */
+ void insert(BinarySearchTree *bst, int num) {
+ // 若树为空,则初始化根节点
+ if (bst->root == NULL) {
+ bst->root = newTreeNode(num);
+ return;
+ }
+ TreeNode *cur = bst->root, *pre = NULL;
+ // 循环查找,越过叶节点后跳出
+ while (cur != NULL) {
+ // 找到重复节点,直接返回
+ if (cur->val == num) {
+ return;
+ }
+ pre = cur;
+ if (cur->val < num) {
+ // 插入位置在 cur 的右子树中
+ cur = cur->right;
+ } else {
+ // 插入位置在 cur 的左子树中
+ cur = cur->left;
+ }
+ }
+ // 插入节点
+ TreeNode *node = newTreeNode(num);
+ if (pre->val < num) {
+ pre->right = node;
+ } else {
+ pre->left = node;
+ }
+ }
+ ```
+
+=== "Kotlin"
+
+ ```kotlin title="binary_search_tree.kt"
+ /* 插入节点 */
+ fun insert(num: Int) {
+ // 若树为空,则初始化根节点
+ if (root == null) {
+ root = TreeNode(num)
+ return
+ }
+ var cur = root
+ var pre: TreeNode? = null
+ // 循环查找,越过叶节点后跳出
+ while (cur != null) {
+ // 找到重复节点,直接返回
+ if (cur.value == num) return
+ pre = cur
+ // 插入位置在 cur 的右子树中
+ cur = if (cur.value < num) cur.right
+ // 插入位置在 cur 的左子树中
+ else cur.left
+ }
+ // 插入节点
+ val node = TreeNode(num)
+ if (pre?.value!! < num) pre.right = node
+ else pre.left = node
+ }
+ ```
+
+=== "Ruby"
+
+ ```ruby title="binary_search_tree.rb"
+ [class]{BinarySearchTree}-[func]{insert}
+ ```
+
+=== "Zig"
+
+ ```zig title="binary_search_tree.zig"
+ // 插入节点
+ fn insert(self: *Self, num: T) !void {
+ // 若树为空,则初始化根节点
+ if (self.root == null) {
+ self.root = try self.mem_allocator.create(inc.TreeNode(T));
+ return;
+ }
+ var cur = self.root;
+ var pre: ?*inc.TreeNode(T) = null;
+ // 循环查找,越过叶节点后跳出
+ while (cur != null) {
+ // 找到重复节点,直接返回
+ if (cur.?.val == num) return;
+ pre = cur;
+ // 插入位置在 cur 的右子树中
+ if (cur.?.val < num) {
+ cur = cur.?.right;
+ // 插入位置在 cur 的左子树中
+ } else {
+ cur = cur.?.left;
+ }
+ }
+ // 插入节点
+ var node = try self.mem_allocator.create(inc.TreeNode(T));
+ node.init(num);
+ if (pre.?.val < num) {
+ pre.?.right = node;
+ } else {
+ pre.?.left = node;
+ }
+ }
+ ```
+
+??? pythontutor "Code Visualization"
+
+
+
+
+Similar to searching for a node, inserting a node uses $O(\log n)$ time.
+
+### 3. Removing a node
+
+First, find the target node in the binary tree, then remove it. Similar to inserting a node, we need to ensure that after the removal operation is completed, the property of the binary search tree "left subtree < root node < right subtree" is still satisfied. Therefore, based on the number of child nodes of the target node, we divide it into 0, 1, and 2 cases, performing the corresponding node removal operations.
+
+As shown in the Figure 7-19 , when the degree of the node to be removed is $0$, it means the node is a leaf node, and it can be directly removed.
+
+![Removing a node in a binary search tree (degree 0)](binary_search_tree.assets/bst_remove_case1.png){ class="animation-figure" }
+
+ Figure 7-19 Removing a node in a binary search tree (degree 0)
+
+As shown in the Figure 7-20 , when the degree of the node to be removed is $1$, replacing the node to be removed with its child node is sufficient.
+
+![Removing a node in a binary search tree (degree 1)](binary_search_tree.assets/bst_remove_case2.png){ class="animation-figure" }
+
+ Figure 7-20 Removing a node in a binary search tree (degree 1)
+
+When the degree of the node to be removed is $2$, we cannot remove it directly, but need to use a node to replace it. To maintain the property of the binary search tree "left subtree < root node < right subtree," **this node can be either the smallest node of the right subtree or the largest node of the left subtree**.
+
+Assuming we choose the smallest node of the right subtree (the next node in in-order traversal), then the removal operation proceeds as shown in the Figure 7-21 .
+
+1. Find the next node in the "in-order traversal sequence" of the node to be removed, denoted as `tmp`.
+2. Replace the value of the node to be removed with `tmp`'s value, and recursively remove the node `tmp` in the tree.
+
+=== "<1>"
+ ![Removing a node in a binary search tree (degree 2)](binary_search_tree.assets/bst_remove_case3_step1.png){ class="animation-figure" }
+
+=== "<2>"
+ ![bst_remove_case3_step2](binary_search_tree.assets/bst_remove_case3_step2.png){ class="animation-figure" }
+
+=== "<3>"
+ ![bst_remove_case3_step3](binary_search_tree.assets/bst_remove_case3_step3.png){ class="animation-figure" }
+
+=== "<4>"
+ ![bst_remove_case3_step4](binary_search_tree.assets/bst_remove_case3_step4.png){ class="animation-figure" }
+
+ Figure 7-21 Removing a node in a binary search tree (degree 2)
+
+The operation of removing a node also uses $O(\log n)$ time, where finding the node to be removed requires $O(\log n)$ time, and obtaining the in-order traversal successor node requires $O(\log n)$ time. Example code is as follows:
+
+=== "Python"
+
+ ```python title="binary_search_tree.py"
+ def remove(self, num: int):
+ """删除节点"""
+ # 若树为空,直接提前返回
+ if self._root is None:
+ return
+ # 循环查找,越过叶节点后跳出
+ cur, pre = self._root, None
+ while cur is not None:
+ # 找到待删除节点,跳出循环
+ if cur.val == num:
+ break
+ pre = cur
+ # 待删除节点在 cur 的右子树中
+ if cur.val < num:
+ cur = cur.right
+ # 待删除节点在 cur 的左子树中
+ else:
+ cur = cur.left
+ # 若无待删除节点,则直接返回
+ if cur is None:
+ return
+
+ # 子节点数量 = 0 or 1
+ if cur.left is None or cur.right is None:
+ # 当子节点数量 = 0 / 1 时, child = null / 该子节点
+ child = cur.left or cur.right
+ # 删除节点 cur
+ if cur != self._root:
+ if pre.left == cur:
+ pre.left = child
+ else:
+ pre.right = child
+ else:
+ # 若删除节点为根节点,则重新指定根节点
+ self._root = child
+ # 子节点数量 = 2
+ else:
+ # 获取中序遍历中 cur 的下一个节点
+ tmp: TreeNode = cur.right
+ while tmp.left is not None:
+ tmp = tmp.left
+ # 递归删除节点 tmp
+ self.remove(tmp.val)
+ # 用 tmp 覆盖 cur
+ cur.val = tmp.val
+ ```
+
+=== "C++"
+
+ ```cpp title="binary_search_tree.cpp"
+ /* 删除节点 */
+ void remove(int num) {
+ // 若树为空,直接提前返回
+ if (root == nullptr)
+ return;
+ TreeNode *cur = root, *pre = nullptr;
+ // 循环查找,越过叶节点后跳出
+ while (cur != nullptr) {
+ // 找到待删除节点,跳出循环
+ if (cur->val == num)
+ break;
+ pre = cur;
+ // 待删除节点在 cur 的右子树中
+ if (cur->val < num)
+ cur = cur->right;
+ // 待删除节点在 cur 的左子树中
+ else
+ cur = cur->left;
+ }
+ // 若无待删除节点,则直接返回
+ if (cur == nullptr)
+ return;
+ // 子节点数量 = 0 or 1
+ if (cur->left == nullptr || cur->right == nullptr) {
+ // 当子节点数量 = 0 / 1 时, child = nullptr / 该子节点
+ TreeNode *child = cur->left != nullptr ? cur->left : cur->right;
+ // 删除节点 cur
+ if (cur != root) {
+ if (pre->left == cur)
+ pre->left = child;
+ else
+ pre->right = child;
+ } else {
+ // 若删除节点为根节点,则重新指定根节点
+ root = child;
+ }
+ // 释放内存
+ delete cur;
+ }
+ // 子节点数量 = 2
+ else {
+ // 获取中序遍历中 cur 的下一个节点
+ TreeNode *tmp = cur->right;
+ while (tmp->left != nullptr) {
+ tmp = tmp->left;
+ }
+ int tmpVal = tmp->val;
+ // 递归删除节点 tmp
+ remove(tmp->val);
+ // 用 tmp 覆盖 cur
+ cur->val = tmpVal;
+ }
+ }
+ ```
+
+=== "Java"
+
+ ```java title="binary_search_tree.java"
+ /* 删除节点 */
+ void remove(int num) {
+ // 若树为空,直接提前返回
+ if (root == null)
+ return;
+ TreeNode cur = root, pre = null;
+ // 循环查找,越过叶节点后跳出
+ while (cur != null) {
+ // 找到待删除节点,跳出循环
+ if (cur.val == num)
+ break;
+ pre = cur;
+ // 待删除节点在 cur 的右子树中
+ if (cur.val < num)
+ cur = cur.right;
+ // 待删除节点在 cur 的左子树中
+ else
+ cur = cur.left;
+ }
+ // 若无待删除节点,则直接返回
+ if (cur == null)
+ return;
+ // 子节点数量 = 0 or 1
+ if (cur.left == null || cur.right == null) {
+ // 当子节点数量 = 0 / 1 时, child = null / 该子节点
+ TreeNode child = cur.left != null ? cur.left : cur.right;
+ // 删除节点 cur
+ if (cur != root) {
+ if (pre.left == cur)
+ pre.left = child;
+ else
+ pre.right = child;
+ } else {
+ // 若删除节点为根节点,则重新指定根节点
+ root = child;
+ }
+ }
+ // 子节点数量 = 2
+ else {
+ // 获取中序遍历中 cur 的下一个节点
+ TreeNode tmp = cur.right;
+ while (tmp.left != null) {
+ tmp = tmp.left;
+ }
+ // 递归删除节点 tmp
+ remove(tmp.val);
+ // 用 tmp 覆盖 cur
+ cur.val = tmp.val;
+ }
+ }
+ ```
+
+=== "C#"
+
+ ```csharp title="binary_search_tree.cs"
+ /* 删除节点 */
+ void Remove(int num) {
+ // 若树为空,直接提前返回
+ if (root == null)
+ return;
+ TreeNode? cur = root, pre = null;
+ // 循环查找,越过叶节点后跳出
+ while (cur != null) {
+ // 找到待删除节点,跳出循环
+ if (cur.val == num)
+ break;
+ pre = cur;
+ // 待删除节点在 cur 的右子树中
+ if (cur.val < num)
+ cur = cur.right;
+ // 待删除节点在 cur 的左子树中
+ else
+ cur = cur.left;
+ }
+ // 若无待删除节点,则直接返回
+ if (cur == null)
+ return;
+ // 子节点数量 = 0 or 1
+ if (cur.left == null || cur.right == null) {
+ // 当子节点数量 = 0 / 1 时, child = null / 该子节点
+ TreeNode? child = cur.left ?? cur.right;
+ // 删除节点 cur
+ if (cur != root) {
+ if (pre!.left == cur)
+ pre.left = child;
+ else
+ pre.right = child;
+ } else {
+ // 若删除节点为根节点,则重新指定根节点
+ root = child;
+ }
+ }
+ // 子节点数量 = 2
+ else {
+ // 获取中序遍历中 cur 的下一个节点
+ TreeNode? tmp = cur.right;
+ while (tmp.left != null) {
+ tmp = tmp.left;
+ }
+ // 递归删除节点 tmp
+ Remove(tmp.val!.Value);
+ // 用 tmp 覆盖 cur
+ cur.val = tmp.val;
+ }
+ }
+ ```
+
+=== "Go"
+
+ ```go title="binary_search_tree.go"
+ /* 删除节点 */
+ func (bst *binarySearchTree) remove(num int) {
+ cur := bst.root
+ // 若树为空,直接提前返回
+ if cur == nil {
+ return
+ }
+ // 待删除节点之前的节点位置
+ var pre *TreeNode = nil
+ // 循环查找,越过叶节点后跳出
+ for cur != nil {
+ if cur.Val == num {
+ break
+ }
+ pre = cur
+ if cur.Val.(int) < num {
+ // 待删除节点在右子树中
+ cur = cur.Right
+ } else {
+ // 待删除节点在左子树中
+ cur = cur.Left
+ }
+ }
+ // 若无待删除节点,则直接返回
+ if cur == nil {
+ return
+ }
+ // 子节点数为 0 或 1
+ if cur.Left == nil || cur.Right == nil {
+ var child *TreeNode = nil
+ // 取出待删除节点的子节点
+ if cur.Left != nil {
+ child = cur.Left
+ } else {
+ child = cur.Right
+ }
+ // 删除节点 cur
+ if cur != bst.root {
+ if pre.Left == cur {
+ pre.Left = child
+ } else {
+ pre.Right = child
+ }
+ } else {
+ // 若删除节点为根节点,则重新指定根节点
+ bst.root = child
+ }
+ // 子节点数为 2
+ } else {
+ // 获取中序遍历中待删除节点 cur 的下一个节点
+ tmp := cur.Right
+ for tmp.Left != nil {
+ tmp = tmp.Left
+ }
+ // 递归删除节点 tmp
+ bst.remove(tmp.Val.(int))
+ // 用 tmp 覆盖 cur
+ cur.Val = tmp.Val
+ }
+ }
+ ```
+
+=== "Swift"
+
+ ```swift title="binary_search_tree.swift"
+ /* 删除节点 */
+ func remove(num: Int) {
+ // 若树为空,直接提前返回
+ if root == nil {
+ return
+ }
+ var cur = root
+ var pre: TreeNode?
+ // 循环查找,越过叶节点后跳出
+ while cur != nil {
+ // 找到待删除节点,跳出循环
+ if cur!.val == num {
+ break
+ }
+ pre = cur
+ // 待删除节点在 cur 的右子树中
+ if cur!.val < num {
+ cur = cur?.right
+ }
+ // 待删除节点在 cur 的左子树中
+ else {
+ cur = cur?.left
+ }
+ }
+ // 若无待删除节点,则直接返回
+ if cur == nil {
+ return
+ }
+ // 子节点数量 = 0 or 1
+ if cur?.left == nil || cur?.right == nil {
+ // 当子节点数量 = 0 / 1 时, child = null / 该子节点
+ let child = cur?.left ?? cur?.right
+ // 删除节点 cur
+ if cur !== root {
+ if pre?.left === cur {
+ pre?.left = child
+ } else {
+ pre?.right = child
+ }
+ } else {
+ // 若删除节点为根节点,则重新指定根节点
+ root = child
+ }
+ }
+ // 子节点数量 = 2
+ else {
+ // 获取中序遍历中 cur 的下一个节点
+ var tmp = cur?.right
+ while tmp?.left != nil {
+ tmp = tmp?.left
+ }
+ // 递归删除节点 tmp
+ remove(num: tmp!.val)
+ // 用 tmp 覆盖 cur
+ cur?.val = tmp!.val
+ }
+ }
+ ```
+
+=== "JS"
+
+ ```javascript title="binary_search_tree.js"
+ /* 删除节点 */
+ remove(num) {
+ // 若树为空,直接提前返回
+ if (this.root === null) return;
+ let cur = this.root,
+ pre = null;
+ // 循环查找,越过叶节点后跳出
+ while (cur !== null) {
+ // 找到待删除节点,跳出循环
+ if (cur.val === num) break;
+ pre = cur;
+ // 待删除节点在 cur 的右子树中
+ if (cur.val < num) cur = cur.right;
+ // 待删除节点在 cur 的左子树中
+ else cur = cur.left;
+ }
+ // 若无待删除节点,则直接返回
+ if (cur === null) return;
+ // 子节点数量 = 0 or 1
+ if (cur.left === null || cur.right === null) {
+ // 当子节点数量 = 0 / 1 时, child = null / 该子节点
+ const child = cur.left !== null ? cur.left : cur.right;
+ // 删除节点 cur
+ if (cur !== this.root) {
+ if (pre.left === cur) pre.left = child;
+ else pre.right = child;
+ } else {
+ // 若删除节点为根节点,则重新指定根节点
+ this.root = child;
+ }
+ }
+ // 子节点数量 = 2
+ else {
+ // 获取中序遍历中 cur 的下一个节点
+ let tmp = cur.right;
+ while (tmp.left !== null) {
+ tmp = tmp.left;
+ }
+ // 递归删除节点 tmp
+ this.remove(tmp.val);
+ // 用 tmp 覆盖 cur
+ cur.val = tmp.val;
+ }
+ }
+ ```
+
+=== "TS"
+
+ ```typescript title="binary_search_tree.ts"
+ /* 删除节点 */
+ remove(num: number): void {
+ // 若树为空,直接提前返回
+ if (this.root === null) return;
+ let cur: TreeNode | null = this.root,
+ pre: TreeNode | null = null;
+ // 循环查找,越过叶节点后跳出
+ while (cur !== null) {
+ // 找到待删除节点,跳出循环
+ if (cur.val === num) break;
+ pre = cur;
+ // 待删除节点在 cur 的右子树中
+ if (cur.val < num) cur = cur.right;
+ // 待删除节点在 cur 的左子树中
+ else cur = cur.left;
+ }
+ // 若无待删除节点,则直接返回
+ if (cur === null) return;
+ // 子节点数量 = 0 or 1
+ if (cur.left === null || cur.right === null) {
+ // 当子节点数量 = 0 / 1 时, child = null / 该子节点
+ const child: TreeNode | null =
+ cur.left !== null ? cur.left : cur.right;
+ // 删除节点 cur
+ if (cur !== this.root) {
+ if (pre!.left === cur) pre!.left = child;
+ else pre!.right = child;
+ } else {
+ // 若删除节点为根节点,则重新指定根节点
+ this.root = child;
+ }
+ }
+ // 子节点数量 = 2
+ else {
+ // 获取中序遍历中 cur 的下一个节点
+ let tmp: TreeNode | null = cur.right;
+ while (tmp!.left !== null) {
+ tmp = tmp!.left;
+ }
+ // 递归删除节点 tmp
+ this.remove(tmp!.val);
+ // 用 tmp 覆盖 cur
+ cur.val = tmp!.val;
+ }
+ }
+ ```
+
+=== "Dart"
+
+ ```dart title="binary_search_tree.dart"
+ /* 删除节点 */
+ void remove(int _num) {
+ // 若树为空,直接提前返回
+ if (_root == null) return;
+ TreeNode? cur = _root;
+ TreeNode? pre = null;
+ // 循环查找,越过叶节点后跳出
+ while (cur != null) {
+ // 找到待删除节点,跳出循环
+ if (cur.val == _num) break;
+ pre = cur;
+ // 待删除节点在 cur 的右子树中
+ if (cur.val < _num)
+ cur = cur.right;
+ // 待删除节点在 cur 的左子树中
+ else
+ cur = cur.left;
+ }
+ // 若无待删除节点,直接返回
+ if (cur == null) return;
+ // 子节点数量 = 0 or 1
+ if (cur.left == null || cur.right == null) {
+ // 当子节点数量 = 0 / 1 时, child = null / 该子节点
+ TreeNode? child = cur.left ?? cur.right;
+ // 删除节点 cur
+ if (cur != _root) {
+ if (pre!.left == cur)
+ pre.left = child;
+ else
+ pre.right = child;
+ } else {
+ // 若删除节点为根节点,则重新指定根节点
+ _root = child;
+ }
+ } else {
+ // 子节点数量 = 2
+ // 获取中序遍历中 cur 的下一个节点
+ TreeNode? tmp = cur.right;
+ while (tmp!.left != null) {
+ tmp = tmp.left;
+ }
+ // 递归删除节点 tmp
+ remove(tmp.val);
+ // 用 tmp 覆盖 cur
+ cur.val = tmp.val;
+ }
+ }
+ ```
+
+=== "Rust"
+
+ ```rust title="binary_search_tree.rs"
+ /* 删除节点 */
+ pub fn remove(&mut self, num: i32) {
+ // 若树为空,直接提前返回
+ if self.root.is_none() {
+ return;
+ }
+ let mut cur = self.root.clone();
+ let mut pre = None;
+ // 循环查找,越过叶节点后跳出
+ while let Some(node) = cur.clone() {
+ match num.cmp(&node.borrow().val) {
+ // 找到待删除节点,跳出循环
+ Ordering::Equal => break,
+ // 待删除节点在 cur 的右子树中
+ Ordering::Greater => {
+ pre = cur.clone();
+ cur = node.borrow().right.clone();
+ }
+ // 待删除节点在 cur 的左子树中
+ Ordering::Less => {
+ pre = cur.clone();
+ cur = node.borrow().left.clone();
+ }
+ }
+ }
+ // 若无待删除节点,则直接返回
+ if cur.is_none() {
+ return;
+ }
+ let cur = cur.unwrap();
+ let (left_child, right_child) = (cur.borrow().left.clone(), cur.borrow().right.clone());
+ match (left_child.clone(), right_child.clone()) {
+ // 子节点数量 = 0 or 1
+ (None, None) | (Some(_), None) | (None, Some(_)) => {
+ // 当子节点数量 = 0 / 1 时, child = nullptr / 该子节点
+ let child = left_child.or(right_child);
+ let pre = pre.unwrap();
+ // 删除节点 cur
+ if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) {
+ let left = pre.borrow().left.clone();
+ if left.is_some() && Rc::ptr_eq(&left.as_ref().unwrap(), &cur) {
+ pre.borrow_mut().left = child;
+ } else {
+ pre.borrow_mut().right = child;
+ }
+ } else {
+ // 若删除节点为根节点,则重新指定根节点
+ self.root = child;
+ }
+ }
+ // 子节点数量 = 2
+ (Some(_), Some(_)) => {
+ // 获取中序遍历中 cur 的下一个节点
+ let mut tmp = cur.borrow().right.clone();
+ while let Some(node) = tmp.clone() {
+ if node.borrow().left.is_some() {
+ tmp = node.borrow().left.clone();
+ } else {
+ break;
+ }
+ }
+ let tmpval = tmp.unwrap().borrow().val;
+ // 递归删除节点 tmp
+ self.remove(tmpval);
+ // 用 tmp 覆盖 cur
+ cur.borrow_mut().val = tmpval;
+ }
+ }
+ }
+ ```
+
+=== "C"
+
+ ```c title="binary_search_tree.c"
+ /* 删除节点 */
+ // 由于引入了 stdio.h ,此处无法使用 remove 关键词
+ void removeItem(BinarySearchTree *bst, int num) {
+ // 若树为空,直接提前返回
+ if (bst->root == NULL)
+ return;
+ TreeNode *cur = bst->root, *pre = NULL;
+ // 循环查找,越过叶节点后跳出
+ while (cur != NULL) {
+ // 找到待删除节点,跳出循环
+ if (cur->val == num)
+ break;
+ pre = cur;
+ if (cur->val < num) {
+ // 待删除节点在 root 的右子树中
+ cur = cur->right;
+ } else {
+ // 待删除节点在 root 的左子树中
+ cur = cur->left;
+ }
+ }
+ // 若无待删除节点,则直接返回
+ if (cur == NULL)
+ return;
+ // 判断待删除节点是否存在子节点
+ if (cur->left == NULL || cur->right == NULL) {
+ /* 子节点数量 = 0 or 1 */
+ // 当子节点数量 = 0 / 1 时, child = nullptr / 该子节点
+ TreeNode *child = cur->left != NULL ? cur->left : cur->right;
+ // 删除节点 cur
+ if (pre->left == cur) {
+ pre->left = child;
+ } else {
+ pre->right = child;
+ }
+ // 释放内存
+ free(cur);
+ } else {
+ /* 子节点数量 = 2 */
+ // 获取中序遍历中 cur 的下一个节点
+ TreeNode *tmp = cur->right;
+ while (tmp->left != NULL) {
+ tmp = tmp->left;
+ }
+ int tmpVal = tmp->val;
+ // 递归删除节点 tmp
+ removeItem(bst, tmp->val);
+ // 用 tmp 覆盖 cur
+ cur->val = tmpVal;
+ }
+ }
+ ```
+
+=== "Kotlin"
+
+ ```kotlin title="binary_search_tree.kt"
+ /* 删除节点 */
+ fun remove(num: Int) {
+ // 若树为空,直接提前返回
+ if (root == null) return
+ var cur = root
+ var pre: TreeNode? = null
+ // 循环查找,越过叶节点后跳出
+ while (cur != null) {
+ // 找到待删除节点,跳出循环
+ if (cur.value == num) break
+ pre = cur
+ // 待删除节点在 cur 的右子树中
+ cur = if (cur.value < num) cur.right
+ // 待删除节点在 cur 的左子树中
+ else cur.left
+ }
+ // 若无待删除节点,则直接返回
+ if (cur == null) return
+ // 子节点数量 = 0 or 1
+ if (cur.left == null || cur.right == null) {
+ // 当子节点数量 = 0 / 1 时, child = null / 该子节点
+ val child = if (cur.left != null) cur.left else cur.right
+ // 删除节点 cur
+ if (cur != root) {
+ if (pre!!.left == cur) pre.left = child
+ else pre.right = child
+ } else {
+ // 若删除节点为根节点,则重新指定根节点
+ root = child
+ }
+ // 子节点数量 = 2
+ } else {
+ // 获取中序遍历中 cur 的下一个节点
+ var tmp = cur.right
+ while (tmp!!.left != null) {
+ tmp = tmp.left
+ }
+ // 递归删除节点 tmp
+ remove(tmp.value)
+ // 用 tmp 覆盖 cur
+ cur.value = tmp.value
+ }
+ }
+ ```
+
+=== "Ruby"
+
+ ```ruby title="binary_search_tree.rb"
+ [class]{BinarySearchTree}-[func]{remove}
+ ```
+
+=== "Zig"
+
+ ```zig title="binary_search_tree.zig"
+ // 删除节点
+ fn remove(self: *Self, num: T) void {
+ // 若树为空,直接提前返回
+ if (self.root == null) return;
+ var cur = self.root;
+ var pre: ?*inc.TreeNode(T) = null;
+ // 循环查找,越过叶节点后跳出
+ while (cur != null) {
+ // 找到待删除节点,跳出循环
+ if (cur.?.val == num) break;
+ pre = cur;
+ // 待删除节点在 cur 的右子树中
+ if (cur.?.val < num) {
+ cur = cur.?.right;
+ // 待删除节点在 cur 的左子树中
+ } else {
+ cur = cur.?.left;
+ }
+ }
+ // 若无待删除节点,则直接返回
+ if (cur == null) return;
+ // 子节点数量 = 0 or 1
+ if (cur.?.left == null or cur.?.right == null) {
+ // 当子节点数量 = 0 / 1 时, child = null / 该子节点
+ var child = if (cur.?.left != null) cur.?.left else cur.?.right;
+ // 删除节点 cur
+ if (pre.?.left == cur) {
+ pre.?.left = child;
+ } else {
+ pre.?.right = child;
+ }
+ // 子节点数量 = 2
+ } else {
+ // 获取中序遍历中 cur 的下一个节点
+ var tmp = cur.?.right;
+ while (tmp.?.left != null) {
+ tmp = tmp.?.left;
+ }
+ var tmp_val = tmp.?.val;
+ // 递归删除节点 tmp
+ self.remove(tmp.?.val);
+ // 用 tmp 覆盖 cur
+ cur.?.val = tmp_val;
+ }
+ }
+ ```
+
+??? pythontutor "Code Visualization"
+
+
+
+
+### 4. In-order traversal is ordered
+
+As shown in the Figure 7-22 , the in-order traversal of a binary tree follows the "left $\rightarrow$ root $\rightarrow$ right" traversal order, and a binary search tree satisfies the size relationship "left child node < root node < right child node".
+
+This means that in-order traversal in a binary search tree always traverses the next smallest node first, thus deriving an important property: **The in-order traversal sequence of a binary search tree is ascending**.
+
+Using the ascending property of in-order traversal, obtaining ordered data in a binary search tree requires only $O(n)$ time, without the need for additional sorting operations, which is very efficient.
+
+![In-order traversal sequence of a binary search tree](binary_search_tree.assets/bst_inorder_traversal.png){ class="animation-figure" }
+
+ Figure 7-22 In-order traversal sequence of a binary search tree
+
+## 7.4.2 Efficiency of binary search trees
+
+Given a set of data, we consider using an array or a binary search tree for storage. Observing the Table 7-2 , the operations on a binary search tree all have logarithmic time complexity, which is stable and efficient. Only in scenarios of high-frequency addition and low-frequency search and removal, arrays are more efficient than binary search trees.
+
+ Table 7-2 Efficiency comparison between arrays and search trees
+
+
+
+| | Unsorted array | Binary search tree |
+| -------------- | -------------- | ------------------ |
+| Search element | $O(n)$ | $O(\log n)$ |
+| Insert element | $O(1)$ | $O(\log n)$ |
+| Remove element | $O(n)$ | $O(\log n)$ |
+
+
+
+In ideal conditions, the binary search tree is "balanced," thus any node can be found within $\log n$ loops.
+
+However, continuously inserting and removing nodes in a binary search tree may lead to the binary tree degenerating into a chain list as shown in the Figure 7-23 , at which point the time complexity of various operations also degrades to $O(n)$.
+
+![Degradation of a binary search tree](binary_search_tree.assets/bst_degradation.png){ class="animation-figure" }
+
+ Figure 7-23 Degradation of a binary search tree
+
+## 7.4.3 Common applications of binary search trees
+
+- Used as multi-level indexes in systems to implement efficient search, insertion, and removal operations.
+- Serves as the underlying data structure for certain search algorithms.
+- Used to store data streams to maintain their ordered state.
diff --git a/en/docs/chapter_tree/binary_tree.md b/en/docs/chapter_tree/binary_tree.md
new file mode 100644
index 000000000..02abb2d45
--- /dev/null
+++ b/en/docs/chapter_tree/binary_tree.md
@@ -0,0 +1,686 @@
+---
+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.
+
+=== "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
+ ```
+
+=== "C++"
+
+ ```cpp title=""
+ /* Binary tree node */
+ struct TreeNode {
+ int val; // Node value
+ TreeNode *left; // Pointer to left child node
+ TreeNode *right; // Pointer to right child node
+ TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
+ };
+ ```
+
+=== "Java"
+
+ ```java 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(int x) { val = x; }
+ }
+ ```
+
+=== "C#"
+
+ ```csharp title=""
+ /* Binary tree node */
+ 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
+ }
+ ```
+
+=== "Go"
+
+ ```go title=""
+ /* Binary tree node */
+ type TreeNode struct {
+ Val int
+ Left *TreeNode
+ Right *TreeNode
+ }
+ /* 构造方法 */
+ func NewTreeNode(v int) *TreeNode {
+ return &TreeNode{
+ Left: nil, // Pointer to left child node
+ Right: nil, // Pointer to right child node
+ Val: v, // Node value
+ }
+ }
+ ```
+
+=== "Swift"
+
+ ```swift title=""
+ /* Binary tree node */
+ class TreeNode {
+ var val: Int // Node value
+ var left: TreeNode? // Reference to left child node
+ var right: TreeNode? // Reference to right child node
+
+ init(x: Int) {
+ val = x
+ }
+ }
+ ```
+
+=== "JS"
+
+ ```javascript title=""
+ /* Binary tree node */
+ class TreeNode {
+ val; // Node value
+ left; // Pointer to left child node
+ right; // Pointer to right child node
+ constructor(val, left, right) {
+ this.val = val === undefined ? 0 : val;
+ this.left = left === undefined ? null : left;
+ this.right = right === undefined ? null : right;
+ }
+ }
+ ```
+
+=== "TS"
+
+ ```typescript title=""
+ /* Binary tree node */
+ class TreeNode {
+ val: number;
+ left: TreeNode | null;
+ right: TreeNode | null;
+
+ 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
+ }
+ }
+ ```
+
+=== "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>>, // Reference to left child node
+ right: Option>>, // Reference to right child node
+ }
+
+ impl TreeNode {
+ /* 构造方法 */
+ fn new(val: i32) -> Rc> {
+ 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 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."
+
+![Parent Node, child Node, subtree](binary_tree.assets/binary_tree_definition.png){ class="animation-figure" }
+
+ Figure 7-1 Parent Node, child Node, subtree
+
+## 7.1.1 Common terminology of binary trees
+
+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){ class="animation-figure" }
+
+ Figure 7-2 Common Terminology of Binary Trees
+
+!!! 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.
+
+## 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).
+
+=== "Python"
+
+ ```python title="binary_tree.py"
+ # Initializing a binary tree
+ # Initializing 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
+ n1.left = n2
+ n1.right = n3
+ n2.left = n4
+ n2.right = n5
+ ```
+
+=== "C++"
+
+ ```cpp title="binary_tree.cpp"
+ /* Initializing a binary tree */
+ // Initializing 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
+ n1->left = n2;
+ n1->right = n3;
+ n2->left = n4;
+ n2->right = n5;
+ ```
+
+=== "Java"
+
+ ```java title="binary_tree.java"
+ // Initializing 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
+ n1.left = n2;
+ n1.right = n3;
+ n2.left = n4;
+ n2.right = n5;
+ ```
+
+=== "C#"
+
+ ```csharp title="binary_tree.cs"
+ /* Initializing a binary tree */
+ // Initializing 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
+ n1.left = n2;
+ n1.right = n3;
+ n2.left = n4;
+ n2.right = n5;
+ ```
+
+=== "Go"
+
+ ```go title="binary_tree.go"
+ /* Initializing a binary tree */
+ // Initializing nodes
+ n1 := NewTreeNode(1)
+ n2 := NewTreeNode(2)
+ n3 := NewTreeNode(3)
+ n4 := NewTreeNode(4)
+ n5 := NewTreeNode(5)
+ // Linking references (pointers) between nodes
+ n1.Left = n2
+ n1.Right = n3
+ n2.Left = n4
+ n2.Right = n5
+ ```
+
+=== "Swift"
+
+ ```swift title="binary_tree.swift"
+ // Initializing 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
+ n1.left = n2
+ n1.right = n3
+ n2.left = n4
+ n2.right = n5
+ ```
+
+=== "JS"
+
+ ```javascript title="binary_tree.js"
+ /* Initializing a binary tree */
+ // Initializing 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
+ n1.left = n2;
+ n1.right = n3;
+ n2.left = n4;
+ n2.right = n5;
+ ```
+
+=== "TS"
+
+ ```typescript title="binary_tree.ts"
+ /* Initializing a binary tree */
+ // Initializing 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
+ n1.left = n2;
+ n1.right = n3;
+ n2.left = n4;
+ n2.right = n5;
+ ```
+
+=== "Dart"
+
+ ```dart title="binary_tree.dart"
+ /* Initializing a binary tree */
+ // Initializing 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
+ n1.left = n2;
+ n1.right = n3;
+ n2.left = n4;
+ n2.right = n5;
+ ```
+
+=== "Rust"
+
+ ```rust title="binary_tree.rs"
+ // Initializing 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
+ n1.borrow_mut().left = Some(n2.clone());
+ n1.borrow_mut().right = Some(n3);
+ n2.borrow_mut().left = Some(n4);
+ n2.borrow_mut().right = Some(n5);
+ ```
+
+=== "C"
+
+ ```c title="binary_tree.c"
+ /* Initializing a binary tree */
+ // Initializing 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
+ n1->left = n2;
+ n1->right = n3;
+ n2->left = n4;
+ n2->right = n5;
+ ```
+
+=== "Kotlin"
+
+ ```kotlin title="binary_tree.kt"
+ // Initializing 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
+ n1.left = n2
+ n1.right = n3
+ n2.left = n4
+ n2.right = n5
+ ```
+
+=== "Ruby"
+
+ ```ruby title="binary_tree.rb"
+
+ ```
+
+=== "Zig"
+
+ ```zig title="binary_tree.zig"
+
+ ```
+
+??? pythontutor "Code visualization"
+
+ 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
+
+### 2. Inserting and removing nodes
+
+Similar to a linked list, inserting and removing nodes in a binary tree can be achieved by modifying pointers. The Figure 7-3 provides an example.
+
+![Inserting and removing nodes in a binary tree](binary_tree.assets/binary_tree_add_remove.png){ class="animation-figure" }
+
+ Figure 7-3 Inserting and removing nodes in a binary tree
+
+=== "Python"
+
+ ```python title="binary_tree.py"
+ # Inserting and removing nodes
+ p = TreeNode(0)
+ # Inserting node P between n1 -> n2
+ n1.left = p
+ p.left = n2
+ # Removing node P
+ n1.left = n2
+ ```
+
+=== "C++"
+
+ ```cpp title="binary_tree.cpp"
+ /* Inserting and removing nodes */
+ TreeNode* P = new TreeNode(0);
+ // Inserting node P between n1 and n2
+ n1->left = P;
+ P->left = n2;
+ // Removing node P
+ n1->left = n2;
+ ```
+
+=== "Java"
+
+ ```java title="binary_tree.java"
+ TreeNode P = new TreeNode(0);
+ // Inserting node P between n1 and n2
+ n1.left = P;
+ P.left = n2;
+ // Removing node P
+ n1.left = n2;
+ ```
+
+=== "C#"
+
+ ```csharp title="binary_tree.cs"
+ /* Inserting and removing nodes */
+ TreeNode P = new(0);
+ // Inserting node P between n1 and n2
+ n1.left = P;
+ P.left = n2;
+ // Removing node P
+ n1.left = n2;
+ ```
+
+=== "Go"
+
+ ```go title="binary_tree.go"
+ /* Inserting and removing nodes */
+ // Inserting node P between n1 and n2
+ p := NewTreeNode(0)
+ n1.Left = p
+ p.Left = n2
+ // Removing node P
+ n1.Left = n2
+ ```
+
+=== "Swift"
+
+ ```swift title="binary_tree.swift"
+ let P = TreeNode(x: 0)
+ // Inserting node P between n1 and n2
+ n1.left = P
+ P.left = n2
+ // Removing node P
+ n1.left = n2
+ ```
+
+=== "JS"
+
+ ```javascript title="binary_tree.js"
+ /* Inserting and removing nodes */
+ let P = new TreeNode(0);
+ // Inserting node P between n1 and n2
+ n1.left = P;
+ P.left = n2;
+ // Removing node P
+ n1.left = n2;
+ ```
+
+=== "TS"
+
+ ```typescript title="binary_tree.ts"
+ /* Inserting and removing nodes */
+ const P = new TreeNode(0);
+ // Inserting node P between n1 and n2
+ n1.left = P;
+ P.left = n2;
+ // Removing node P
+ n1.left = n2;
+ ```
+
+=== "Dart"
+
+ ```dart title="binary_tree.dart"
+ /* Inserting and removing nodes */
+ TreeNode P = new TreeNode(0);
+ // Inserting node P between n1 and n2
+ n1.left = P;
+ P.left = n2;
+ // Removing node P
+ n1.left = n2;
+ ```
+
+=== "Rust"
+
+ ```rust title="binary_tree.rs"
+ let p = TreeNode::new(0);
+ // Inserting node P between n1 and n2
+ n1.borrow_mut().left = Some(p.clone());
+ p.borrow_mut().left = Some(n2.clone());
+ // Removing node P
+ n1.borrow_mut().left = Some(n2);
+ ```
+
+=== "C"
+
+ ```c title="binary_tree.c"
+ /* Inserting and removing nodes */
+ TreeNode *P = newTreeNode(0);
+ // Inserting node P between n1 and n2
+ n1->left = P;
+ P->left = n2;
+ // Removing node P
+ n1->left = n2;
+ ```
+
+=== "Kotlin"
+
+ ```kotlin title="binary_tree.kt"
+ val P = TreeNode(0)
+ // Inserting node P between n1 and n2
+ n1.left = P
+ P.left = n2
+ // Removing node P
+ n1.left = n2
+ ```
+
+=== "Ruby"
+
+ ```ruby title="binary_tree.rb"
+
+ ```
+
+=== "Zig"
+
+ ```zig title="binary_tree.zig"
+
+ ```
+
+??? pythontutor "Code visualization"
+
+ 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
+
+!!! note
+
+ 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.
+
+## 7.1.3 Common types of binary trees
+
+### 1. Perfect binary tree
+
+As shown in the 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.
+
+!!! tip
+
+ Please note that in the Chinese community, a perfect binary tree is often referred to as a "full binary tree."
+
+![Perfect binary tree](binary_tree.assets/perfect_binary_tree.png){ class="animation-figure" }
+
+ Figure 7-4 Perfect binary tree
+
+### 2. Complete binary tree
+
+As shown in the 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.
+
+![Complete binary tree](binary_tree.assets/complete_binary_tree.png){ class="animation-figure" }
+
+ Figure 7-5 Complete binary tree
+
+### 3. Full binary tree
+
+As shown in the Figure 7-6 , a "full binary tree" has all nodes except leaf nodes having two children.
+
+![Full binary tree](binary_tree.assets/full_binary_tree.png){ class="animation-figure" }
+
+ Figure 7-6 Full binary tree
+
+### 4. Balanced binary tree
+
+As shown in the 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.
+
+![Balanced binary tree](binary_tree.assets/balanced_binary_tree.png){ class="animation-figure" }
+
+ Figure 7-7 Balanced binary tree
+
+## 7.1.4 Degeneration of binary trees
+
+The 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".
+
+- 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){ class="animation-figure" }
+
+ Figure 7-8 The Best and Worst Structures of Binary Trees
+
+As shown in the Table 7-1 , 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.
+
+ Table 7-1 The Best and Worst Structures of Binary Trees
+
+
+
+| | 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$ |
+
+
diff --git a/en/docs/chapter_tree/binary_tree_traversal.md b/en/docs/chapter_tree/binary_tree_traversal.md
new file mode 100755
index 000000000..011002271
--- /dev/null
+++ b/en/docs/chapter_tree/binary_tree_traversal.md
@@ -0,0 +1,883 @@
+---
+comments: true
+---
+
+# 7.2 Binary tree traversal
+
+From the perspective of physical structure, a tree is a data structure based on linked lists, hence its traversal method involves accessing nodes one by one through pointers. However, a tree is a non-linear data structure, which makes traversing a tree more complex than traversing a linked list, requiring the assistance of search algorithms to achieve.
+
+Common traversal methods for binary trees include level-order traversal, preorder traversal, inorder traversal, and postorder traversal, among others.
+
+## 7.2.1 Level-order traversal
+
+As shown in the Figure 7-9 , "level-order traversal" traverses the binary tree from top to bottom, layer by layer, and accesses nodes in each layer in a left-to-right order.
+
+Level-order traversal essentially belongs to "breadth-first traversal", also known as "breadth-first search (BFS)", which embodies a "circumferentially outward expanding" layer-by-layer traversal method.
+
+![Level-order traversal of a binary tree](binary_tree_traversal.assets/binary_tree_bfs.png){ class="animation-figure" }
+
+ Figure 7-9 Level-order traversal of a binary tree
+
+### 1. Code implementation
+
+Breadth-first traversal is usually implemented with the help of a "queue". The queue follows the "first in, first out" rule, while breadth-first traversal follows the "layer-by-layer progression" rule, the underlying ideas of the two are consistent. The implementation code is as follows:
+
+=== "Python"
+
+ ```python title="binary_tree_bfs.py"
+ def level_order(root: TreeNode | None) -> list[int]:
+ """层序遍历"""
+ # 初始化队列,加入根节点
+ queue: deque[TreeNode] = deque()
+ queue.append(root)
+ # 初始化一个列表,用于保存遍历序列
+ res = []
+ while queue:
+ node: TreeNode = queue.popleft() # 队列出队
+ res.append(node.val) # 保存节点值
+ if node.left is not None:
+ queue.append(node.left) # 左子节点入队
+ if node.right is not None:
+ queue.append(node.right) # 右子节点入队
+ return res
+ ```
+
+=== "C++"
+
+ ```cpp title="binary_tree_bfs.cpp"
+ /* 层序遍历 */
+ vector levelOrder(TreeNode *root) {
+ // 初始化队列,加入根节点
+ queue queue;
+ queue.push(root);
+ // 初始化一个列表,用于保存遍历序列
+ vector vec;
+ while (!queue.empty()) {
+ TreeNode *node = queue.front();
+ queue.pop(); // 队列出队
+ vec.push_back(node->val); // 保存节点值
+ if (node->left != nullptr)
+ queue.push(node->left); // 左子节点入队
+ if (node->right != nullptr)
+ queue.push(node->right); // 右子节点入队
+ }
+ return vec;
+ }
+ ```
+
+=== "Java"
+
+ ```java title="binary_tree_bfs.java"
+ /* 层序遍历 */
+ List levelOrder(TreeNode root) {
+ // 初始化队列,加入根节点
+ Queue queue = new LinkedList<>();
+ queue.add(root);
+ // 初始化一个列表,用于保存遍历序列
+ List list = new ArrayList<>();
+ while (!queue.isEmpty()) {
+ TreeNode node = queue.poll(); // 队列出队
+ list.add(node.val); // 保存节点值
+ if (node.left != null)
+ queue.offer(node.left); // 左子节点入队
+ if (node.right != null)
+ queue.offer(node.right); // 右子节点入队
+ }
+ return list;
+ }
+ ```
+
+=== "C#"
+
+ ```csharp title="binary_tree_bfs.cs"
+ /* 层序遍历 */
+ List LevelOrder(TreeNode root) {
+ // 初始化队列,加入根节点
+ Queue queue = new();
+ queue.Enqueue(root);
+ // 初始化一个列表,用于保存遍历序列
+ List list = [];
+ while (queue.Count != 0) {
+ TreeNode node = queue.Dequeue(); // 队列出队
+ list.Add(node.val!.Value); // 保存节点值
+ if (node.left != null)
+ queue.Enqueue(node.left); // 左子节点入队
+ if (node.right != null)
+ queue.Enqueue(node.right); // 右子节点入队
+ }
+ return list;
+ }
+ ```
+
+=== "Go"
+
+ ```go title="binary_tree_bfs.go"
+ /* 层序遍历 */
+ func levelOrder(root *TreeNode) []any {
+ // 初始化队列,加入根节点
+ queue := list.New()
+ queue.PushBack(root)
+ // 初始化一个切片,用于保存遍历序列
+ nums := make([]any, 0)
+ for queue.Len() > 0 {
+ // 队列出队
+ node := queue.Remove(queue.Front()).(*TreeNode)
+ // 保存节点值
+ nums = append(nums, node.Val)
+ if node.Left != nil {
+ // 左子节点入队
+ queue.PushBack(node.Left)
+ }
+ if node.Right != nil {
+ // 右子节点入队
+ queue.PushBack(node.Right)
+ }
+ }
+ return nums
+ }
+ ```
+
+=== "Swift"
+
+ ```swift title="binary_tree_bfs.swift"
+ /* 层序遍历 */
+ func levelOrder(root: TreeNode) -> [Int] {
+ // 初始化队列,加入根节点
+ var queue: [TreeNode] = [root]
+ // 初始化一个列表,用于保存遍历序列
+ var list: [Int] = []
+ while !queue.isEmpty {
+ let node = queue.removeFirst() // 队列出队
+ list.append(node.val) // 保存节点值
+ if let left = node.left {
+ queue.append(left) // 左子节点入队
+ }
+ if let right = node.right {
+ queue.append(right) // 右子节点入队
+ }
+ }
+ return list
+ }
+ ```
+
+=== "JS"
+
+ ```javascript title="binary_tree_bfs.js"
+ /* 层序遍历 */
+ function levelOrder(root) {
+ // 初始化队列,加入根节点
+ const queue = [root];
+ // 初始化一个列表,用于保存遍历序列
+ const list = [];
+ while (queue.length) {
+ let node = queue.shift(); // 队列出队
+ list.push(node.val); // 保存节点值
+ if (node.left) queue.push(node.left); // 左子节点入队
+ if (node.right) queue.push(node.right); // 右子节点入队
+ }
+ return list;
+ }
+ ```
+
+=== "TS"
+
+ ```typescript title="binary_tree_bfs.ts"
+ /* 层序遍历 */
+ function levelOrder(root: TreeNode | null): number[] {
+ // 初始化队列,加入根节点
+ const queue = [root];
+ // 初始化一个列表,用于保存遍历序列
+ const list: number[] = [];
+ while (queue.length) {
+ let node = queue.shift() as TreeNode; // 队列出队
+ list.push(node.val); // 保存节点值
+ if (node.left) {
+ queue.push(node.left); // 左子节点入队
+ }
+ if (node.right) {
+ queue.push(node.right); // 右子节点入队
+ }
+ }
+ return list;
+ }
+ ```
+
+=== "Dart"
+
+ ```dart title="binary_tree_bfs.dart"
+ /* 层序遍历 */
+ List levelOrder(TreeNode? root) {
+ // 初始化队列,加入根节点
+ Queue queue = Queue();
+ queue.add(root);
+ // 初始化一个列表,用于保存遍历序列
+ List res = [];
+ while (queue.isNotEmpty) {
+ TreeNode? node = queue.removeFirst(); // 队列出队
+ res.add(node!.val); // 保存节点值
+ if (node.left != null) queue.add(node.left); // 左子节点入队
+ if (node.right != null) queue.add(node.right); // 右子节点入队
+ }
+ return res;
+ }
+ ```
+
+=== "Rust"
+
+ ```rust title="binary_tree_bfs.rs"
+ /* 层序遍历 */
+ fn level_order(root: &Rc>) -> Vec {
+ // 初始化队列,加入根节点
+ let mut que = VecDeque::new();
+ que.push_back(Rc::clone(&root));
+ // 初始化一个列表,用于保存遍历序列
+ let mut vec = Vec::new();
+
+ while let Some(node) = que.pop_front() {
+ // 队列出队
+ vec.push(node.borrow().val); // 保存节点值
+ if let Some(left) = node.borrow().left.as_ref() {
+ que.push_back(Rc::clone(left)); // 左子节点入队
+ }
+ if let Some(right) = node.borrow().right.as_ref() {
+ que.push_back(Rc::clone(right)); // 右子节点入队
+ };
+ }
+ vec
+ }
+ ```
+
+=== "C"
+
+ ```c title="binary_tree_bfs.c"
+ /* 层序遍历 */
+ int *levelOrder(TreeNode *root, int *size) {
+ /* 辅助队列 */
+ int front, rear;
+ int index, *arr;
+ TreeNode *node;
+ TreeNode **queue;
+
+ /* 辅助队列 */
+ queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_SIZE);
+ // 队列指针
+ front = 0, rear = 0;
+ // 加入根节点
+ queue[rear++] = root;
+ // 初始化一个列表,用于保存遍历序列
+ /* 辅助数组 */
+ arr = (int *)malloc(sizeof(int) * MAX_SIZE);
+ // 数组指针
+ index = 0;
+ while (front < rear) {
+ // 队列出队
+ node = queue[front++];
+ // 保存节点值
+ arr[index++] = node->val;
+ if (node->left != NULL) {
+ // 左子节点入队
+ queue[rear++] = node->left;
+ }
+ if (node->right != NULL) {
+ // 右子节点入队
+ queue[rear++] = node->right;
+ }
+ }
+ // 更新数组长度的值
+ *size = index;
+ arr = realloc(arr, sizeof(int) * (*size));
+
+ // 释放辅助数组空间
+ free(queue);
+ return arr;
+ }
+ ```
+
+=== "Kotlin"
+
+ ```kotlin title="binary_tree_bfs.kt"
+ /* 层序遍历 */
+ fun levelOrder(root: TreeNode?): MutableList {
+ // 初始化队列,加入根节点
+ val queue = LinkedList()
+ queue.add(root)
+ // 初始化一个列表,用于保存遍历序列
+ val list = ArrayList()
+ while (!queue.isEmpty()) {
+ val node = queue.poll() // 队列出队
+ list.add(node?.value!!) // 保存节点值
+ if (node.left != null) queue.offer(node.left) // 左子节点入队
+
+ if (node.right != null) queue.offer(node.right) // 右子节点入队
+ }
+ return list
+ }
+ ```
+
+=== "Ruby"
+
+ ```ruby title="binary_tree_bfs.rb"
+ [class]{}-[func]{level_order}
+ ```
+
+=== "Zig"
+
+ ```zig title="binary_tree_bfs.zig"
+ // 层序遍历
+ fn levelOrder(comptime T: type, mem_allocator: std.mem.Allocator, root: *inc.TreeNode(T)) !std.ArrayList(T) {
+ // 初始化队列,加入根节点
+ const L = std.TailQueue(*inc.TreeNode(T));
+ var queue = L{};
+ var root_node = try mem_allocator.create(L.Node);
+ root_node.data = root;
+ queue.append(root_node);
+ // 初始化一个列表,用于保存遍历序列
+ var list = std.ArrayList(T).init(std.heap.page_allocator);
+ while (queue.len > 0) {
+ var queue_node = queue.popFirst().?; // 队列出队
+ var node = queue_node.data;
+ try list.append(node.val); // 保存节点值
+ if (node.left != null) {
+ var tmp_node = try mem_allocator.create(L.Node);
+ tmp_node.data = node.left.?;
+ queue.append(tmp_node); // 左子节点入队
+ }
+ if (node.right != null) {
+ var tmp_node = try mem_allocator.create(L.Node);
+ tmp_node.data = node.right.?;
+ queue.append(tmp_node); // 右子节点入队
+ }
+ }
+ return list;
+ }
+ ```
+
+??? pythontutor "Code Visualization"
+
+
+
+
+### 2. Complexity analysis
+
+- **Time complexity is $O(n)$**: All nodes are visited once, using $O(n)$ time, where $n$ is the number of nodes.
+- **Space complexity is $O(n)$**: In the worst case, i.e., a full binary tree, before traversing to the lowest level, the queue can contain at most $(n + 1) / 2$ nodes at the same time, occupying $O(n)$ space.
+
+## 7.2.2 Preorder, inorder, and postorder traversal
+
+Correspondingly, preorder, inorder, and postorder traversal all belong to "depth-first traversal", also known as "depth-first search (DFS)", which embodies a "proceed to the end first, then backtrack and continue" traversal method.
+
+The Figure 7-10 shows the working principle of performing a depth-first traversal on a binary tree. **Depth-first traversal is like walking around the perimeter of the entire binary tree**, encountering three positions at each node, corresponding to preorder traversal, inorder traversal, and postorder traversal.
+
+![Preorder, inorder, and postorder traversal of a binary search tree](binary_tree_traversal.assets/binary_tree_dfs.png){ class="animation-figure" }
+
+ Figure 7-10 Preorder, inorder, and postorder traversal of a binary search tree
+
+### 1. Code implementation
+
+Depth-first search is usually implemented based on recursion:
+
+=== "Python"
+
+ ```python title="binary_tree_dfs.py"
+ def pre_order(root: TreeNode | None):
+ """前序遍历"""
+ if root is None:
+ return
+ # 访问优先级:根节点 -> 左子树 -> 右子树
+ res.append(root.val)
+ pre_order(root=root.left)
+ pre_order(root=root.right)
+
+ def in_order(root: TreeNode | None):
+ """中序遍历"""
+ if root is None:
+ return
+ # 访问优先级:左子树 -> 根节点 -> 右子树
+ in_order(root=root.left)
+ res.append(root.val)
+ in_order(root=root.right)
+
+ def post_order(root: TreeNode | None):
+ """后序遍历"""
+ if root is None:
+ return
+ # 访问优先级:左子树 -> 右子树 -> 根节点
+ post_order(root=root.left)
+ post_order(root=root.right)
+ res.append(root.val)
+ ```
+
+=== "C++"
+
+ ```cpp title="binary_tree_dfs.cpp"
+ /* 前序遍历 */
+ void preOrder(TreeNode *root) {
+ if (root == nullptr)
+ return;
+ // 访问优先级:根节点 -> 左子树 -> 右子树
+ vec.push_back(root->val);
+ preOrder(root->left);
+ preOrder(root->right);
+ }
+
+ /* 中序遍历 */
+ void inOrder(TreeNode *root) {
+ if (root == nullptr)
+ return;
+ // 访问优先级:左子树 -> 根节点 -> 右子树
+ inOrder(root->left);
+ vec.push_back(root->val);
+ inOrder(root->right);
+ }
+
+ /* 后序遍历 */
+ void postOrder(TreeNode *root) {
+ if (root == nullptr)
+ return;
+ // 访问优先级:左子树 -> 右子树 -> 根节点
+ postOrder(root->left);
+ postOrder(root->right);
+ vec.push_back(root->val);
+ }
+ ```
+
+=== "Java"
+
+ ```java title="binary_tree_dfs.java"
+ /* 前序遍历 */
+ void preOrder(TreeNode root) {
+ if (root == null)
+ return;
+ // 访问优先级:根节点 -> 左子树 -> 右子树
+ list.add(root.val);
+ preOrder(root.left);
+ preOrder(root.right);
+ }
+
+ /* 中序遍历 */
+ void inOrder(TreeNode root) {
+ if (root == null)
+ return;
+ // 访问优先级:左子树 -> 根节点 -> 右子树
+ inOrder(root.left);
+ list.add(root.val);
+ inOrder(root.right);
+ }
+
+ /* 后序遍历 */
+ void postOrder(TreeNode root) {
+ if (root == null)
+ return;
+ // 访问优先级:左子树 -> 右子树 -> 根节点
+ postOrder(root.left);
+ postOrder(root.right);
+ list.add(root.val);
+ }
+ ```
+
+=== "C#"
+
+ ```csharp title="binary_tree_dfs.cs"
+ /* 前序遍历 */
+ void PreOrder(TreeNode? root) {
+ if (root == null) return;
+ // 访问优先级:根节点 -> 左子树 -> 右子树
+ list.Add(root.val!.Value);
+ PreOrder(root.left);
+ PreOrder(root.right);
+ }
+
+ /* 中序遍历 */
+ void InOrder(TreeNode? root) {
+ if (root == null) return;
+ // 访问优先级:左子树 -> 根节点 -> 右子树
+ InOrder(root.left);
+ list.Add(root.val!.Value);
+ InOrder(root.right);
+ }
+
+ /* 后序遍历 */
+ void PostOrder(TreeNode? root) {
+ if (root == null) return;
+ // 访问优先级:左子树 -> 右子树 -> 根节点
+ PostOrder(root.left);
+ PostOrder(root.right);
+ list.Add(root.val!.Value);
+ }
+ ```
+
+=== "Go"
+
+ ```go title="binary_tree_dfs.go"
+ /* 前序遍历 */
+ func preOrder(node *TreeNode) {
+ if node == nil {
+ return
+ }
+ // 访问优先级:根节点 -> 左子树 -> 右子树
+ nums = append(nums, node.Val)
+ preOrder(node.Left)
+ preOrder(node.Right)
+ }
+
+ /* 中序遍历 */
+ func inOrder(node *TreeNode) {
+ if node == nil {
+ return
+ }
+ // 访问优先级:左子树 -> 根节点 -> 右子树
+ inOrder(node.Left)
+ nums = append(nums, node.Val)
+ inOrder(node.Right)
+ }
+
+ /* 后序遍历 */
+ func postOrder(node *TreeNode) {
+ if node == nil {
+ return
+ }
+ // 访问优先级:左子树 -> 右子树 -> 根节点
+ postOrder(node.Left)
+ postOrder(node.Right)
+ nums = append(nums, node.Val)
+ }
+ ```
+
+=== "Swift"
+
+ ```swift title="binary_tree_dfs.swift"
+ /* 前序遍历 */
+ func preOrder(root: TreeNode?) {
+ guard let root = root else {
+ return
+ }
+ // 访问优先级:根节点 -> 左子树 -> 右子树
+ list.append(root.val)
+ preOrder(root: root.left)
+ preOrder(root: root.right)
+ }
+
+ /* 中序遍历 */
+ func inOrder(root: TreeNode?) {
+ guard let root = root else {
+ return
+ }
+ // 访问优先级:左子树 -> 根节点 -> 右子树
+ inOrder(root: root.left)
+ list.append(root.val)
+ inOrder(root: root.right)
+ }
+
+ /* 后序遍历 */
+ func postOrder(root: TreeNode?) {
+ guard let root = root else {
+ return
+ }
+ // 访问优先级:左子树 -> 右子树 -> 根节点
+ postOrder(root: root.left)
+ postOrder(root: root.right)
+ list.append(root.val)
+ }
+ ```
+
+=== "JS"
+
+ ```javascript title="binary_tree_dfs.js"
+ /* 前序遍历 */
+ function preOrder(root) {
+ if (root === null) return;
+ // 访问优先级:根节点 -> 左子树 -> 右子树
+ list.push(root.val);
+ preOrder(root.left);
+ preOrder(root.right);
+ }
+
+ /* 中序遍历 */
+ function inOrder(root) {
+ if (root === null) return;
+ // 访问优先级:左子树 -> 根节点 -> 右子树
+ inOrder(root.left);
+ list.push(root.val);
+ inOrder(root.right);
+ }
+
+ /* 后序遍历 */
+ function postOrder(root) {
+ if (root === null) return;
+ // 访问优先级:左子树 -> 右子树 -> 根节点
+ postOrder(root.left);
+ postOrder(root.right);
+ list.push(root.val);
+ }
+ ```
+
+=== "TS"
+
+ ```typescript title="binary_tree_dfs.ts"
+ /* 前序遍历 */
+ function preOrder(root: TreeNode | null): void {
+ if (root === null) {
+ return;
+ }
+ // 访问优先级:根节点 -> 左子树 -> 右子树
+ list.push(root.val);
+ preOrder(root.left);
+ preOrder(root.right);
+ }
+
+ /* 中序遍历 */
+ function inOrder(root: TreeNode | null): void {
+ if (root === null) {
+ return;
+ }
+ // 访问优先级:左子树 -> 根节点 -> 右子树
+ inOrder(root.left);
+ list.push(root.val);
+ inOrder(root.right);
+ }
+
+ /* 后序遍历 */
+ function postOrder(root: TreeNode | null): void {
+ if (root === null) {
+ return;
+ }
+ // 访问优先级:左子树 -> 右子树 -> 根节点
+ postOrder(root.left);
+ postOrder(root.right);
+ list.push(root.val);
+ }
+ ```
+
+=== "Dart"
+
+ ```dart title="binary_tree_dfs.dart"
+ /* 前序遍历 */
+ void preOrder(TreeNode? node) {
+ if (node == null) return;
+ // 访问优先级:根节点 -> 左子树 -> 右子树
+ list.add(node.val);
+ preOrder(node.left);
+ preOrder(node.right);
+ }
+
+ /* 中序遍历 */
+ void inOrder(TreeNode? node) {
+ if (node == null) return;
+ // 访问优先级:左子树 -> 根节点 -> 右子树
+ inOrder(node.left);
+ list.add(node.val);
+ inOrder(node.right);
+ }
+
+ /* 后序遍历 */
+ void postOrder(TreeNode? node) {
+ if (node == null) return;
+ // 访问优先级:左子树 -> 右子树 -> 根节点
+ postOrder(node.left);
+ postOrder(node.right);
+ list.add(node.val);
+ }
+ ```
+
+=== "Rust"
+
+ ```rust title="binary_tree_dfs.rs"
+ /* 前序遍历 */
+ fn pre_order(root: Option<&Rc>>) -> Vec {
+ let mut result = vec![];
+
+ if let Some(node) = root {
+ // 访问优先级:根节点 -> 左子树 -> 右子树
+ result.push(node.borrow().val);
+ result.append(&mut pre_order(node.borrow().left.as_ref()));
+ result.append(&mut pre_order(node.borrow().right.as_ref()));
+ }
+ result
+ }
+
+ /* 中序遍历 */
+ fn in_order(root: Option<&Rc>>) -> Vec {
+ let mut result = vec![];
+
+ if let Some(node) = root {
+ // 访问优先级:左子树 -> 根节点 -> 右子树
+ result.append(&mut in_order(node.borrow().left.as_ref()));
+ result.push(node.borrow().val);
+ result.append(&mut in_order(node.borrow().right.as_ref()));
+ }
+ result
+ }
+
+ /* 后序遍历 */
+ fn post_order(root: Option<&Rc>>) -> Vec {
+ let mut result = vec![];
+
+ if let Some(node) = root {
+ // 访问优先级:左子树 -> 右子树 -> 根节点
+ result.append(&mut post_order(node.borrow().left.as_ref()));
+ result.append(&mut post_order(node.borrow().right.as_ref()));
+ result.push(node.borrow().val);
+ }
+ result
+ }
+ ```
+
+=== "C"
+
+ ```c title="binary_tree_dfs.c"
+ /* 前序遍历 */
+ void preOrder(TreeNode *root, int *size) {
+ if (root == NULL)
+ return;
+ // 访问优先级:根节点 -> 左子树 -> 右子树
+ arr[(*size)++] = root->val;
+ preOrder(root->left, size);
+ preOrder(root->right, size);
+ }
+
+ /* 中序遍历 */
+ void inOrder(TreeNode *root, int *size) {
+ if (root == NULL)
+ return;
+ // 访问优先级:左子树 -> 根节点 -> 右子树
+ inOrder(root->left, size);
+ arr[(*size)++] = root->val;
+ inOrder(root->right, size);
+ }
+
+ /* 后序遍历 */
+ void postOrder(TreeNode *root, int *size) {
+ if (root == NULL)
+ return;
+ // 访问优先级:左子树 -> 右子树 -> 根节点
+ postOrder(root->left, size);
+ postOrder(root->right, size);
+ arr[(*size)++] = root->val;
+ }
+ ```
+
+=== "Kotlin"
+
+ ```kotlin title="binary_tree_dfs.kt"
+ /* 前序遍历 */
+ fun preOrder(root: TreeNode?) {
+ if (root == null) return
+ // 访问优先级:根节点 -> 左子树 -> 右子树
+ list.add(root.value)
+ preOrder(root.left)
+ preOrder(root.right)
+ }
+
+ /* 中序遍历 */
+ fun inOrder(root: TreeNode?) {
+ if (root == null) return
+ // 访问优先级:左子树 -> 根节点 -> 右子树
+ inOrder(root.left)
+ list.add(root.value)
+ inOrder(root.right)
+ }
+
+ /* 后序遍历 */
+ fun postOrder(root: TreeNode?) {
+ if (root == null) return
+ // 访问优先级:左子树 -> 右子树 -> 根节点
+ postOrder(root.left)
+ postOrder(root.right)
+ list.add(root.value)
+ }
+ ```
+
+=== "Ruby"
+
+ ```ruby title="binary_tree_dfs.rb"
+ [class]{}-[func]{pre_order}
+
+ [class]{}-[func]{in_order}
+
+ [class]{}-[func]{post_order}
+ ```
+
+=== "Zig"
+
+ ```zig title="binary_tree_dfs.zig"
+ // 前序遍历
+ fn preOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {
+ if (root == null) return;
+ // 访问优先级:根节点 -> 左子树 -> 右子树
+ try list.append(root.?.val);
+ try preOrder(T, root.?.left);
+ try preOrder(T, root.?.right);
+ }
+
+ // 中序遍历
+ fn inOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {
+ if (root == null) return;
+ // 访问优先级:左子树 -> 根节点 -> 右子树
+ try inOrder(T, root.?.left);
+ try list.append(root.?.val);
+ try inOrder(T, root.?.right);
+ }
+
+ // 后序遍历
+ fn postOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {
+ if (root == null) return;
+ // 访问优先级:左子树 -> 右子树 -> 根节点
+ try postOrder(T, root.?.left);
+ try postOrder(T, root.?.right);
+ try list.append(root.?.val);
+ }
+ ```
+
+??? pythontutor "Code Visualization"
+
+
+
+
+!!! tip
+
+ Depth-first search can also be implemented based on iteration, interested readers can study this on their own.
+
+The Figure 7-11 shows the recursive process of preorder traversal of a binary tree, which can be divided into two opposite parts: "recursion" and "return".
+
+1. "Recursion" means starting a new method, the program accesses the next node in this process.
+2. "Return" means the function returns, indicating the current node has been fully accessed.
+
+=== "<1>"
+ ![The recursive process of preorder traversal](binary_tree_traversal.assets/preorder_step1.png){ class="animation-figure" }
+
+=== "<2>"
+ ![preorder_step2](binary_tree_traversal.assets/preorder_step2.png){ class="animation-figure" }
+
+=== "<3>"
+ ![preorder_step3](binary_tree_traversal.assets/preorder_step3.png){ class="animation-figure" }
+
+=== "<4>"
+ ![preorder_step4](binary_tree_traversal.assets/preorder_step4.png){ class="animation-figure" }
+
+=== "<5>"
+ ![preorder_step5](binary_tree_traversal.assets/preorder_step5.png){ class="animation-figure" }
+
+=== "<6>"
+ ![preorder_step6](binary_tree_traversal.assets/preorder_step6.png){ class="animation-figure" }
+
+=== "<7>"
+ ![preorder_step7](binary_tree_traversal.assets/preorder_step7.png){ class="animation-figure" }
+
+=== "<8>"
+ ![preorder_step8](binary_tree_traversal.assets/preorder_step8.png){ class="animation-figure" }
+
+=== "<9>"
+ ![preorder_step9](binary_tree_traversal.assets/preorder_step9.png){ class="animation-figure" }
+
+=== "<10>"
+ ![preorder_step10](binary_tree_traversal.assets/preorder_step10.png){ class="animation-figure" }
+
+=== "<11>"
+ ![preorder_step11](binary_tree_traversal.assets/preorder_step11.png){ class="animation-figure" }
+
+ Figure 7-11 The recursive process of preorder traversal
+
+### 2. Complexity analysis
+
+- **Time complexity is $O(n)$**: All nodes are visited once, using $O(n)$ time.
+- **Space complexity is $O(n)$**: In the worst case, i.e., the tree degrades into a linked list, the recursion depth reaches $n$, the system occupies $O(n)$ stack frame space.
diff --git a/en/docs/chapter_tree/index.md b/en/docs/chapter_tree/index.md
new file mode 100644
index 000000000..a5d1da56c
--- /dev/null
+++ b/en/docs/chapter_tree/index.md
@@ -0,0 +1,23 @@
+---
+comments: true
+icon: material/graph-outline
+---
+
+# Chapter 7. Tree
+
+![Tree](../assets/covers/chapter_tree.jpg){ class="cover-image" }
+
+!!! abstract
+
+ The towering tree, full of vitality with its roots deep and leaves lush, branches spreading wide.
+
+ It vividly demonstrates the form of data divide-and-conquer.
+
+## Chapter Contents
+
+- [7.1 Binary Tree](https://www.hello-algo.com/en/chapter_tree/binary_tree/)
+- [7.2 Binary Tree Traversal](https://www.hello-algo.com/en/chapter_tree/binary_tree_traversal/)
+- [7.3 Array Representation of Tree](https://www.hello-algo.com/en/chapter_tree/array_representation_of_tree/)
+- [7.4 Binary Search Tree](https://www.hello-algo.com/en/chapter_tree/binary_search_tree/)
+- [7.5 AVL Tree *](https://www.hello-algo.com/en/chapter_tree/avl_tree/)
+- [7.6 Summary](https://www.hello-algo.com/en/chapter_tree/summary/)
diff --git a/en/docs/chapter_tree/summary.md b/en/docs/chapter_tree/summary.md
new file mode 100644
index 000000000..a7cd0a41a
--- /dev/null
+++ b/en/docs/chapter_tree/summary.md
@@ -0,0 +1,58 @@
+---
+comments: true
+---
+
+# 7.6 Summary
+
+### 1. Key review
+
+- A binary tree is a non-linear data structure that reflects the "divide and conquer" logic of splitting one into two. Each binary tree node contains a value and two pointers, which point to its left and right child nodes, respectively.
+- For a node in a binary tree, the tree formed by its left (right) child node and all nodes under it is called the node's left (right) subtree.
+- Related terminology of binary trees includes root node, leaf node, level, degree, edge, height, and depth, among others.
+- The operations of initializing a binary tree, inserting nodes, and removing nodes are similar to those of linked list operations.
+- Common types of binary trees include perfect binary trees, complete binary trees, full binary trees, and balanced binary trees. The perfect binary tree represents the ideal state, while the linked list is the worst state after degradation.
+- A binary tree can be represented using an array by arranging the node values and empty slots in a level-order traversal sequence and implementing pointers based on the index mapping relationship between parent nodes and child nodes.
+- The level-order traversal of a binary tree is a breadth-first search method, which reflects a layer-by-layer traversal manner of "expanding circle by circle." It is usually implemented using a queue.
+- Pre-order, in-order, and post-order traversals are all depth-first search methods, reflecting the traversal manner of "going to the end first, then backtracking to continue." They are usually implemented using recursion.
+- A binary search tree is an efficient data structure for element searching, with the time complexity of search, insert, and remove operations all being $O(\log n)$. When a binary search tree degrades into a linked list, these time complexities deteriorate to $O(n)$.
+- An AVL tree, also known as a balanced binary search tree, ensures that the tree remains balanced after continuous node insertions and removals through rotation operations.
+- Rotation operations in an AVL tree include right rotation, left rotation, right-then-left rotation, and left-then-right rotation. After inserting or removing nodes, an AVL tree performs rotation operations from bottom to top to rebalance the tree.
+
+### 2. Q & A
+
+**Q**: For a binary tree with only one node, are both the height of the tree and the depth of the root node $0$?
+
+Yes, because height and depth are typically defined as "the number of edges passed."
+
+**Q**: The insertion and removal in a binary tree are generally completed by a set of operations. What does "a set of operations" refer to here? Can it be understood as the release of resources of the child nodes?
+
+Taking the binary search tree as an example, the operation of removing a node needs to be handled in three different scenarios, each requiring multiple steps of node operations.
+
+**Q**: Why are there three sequences: pre-order, in-order, and post-order for DFS traversal of a binary tree, and what are their uses?
+
+Similar to sequential and reverse traversal of arrays, pre-order, in-order, and post-order traversals are three methods of traversing a binary tree, allowing us to obtain a traversal result in a specific order. For example, in a binary search tree, since the node sizes satisfy `left child node value < root node value < right child node value`, we can obtain an ordered node sequence by traversing the tree in the "left → root → right" priority.
+
+**Q**: In a right rotation operation that deals with the relationship between the imbalance nodes `node`, `child`, `grand_child`, isn't the connection between `node` and its parent node and the original link of `node` lost after the right rotation?
+
+We need to view this problem from a recursive perspective. The `right_rotate(root)` operation passes the root node of the subtree and eventually returns the root node of the rotated subtree with `return child`. The connection between the subtree's root node and its parent node is established after this function returns, which is outside the scope of the right rotation operation's maintenance.
+
+**Q**: In C++, functions are divided into `private` and `public` sections. What considerations are there for this? Why are the `height()` function and the `updateHeight()` function placed in `public` and `private`, respectively?
+
+It depends on the scope of the method's use. If a method is only used within the class, then it is designed to be `private`. For example, it makes no sense for users to call `updateHeight()` on their own, as it is just a step in the insertion or removal operations. However, `height()` is for accessing node height, similar to `vector.size()`, thus it is set to `public` for use.
+
+**Q**: How do you build a binary search tree from a set of input data? Is the choice of root node very important?
+
+Yes, the method for building the tree is provided in the `build_tree()` method in the binary search tree code. As for the choice of the root node, we usually sort the input data and then select the middle element as the root node, recursively building the left and right subtrees. This approach maximizes the balance of the tree.
+
+**Q**: In Java, do you always have to use the `equals()` method for string comparison?
+
+In Java, for primitive data types, `==` is used to compare whether the values of two variables are equal. For reference types, the working principles of the two symbols are different.
+
+- `==`: Used to compare whether two variables point to the same object, i.e., whether their positions in memory are the same.
+- `equals()`: Used to compare whether the values of two objects are equal.
+
+Therefore, to compare values, we should use `equals()`. However, strings initialized with `String a = "hi"; String b = "hi";` are stored in the string constant pool and point to the same object, so `a == b` can also be used to compare the contents of two strings.
+
+**Q**: Before reaching the bottom level, is the number of nodes in the queue $2^h$ in breadth-first traversal?
+
+Yes, for example, a full binary tree with height $h = 2$ has a total of $n = 7$ nodes, then the bottom level has $4 = 2^h = (n + 1) / 2$ nodes.