Skip to content

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

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.

Example of searching for a node in a binary search tree

bst_search_step2

bst_search_step3

bst_search_step4

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:

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
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;
}
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;
}
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;
}
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
}
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
}
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;
}
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;
}
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;
}
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
}
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;
}
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
}
binary_search_tree.rb
[class]{BinarySearchTree}-[func]{search}
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;
}
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

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.
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
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;
}
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;
}
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;
    }
}
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
    }
}
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
    }
}
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;
}
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;
}
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;
}
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;
    }
}
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;
    }
}
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
}
binary_search_tree.rb
[class]{BinarySearchTree}-[func]{insert}
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;
    }
}
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)

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)

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.

Removing a node in a binary search tree (degree 2)

bst_remove_case3_step2

bst_remove_case3_step3

bst_remove_case3_step4

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:

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
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;
    }
}
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;
    }
}
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;
    }
}
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
    }
}
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
    }
}
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;
    }
}
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;
    }
}
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;
  }
}
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;
        }
    }
}
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;
    }
}
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
    }
}
binary_search_tree.rb
[class]{BinarySearchTree}-[func]{remove}
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;
    }
}
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

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

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.
Feel free to drop your insights, questions or suggestions