en/docs/chapter_tree/binary_search_tree.md
As shown in the figure below, a <u>binary search tree</u> satisfies the following conditions.
1. as well.We encapsulate the binary search tree as a class BinarySearchTree and declare a member variable root pointing to the tree's root node.
Given a target node value num, we can search according to the properties of the binary search tree. As shown in the figure below, we declare a node cur and start from the binary tree's root node root, looping to compare the node value cur.val with num.
cur.val < num, it means the target node is in cur's right subtree, thus execute cur = cur.right.cur.val > num, it means the target node is in cur's left subtree, thus execute cur = cur.left.cur.val = num, it means the target node is found, exit the loop, and return the node.=== "<1>"
=== "<2>"
=== "<3>"
=== "<4>"
The search operation in a binary search tree works on the same principle as the binary search algorithm, both eliminating half of the cases in each round. The number of loop iterations is at most the height of the binary tree. When the binary tree is balanced, it uses $O(\log n)$ time. The example code is as follows:
[file]{binary_search_tree}-[class]{binary_search_tree}-[func]{search}
Given an element num to be inserted, in order to maintain the property of the binary search tree "left subtree < root node < right subtree," the insertion process is as shown in the figure below.
num, until passing the leaf node (traversing to None) and then exit the loop.num and place it at the None position.In the code implementation, note the following two points:
pre to save the node from the previous loop iteration. This way, when traversing to None, we can obtain its parent node, thereby completing the node insertion operation.[file]{binary_search_tree}-[class]{binary_search_tree}-[func]{insert}
Similar to searching for a node, inserting a node uses $O(\log n)$ time.
First, find the target node in the binary tree, then remove it. Similar to node insertion, we need to ensure that after the removal operation is completed, the binary search tree's property of "left subtree $<$ root node $<$ right subtree" is still maintained. Therefore, depending on the number of child nodes the target node has, we divide it into 0, 1, and 2 three cases, and execute the corresponding node removal operations.
As shown in the figure below, when the degree of the node to be removed is $0$, it means the node is a leaf node and can be directly removed.
As shown in the figure below, when the degree of the node to be removed is $1$, replacing the node to be removed with its child node is sufficient.
When the degree of the node to be removed is $2$, we cannot directly remove it; instead, we need to use a node to replace it. To maintain the binary search tree's property of "left subtree $<$ root node $<$ right subtree," this node can be either the smallest node in the right subtree or the largest node in the left subtree.
Assuming we choose the smallest node in the right subtree (the next node in the inorder traversal), the removal process is as shown in the figure below.
tmp.tmp, and recursively remove node tmp in the tree.=== "<1>"
=== "<2>"
=== "<3>"
=== "<4>"
The node removal operation also uses $O(\log n)$ time, where finding the node to be removed requires $O(\log n)$ time, and obtaining the inorder successor node requires $O(\log n)$ time. Example code is as follows:
[file]{binary_search_tree}-[class]{binary_search_tree}-[func]{remove}
As shown in the figure below, the inorder traversal of a binary tree follows the "left $\rightarrow$ root $\rightarrow$ right" traversal order, while the binary search tree satisfies the "left child node $<$ root node $<$ right child node" size relationship.
This means that when performing an inorder traversal in a binary search tree, the next smallest node is always traversed first, thus yielding an important property: The inorder traversal sequence of a binary search tree is ascending.
Using the property of inorder traversal being ascending, we can obtain ordered data in a binary search tree in only $O(n)$ time, without the need for additional sorting operations, which is very efficient.
Given a set of data, we consider using an array or a binary search tree for storage. Observing the table below, all operations in a binary search tree have logarithmic time complexity, providing stable and efficient performance. Arrays are more efficient than binary search trees only in scenarios with high-frequency additions and low-frequency searches and deletions.
<p align="center"> Table <id> Efficiency comparison between arrays and search trees </p>| 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 the ideal case, a binary search tree is "balanced," such that any node can be found within $\log n$ loop iterations.
However, if we continuously insert and remove nodes in a binary search tree, it may degenerate into a linked list as shown in the figure below, where the time complexity of various operations also degrades to $O(n)$.