7.4 Binary search trees¶
A binary search tree satisfies the following conditions, as shown below.
- Every value in the left subtree is \(<\) the root value, which is \(<\) every value in the right subtree.
- The left and right subtrees of every node are also binary search trees and satisfy condition
1..
Figure 7-16 A binary search tree
Description of Figure 7-16: Binary search tree containing 1 through 15
At every node in a binary search tree, all values in its left subtree are smaller and all values in its right subtree are larger than the node’s value.
The tree has four levels and 15 nodes with values 1 through 15. Each directed edge points from parent to child. Root 8 on the first level has left child 4 and right child 12.
On level two, 4 has left and right children 2 and 6, and 12 has children 10 and 14. Level three connects to the 8 leaves on level four. From left to right the children are 1 and 3 under 2, 5 and 7 under 6, 9 and 11 under 10, and 13 and 15 under 14.
The same ordering holds recursively. At root 8 all values 1 through 7 on the left are smaller than 8, and all values 9 through 15 on the right are larger. At node 4, for example, the left subtree contains 1 through 3, all below 4. Its right subtree contains 5 through 7, all above 4 yet below root 8.
7.4.1 Binary search tree operations¶
We encapsulate the tree in a BinarySearchTree class with a member variable root pointing to its root node.
1. Finding a node¶
To find a target value num, use the tree's ordering property. As shown below, initialize a node reference cur at root and repeatedly compare cur.val with num.
- If
cur.val < num, the target lies in the right subtree, so setcur = cur.right. - If
cur.val > num, the target lies in the left subtree, so setcur = cur.left. - If
cur.val = num, the target has been found; exit the loop and return the node.
Figure 7-17 Finding a node in a binary search tree
Description of Figure 7-17: Search a binary search tree for node 7
Search for 7 among values 1 through 15, comparing at each node and choosing the left or right branch.
At any node in a binary search tree all values on the left are smaller and all on the right are larger than its value. Arrows lead from parent to child.
This perfect binary search tree has four levels with values 1 through 15. Root 8 has children 4 and 12, in left-to-right order. The children of 4 are 2 and 6, and of 12 are 10 and 14. The children of 2 are 1 and 3; of 6, 5 and 7; of 10, 9 and 11; and of 14, 13 and 15.
The pointer cur marks the node being checked, filled in blue. Blue arrows and blue rings around previously visited nodes show the search path.
Start at root 8
Initialize cur at root 8, filled in blue. Compare target 7 with current value 8. Since 7 < 8, the target lies in the left subtree. Next move down to left child 4. Path so far: 8.
Go left to node 4
Since 7 < 8, execute cur = cur.left, following the blue arrow from 8 to left child 4. Compare target 7 with current value 4: since 7 > 4, search next in the right subtree of 4. Path: 8→4.
Go right to node 6
Since 7 > 4, execute cur = cur.right, following the blue arrow from 4 to right child 6. Compare target 7 with current value 6: since 7 > 6, continue to the right child of 6. Path: 8→4→6.
Find node 7
Since 7 > 6, execute cur = cur.right and follow the blue arrow from 6 to right child 7. The value at cur matches target 7, so the loop exits and returns node 7. Path: 8→4→6→7.
Pointer cur compares four nodes along 8 → 4 → 6 → 7. At step four the target value 7 matches, and the search returns that node. No tree links change.
Searching a binary search tree follows the same principle as binary search: each round eliminates half the possibilities. The number of iterations is at most the tree's height, giving \(O(\log n)\) time when the tree is balanced. An implementation follows:
def search(self, num: int) -> TreeNode | None:
"""Find a node"""
cur = self._root
# Search in a loop, stopping if cur becomes None
while cur is not None:
# If present, the target node is in the right subtree of cur
if cur.val < num:
cur = cur.right
# If present, the target node is in the left subtree of cur
elif cur.val > num:
cur = cur.left
# The target node was found; exit the loop
else:
break
return cur
/* Find a node */
TreeNode *search(int num) {
TreeNode *cur = root;
// Continue searching while the current node pointer is not null
while (cur != nullptr) {
// The target node lies in the right subtree of cur
if (cur->val < num)
cur = cur->right;
// The target node lies in the left subtree of cur
else if (cur->val > num)
cur = cur->left;
// The target node was found; exit the loop
else
break;
}
// Return the target node, or nullptr if it was not found
return cur;
}
/* Find a node */
TreeNode search(int num) {
TreeNode cur = root;
// Search in a loop until reaching a null reference beyond a leaf
while (cur != null) {
// The target node lies in the right subtree of cur
if (cur.val < num)
cur = cur.right;
// The target node lies in the left subtree of cur
else if (cur.val > num)
cur = cur.left;
// The target node was found; exit the loop
else
break;
}
// Return the target node
return cur;
}
/* Find a node */
TreeNode? Search(int num) {
TreeNode? cur = root;
// Search in a loop, stopping if a null reference is reached
while (cur != null) {
// The target node lies in the right subtree of cur
if (cur.val < num) cur =
cur.right;
// The target node lies in the left subtree of cur
else if (cur.val > num)
cur = cur.left;
// The target node was found; exit the loop
else
break;
}
// Return the target node
return cur;
}
/* Find a node */
func (bst *binarySearchTree) search(num int) *TreeNode {
node := bst.root
// Continue searching while the current node is not nil
for node != nil {
if node.Val.(int) < num {
// If the target node exists, it lies in the right subtree of node
node = node.Right
} else if node.Val.(int) > num {
// If the target node exists, it lies in the left subtree of node
node = node.Left
} else {
// The target node was found; exit the loop
break
}
}
// Return the target node, or nil if it was not found
return node
}
/* Find a node */
func search(num: Int) -> TreeNode? {
var cur = root
// Search in a loop, stopping if cur becomes nil
while cur != nil {
// If present, the target node lies in the right subtree of cur
if cur!.val < num {
cur = cur?.right
}
// If present, the target node lies in the left subtree of cur
else if cur!.val > num {
cur = cur?.left
}
// The target node was found; exit the loop
else {
break
}
}
// Return the target node, or nil if it was not found
return cur
}
/* Find a node */
search(num) {
let cur = this.root;
// Search in a loop until reaching a null reference beyond a leaf
while (cur !== null) {
// The target node lies in the right subtree of cur
if (cur.val < num) cur = cur.right;
// The target node lies in the left subtree of cur
else if (cur.val > num) cur = cur.left;
// The target node was found; exit the loop
else break;
}
// Return the target node
return cur;
}
/* Find a node */
search(num: number): TreeNode | null {
let cur = this.root;
// Search in a loop, stopping if a null reference is reached
while (cur !== null) {
// The target node lies in the right subtree of cur
if (cur.val < num) cur = cur.right;
// The target node lies in the left subtree of cur
else if (cur.val > num) cur = cur.left;
// The target node was found; exit the loop
else break;
}
// Return the target node
return cur;
}
/* Find a node */
TreeNode? search(int _num) {
TreeNode? cur = _root;
// Search in a loop, stopping if a null reference is reached
while (cur != null) {
// The target node lies in the right subtree of cur
if (cur.val < _num)
cur = cur.right;
// The target node lies in the left subtree of cur
else if (cur.val > _num)
cur = cur.left;
// The target node was found; exit the loop
else
break;
}
// Return the target node
return cur;
}
/* Find a node */
pub fn search(&self, num: i32) -> OptionTreeNodeRc {
let mut cur = self.root.clone();
// Search while cur refers to a node
while let Some(node) = cur.clone() {
match num.cmp(&node.borrow().val) {
// The target node lies in the right subtree of cur
Ordering::Greater => cur = node.borrow().right.clone(),
// The target node lies in the left subtree of cur
Ordering::Less => cur = node.borrow().left.clone(),
// The target node was found; exit the loop
Ordering::Equal => break,
}
}
// Return the matching node, or None if no match was found
cur
}
/* Find a node */
TreeNode *search(BinarySearchTree *bst, int num) {
TreeNode *cur = bst->root;
// Search in a loop until reaching a null reference beyond a leaf
while (cur != NULL) {
if (cur->val < num) {
// The target node lies in the right subtree of cur
cur = cur->right;
} else if (cur->val > num) {
// The target node lies in the left subtree of cur
cur = cur->left;
} else {
// The target node was found; exit the loop
break;
}
}
// Return the target node
return cur;
}
/* Find a node */
fun search(num: Int): TreeNode? {
var cur = root
// Continue searching while cur is not null
while (cur != null) {
// The target node lies in the right subtree of cur
cur = if (cur._val < num)
cur.right
// The target node lies in the left subtree of cur
else if (cur._val > num)
cur.left
// The target node was found; exit the loop
else
break
}
// Return the target node
return cur
}
### Find a node ###
def search(num)
cur = @root
# Search in a loop, stopping if cur becomes nil
while !cur.nil?
# The target node lies in the right subtree of cur
if cur.val < num
cur = cur.right
# The target node lies in the left subtree of cur
elsif cur.val > num
cur = cur.left
# The target node was found; exit the loop
else
break
end
end
cur
end
Visualize execution
2. Inserting a node¶
To insert num while preserving the ordering “left subtree < root < right subtree,” follow the process below.
- Find the insertion position: as in a search, start at the root and move down according to comparisons with
num. Stop when the traversal passes a leaf and reachesNone. - Insert at that position: create a node containing
numand place it at theNoneposition.
Figure 7-18 Inserting a node into a binary search tree
Description of Figure 7-18: Insert a node into a binary search tree
Compare values while descending until an empty link is found. Use the saved parent pointer pre to attach the new leaf.
The binary search trees in this book have no duplicate values. At every node, all values in its left subtree are smaller and all values in its right subtree are larger.
Insert 16 into a tree rooted at 8 containing values 1 through 15. Two panels show the tree before and after insertion. The table places their links side by side. “Empty” means there is no child, and “Not yet inserted” means the node did not yet exist.
| Node | Left child before insertion | Right child before insertion | Left child after insertion | Right child after insertion |
|---|---|---|---|---|
| 1 | Empty | Empty | Empty | Empty |
| 2 | 1 | 3 | 1 | 3 |
| 3 | Empty | Empty | Empty | Empty |
| 4 | 2 | 6 | 2 | 6 |
| 5 | Empty | Empty | Empty | Empty |
| 6 | 5 | 7 | 5 | 7 |
| 7 | Empty | Empty | Empty | Empty |
| 8 | 4 | 12 | 4 | 12 |
| 9 | Empty | Empty | Empty | Empty |
| 10 | 9 | 11 | 9 | 11 |
| 11 | Empty | Empty | Empty | Empty |
| 12 | 10 | 14 | 10 | 14 |
| 13 | Empty | Empty | Empty | Empty |
| 14 | 13 | 15 | 13 | 15 |
| 15 | Empty | Empty | Empty | 16 |
| 16 | Not yet inserted | Not yet inserted | Empty | Empty |
16 is greater than 8, 12, 14, and 15 in turn. Follow right links through those four nodes until cur reaches None. The last nonempty node 15 is saved in pre. Create node 16 and set pre.right to the new node, making it the right child of 15.
After insertion the tree contains 1 through 16 and preserves search tree order. Once cur becomes None it no longer identifies the parent. The pointer pre retains the last nonempty node.
Keep two details in mind when implementing this.
- Duplicate values are not allowed under this definition. If the value already exists, return without inserting it.
- Keep a reference
preto the node from the previous iteration. When the traversal reachesNone,pregives its parent so that the new node can be attached.
def insert(self, num: int):
"""Insert a node"""
# If the tree is empty, initialize the root node
if self._root is None:
self._root = TreeNode(num)
return
# Search in a loop, stopping if cur becomes None
cur, pre = self._root, None
while cur is not None:
# Return immediately if a duplicate node is found
if cur.val == num:
return
pre = cur
# The insertion position lies in the right subtree of cur
if cur.val < num:
cur = cur.right
# The insertion position lies in the left subtree of cur
else:
cur = cur.left
# Insert a node
node = TreeNode(num)
if pre.val < num:
pre.right = node
else:
pre.left = node
/* Insert a node */
void insert(int num) {
// If the tree is empty, initialize the root node
if (root == nullptr) {
root = new TreeNode(num);
return;
}
TreeNode *cur = root, *pre = nullptr;
// Continue searching while the current node pointer is not null
while (cur != nullptr) {
// Return immediately if a duplicate node is found
if (cur->val == num)
return;
pre = cur;
// The insertion position lies in the right subtree of cur
if (cur->val < num)
cur = cur->right;
// The insertion position lies in the left subtree of cur
else
cur = cur->left;
}
// Insert a node
TreeNode *node = new TreeNode(num);
if (pre->val < num)
pre->right = node;
else
pre->left = node;
}
/* Insert a node */
void insert(int num) {
// If the tree is empty, initialize the root node
if (root == null) {
root = new TreeNode(num);
return;
}
TreeNode cur = root, pre = null;
// Search in a loop until reaching a null reference beyond a leaf
while (cur != null) {
// Return immediately if a duplicate node is found
if (cur.val == num)
return;
pre = cur;
// The insertion position lies in the right subtree of cur
if (cur.val < num)
cur = cur.right;
// The insertion position lies in the left subtree of cur
else
cur = cur.left;
}
// Insert a node
TreeNode node = new TreeNode(num);
if (pre.val < num)
pre.right = node;
else
pre.left = node;
}
/* Insert a node */
void Insert(int num) {
// If the tree is empty, initialize the root node
if (root == null) {
root = new TreeNode(num);
return;
}
TreeNode? cur = root, pre = null;
// Search in a loop, stopping if a null reference is reached
while (cur != null) {
// Return immediately if a duplicate node is found
if (cur.val == num)
return;
pre = cur;
// The insertion position lies in the right subtree of cur
if (cur.val < num)
cur = cur.right;
// The insertion position lies in the left subtree of cur
else
cur = cur.left;
}
// Insert a node
TreeNode node = new(num);
if (pre != null) {
if (pre.val < num)
pre.right = node;
else
pre.left = node;
}
}
/* Insert a node */
func (bst *binarySearchTree) insert(num int) {
cur := bst.root
// If the tree is empty, initialize the root node
if cur == nil {
bst.root = NewTreeNode(num)
return
}
// Track the parent of the node to insert
var pre *TreeNode = nil
// Continue searching while the current node is not nil
for cur != nil {
if cur.Val == num {
return
}
pre = cur
if cur.Val.(int) < num {
cur = cur.Right
} else {
cur = cur.Left
}
}
// Insert a node
node := NewTreeNode(num)
if pre.Val.(int) < num {
pre.Right = node
} else {
pre.Left = node
}
}
/* Insert a node */
func insert(num: Int) {
// If the tree is empty, initialize the root node
if root == nil {
root = TreeNode(x: num)
return
}
var cur = root
var pre: TreeNode?
// Search in a loop, stopping if cur becomes nil
while cur != nil {
// Return immediately if a duplicate node is found
if cur!.val == num {
return
}
pre = cur
// The insertion position lies in the right subtree of cur
if cur!.val < num {
cur = cur?.right
}
// The insertion position lies in the left subtree of cur
else {
cur = cur?.left
}
}
// Insert a node
let node = TreeNode(x: num)
if pre!.val < num {
pre?.right = node
} else {
pre?.left = node
}
}
/* Insert a node */
insert(num) {
// If the tree is empty, initialize the root node
if (this.root === null) {
this.root = new TreeNode(num);
return;
}
let cur = this.root,
pre = null;
// Search in a loop until reaching a null reference beyond a leaf
while (cur !== null) {
// Return immediately if a duplicate node is found
if (cur.val === num) return;
pre = cur;
// The insertion position lies in the right subtree of cur
if (cur.val < num) cur = cur.right;
// The insertion position lies in the left subtree of cur
else cur = cur.left;
}
// Insert a node
const node = new TreeNode(num);
if (pre.val < num) pre.right = node;
else pre.left = node;
}
/* Insert a node */
insert(num: number): void {
// If the tree is empty, initialize the root node
if (this.root === null) {
this.root = new TreeNode(num);
return;
}
let cur: TreeNode | null = this.root,
pre: TreeNode | null = null;
// Search in a loop, stopping if a null reference is reached
while (cur !== null) {
// Return immediately if a duplicate node is found
if (cur.val === num) return;
pre = cur;
// The insertion position lies in the right subtree of cur
if (cur.val < num) cur = cur.right;
// The insertion position lies in the left subtree of cur
else cur = cur.left;
}
// Insert a node
const node = new TreeNode(num);
if (pre!.val < num) pre!.right = node;
else pre!.left = node;
}
/* Insert a node */
void insert(int _num) {
// If the tree is empty, initialize the root node
if (_root == null) {
_root = TreeNode(_num);
return;
}
TreeNode? cur = _root;
TreeNode? pre = null;
// Search in a loop, stopping if a null reference is reached
while (cur != null) {
// Return immediately if a duplicate node is found
if (cur.val == _num) return;
pre = cur;
// The insertion position lies in the right subtree of cur
if (cur.val < _num)
cur = cur.right;
// The insertion position lies in the left subtree of cur
else
cur = cur.left;
}
// Insert a node
TreeNode? node = TreeNode(_num);
if (pre!.val < _num)
pre.right = node;
else
pre.left = node;
}
/* Insert a node */
pub fn insert(&mut self, num: i32) {
// If the tree is empty, initialize the root node
if self.root.is_none() {
self.root = Some(TreeNode::new(num));
return;
}
let mut cur = self.root.clone();
let mut pre = None;
// Search while cur refers to a node
while let Some(node) = cur.clone() {
match num.cmp(&node.borrow().val) {
// Return immediately if a duplicate node is found
Ordering::Equal => return,
// The insertion position lies in the right subtree of cur
Ordering::Greater => {
pre = cur.clone();
cur = node.borrow().right.clone();
}
// The insertion position lies in the left subtree of cur
Ordering::Less => {
pre = cur.clone();
cur = node.borrow().left.clone();
}
}
}
// Insert a node
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;
}
}
/* Insert a node */
void insert(BinarySearchTree *bst, int num) {
// If the tree is empty, initialize the root node
if (bst->root == NULL) {
bst->root = newTreeNode(num);
return;
}
TreeNode *cur = bst->root, *pre = NULL;
// Search in a loop until reaching a null reference beyond a leaf
while (cur != NULL) {
// Return immediately if a duplicate node is found
if (cur->val == num) {
return;
}
pre = cur;
if (cur->val < num) {
// The insertion position lies in the right subtree of cur
cur = cur->right;
} else {
// The insertion position lies in the left subtree of cur
cur = cur->left;
}
}
// Insert a node
TreeNode *node = newTreeNode(num);
if (pre->val < num) {
pre->right = node;
} else {
pre->left = node;
}
}
/* Insert a node */
fun insert(num: Int) {
// If the tree is empty, initialize the root node
if (root == null) {
root = TreeNode(num)
return
}
var cur = root
var pre: TreeNode? = null
// Continue searching while cur is not null
while (cur != null) {
// Return immediately if a duplicate node is found
if (cur._val == num)
return
pre = cur
// The insertion position lies in the right subtree of cur
cur = if (cur._val < num)
cur.right
// The insertion position lies in the left subtree of cur
else
cur.left
}
// Insert a node
val node = TreeNode(num)
if (pre?._val!! < num)
pre.right = node
else
pre.left = node
}
### Insert a node ###
def insert(num)
# If the tree is empty, initialize the root node
if @root.nil?
@root = TreeNode.new(num)
return
end
# Search in a loop, stopping if cur becomes nil
cur, pre = @root, nil
while !cur.nil?
# Return immediately if a duplicate node is found
return if cur.val == num
pre = cur
# The insertion position lies in the right subtree of cur
if cur.val < num
cur = cur.right
# The insertion position lies in the left subtree of cur
else
cur = cur.left
end
end
# Insert a node
node = TreeNode.new(num)
if pre.val < num
pre.right = node
else
pre.left = node
end
end
Visualize execution
Like lookup, insertion takes \(O(\log n)\) time.
3. Deleting a node¶
First locate the target node, then remove it while preserving “left subtree < root < right subtree.” The procedure depends on whether the target has 0, 1, or 2 children.
As shown below, a node of degree \(0\) is a leaf and can be removed directly.
Figure 7-19 Deleting a node of degree 0
Description of Figure 7-19: Delete a leaf from a binary search tree
A leaf has degree 0, so it leaves no subtree to reconnect. Clear the parent’s reference to the leaf.
Delete leaf 1 from a tree rooted at 8 with values 1 through 15. The two panels show the tree before and after deletion with a degree-0 check between them. The pointer cur identifies the target and pre its parent. The table compares links before and after deletion. “Removed” means the node is no longer in the tree.
| Node | Left child before deletion | Right child before deletion | Left child after deletion | Right child after deletion |
|---|---|---|---|---|
| 1 | Empty | Empty | Removed | Removed |
| 2 | 1 | 3 | Empty | 3 |
| 3 | Empty | Empty | Empty | Empty |
| 4 | 2 | 6 | 2 | 6 |
| 5 | Empty | Empty | Empty | Empty |
| 6 | 5 | 7 | 5 | 7 |
| 7 | Empty | Empty | Empty | Empty |
| 8 | 4 | 12 | 4 | 12 |
| 9 | Empty | Empty | Empty | Empty |
| 10 | 9 | 11 | 9 | 11 |
| 11 | Empty | Empty | Empty | Empty |
| 12 | 10 | 14 | 10 | 14 |
| 13 | Empty | Empty | Empty | Empty |
| 14 | 13 | 15 | 13 | 15 |
| 15 | Empty | Empty | Empty | Empty |
Follow 8 → 4 → 2 → 1 to find target 1. Now cur points to 1 and pre to parent 2. Since 1 has no children, its degree is 0 and no subtree needs reattaching. Set pre.left = None to clear the left link of parent 2.
The remaining values 2 through 15 preserve binary search tree order. The empty position to the left of 2 represents a None reference, not a new node.
As shown below, replace a node of degree \(1\) with its only child.
Figure 7-20 Deleting a node of degree 1
Description of Figure 7-20: Delete a node with one child from a binary search tree
To delete a node of degree 1, redirect its parent’s reference to the target’s only child.
Delete node 2 from a tree rooted at 8 containing values 2 through 15. The starting tree is the result of deleting node 1 in Figure 7-19. Node 2 has only a right child, 3. Two panels with a degree-1 check show the tree before and after deletion. The table compares their links. “Removed” means the node is no longer in the tree.
| Node | Left child before deletion | Right child before deletion | Left child after deletion | Right child after deletion |
|---|---|---|---|---|
| 2 | Empty | 3 | Removed | Removed |
| 3 | Empty | Empty | Empty | Empty |
| 4 | 2 | 6 | 3 | 6 |
| 5 | Empty | Empty | Empty | Empty |
| 6 | 5 | 7 | 5 | 7 |
| 7 | Empty | Empty | Empty | Empty |
| 8 | 4 | 12 | 4 | 12 |
| 9 | Empty | Empty | Empty | Empty |
| 10 | 9 | 11 | 9 | 11 |
| 11 | Empty | Empty | Empty | Empty |
| 12 | 10 | 14 | 10 | 14 |
| 13 | Empty | Empty | Empty | Empty |
| 14 | 13 | 15 | 13 | 15 |
| 15 | Empty | Empty | Empty | Empty |
Follow 8 → 4 → 2 to find cur = 2 with parent pre = 4. Node 2 has no left child and one right child 3, so its degree is 1. Clearing 4.left as for a leaf would make node 3 unreachable from the root. Instead, pre.left = cur.right makes 4.left point to 3.
The remaining values 3 through 15 preserve search tree order. The operation redirects a link: the original node 3 becomes the left child of 4. It does not overwrite the value in node 2.
A node of degree \(2\) cannot simply be removed; another node must replace it. To preserve “left subtree \(<\) root \(<\) right subtree,” use either the smallest node in the right subtree or the largest node in the left subtree.
Suppose we choose the smallest node in the right subtree, the next node in inorder traversal. The process is shown below.
- Find the target node's successor in inorder traversal and call it
tmp. - Replace the target node's value with
tmp's value, then recursively deletetmpfrom the tree.
Figure 7-21 Deleting a node of degree 2
Description of Figure 7-21: Delete a node with two children from a binary search tree
For a target of degree 2, find its inorder successor in the right subtree, detach that node, and then copy its value into the target cur.
The target value is 4. The initial tree contains 3 through 15 and has root 8. The table gives all initial links, with “Empty” for a missing child. Throughout, cur points to the object that originally held 4.
Steps 1 and 2 leave all links intact. Step 3 detaches the original node 5, and only step 4 writes value 5 into cur. Links in the table stay the same except for the changes noted at each step. The right subtree rooted at 12 never changes. The figure’s successor pointer nex corresponds to tmp in the source code.
The code first recursively deletes the successor, then overwrites the value of cur. If cur were changed to 5 first, a search from the root to delete value 5 would encounter cur before the original successor. These two operations cannot simply be reversed in this implementation.
| Node | Initial left child | Initial right child |
|---|---|---|
| 8 (root) | cur (initial value 4) | 12 |
| 3 | Empty | Empty |
| cur (initial value 4) | 3 | 6 |
| 5 (original node) | Empty | Empty |
| 6 | 5 (original node) | 7 |
| 7 | Empty | Empty |
| 9 | Empty | Empty |
| 10 | 9 | 11 |
| 11 | Empty | Empty |
| 12 | 10 | 14 |
| 13 | Empty | Empty |
| 14 | 13 | 15 |
| 15 | Empty | Empty |
Locate the target with two children
Since 4 is smaller than root 8, follow the left link to cur, currently holding 4. Its left and right children are 3 and 6; 6 has left leaf 5 and right leaf 7, as in “All tree links before deletion.” The degree of cur is 2. Simply disconnecting its parent’s edge would lose its children, so this example searches the right subtree for its minimum as a replacement.
Find inorder successor 5
Follow cur (4)→6→5 and point nex to the original node 5. Root 8 has cur on the left; cur still has left child 3 and right child 6; 6 has left child 5 and right child 7. Values and links are unchanged. As the minimum on the right, 5 is greater than 3 on the left and smaller than the remaining right-side values 6 and 7, preserving order after replacement.
Detach the original successor node
Recursively delete leaf 5 referenced by nex and clear 6.left. Root 8 still has cur (value 4) as its left child, and cur has left child 3 and right child 6. Node 6 has no left child and has right leaf 7. Only the original node 5 has been detached; other links match the initial table. Pointer nex retains a reference to the original object so its value can be read next. The dashed circle marks its former position; no node remains there.
Copy the successor’s value into cur
Write successor value 5 into cur, replacing 4. Root 8 has cur (now holding 5) as its left child; cur has left child 3 and right child 6. Node 6 has no left child and has right leaf 7. Only the value in cur changed. Its links to parent 8 and children 3 and 6 are unchanged. The original node 5 is gone, leaving just one copy of value 5 in the tree.
Afterward the tree no longer contains 4. The cur object remains in its original position. The other nodes and the binary search tree order are preserved.
Deletion also takes \(O(\log n)\) time: locating the target takes \(O(\log n)\), and finding its inorder successor takes \(O(\log n)\). An implementation follows:
def remove(self, num: int):
"""Remove a node"""
# Return immediately if the tree is empty
if self._root is None:
return
# Search in a loop, stopping if cur becomes None
cur, pre = self._root, None
while cur is not None:
# The node to remove was found; exit the loop
if cur.val == num:
break
pre = cur
# If present, the node to remove is in the right subtree of cur
if cur.val < num:
cur = cur.right
# If present, the node to remove is in the left subtree of cur
else:
cur = cur.left
# Return immediately if the node to remove does not exist
if cur is None:
return
# Number of children = 0 or 1
if cur.left is None or cur.right is None:
# With 0 or 1 child, child is None or that child, respectively
child = cur.left or cur.right
# Remove node cur
if cur != self._root:
if pre.left == cur:
pre.left = child
else:
pre.right = child
else:
# If the removed node is the root, assign the new root
self._root = child
# Number of children = 2
else:
# Get the inorder successor of cur
tmp: TreeNode = cur.right
while tmp.left is not None:
tmp = tmp.left
# Recursively remove node tmp
self.remove(tmp.val)
# Replace the value of cur with the value of tmp
cur.val = tmp.val
/* Remove a node */
void remove(int num) {
// Return immediately if the tree is empty
if (root == nullptr)
return;
TreeNode *cur = root, *pre = nullptr;
// Continue searching while the current node pointer is not null
while (cur != nullptr) {
// The node to remove was found; exit the loop
if (cur->val == num)
break;
pre = cur;
// The node to remove lies in the right subtree of cur
if (cur->val < num)
cur = cur->right;
// The node to remove lies in the left subtree of cur
else
cur = cur->left;
}
// Return immediately if the node to remove does not exist
if (cur == nullptr)
return;
// Number of children = 0 or 1
if (cur->left == nullptr || cur->right == nullptr) {
// With 0 or 1 child, child is nullptr or that child, respectively
TreeNode *child = cur->left != nullptr ? cur->left : cur->right;
// Remove node cur
if (cur != root) {
if (pre->left == cur)
pre->left = child;
else
pre->right = child;
} else {
// If the removed node is the root, assign the new root
root = child;
}
// Free memory
delete cur;
}
// Number of children = 2
else {
// Get the inorder successor of cur
TreeNode *tmp = cur->right;
while (tmp->left != nullptr) {
tmp = tmp->left;
}
int tmpVal = tmp->val;
// Recursively remove node tmp
remove(tmp->val);
// Replace the value of cur with the value of tmp
cur->val = tmpVal;
}
}
/* Remove a node */
void remove(int num) {
// Return immediately if the tree is empty
if (root == null)
return;
TreeNode cur = root, pre = null;
// Search in a loop until reaching a null reference beyond a leaf
while (cur != null) {
// The node to remove was found; exit the loop
if (cur.val == num)
break;
pre = cur;
// The node to remove lies in the right subtree of cur
if (cur.val < num)
cur = cur.right;
// The node to remove lies in the left subtree of cur
else
cur = cur.left;
}
// Return immediately if the node to remove does not exist
if (cur == null)
return;
// Number of children = 0 or 1
if (cur.left == null || cur.right == null) {
// With 0 / 1 children, child is null / the child node, respectively
TreeNode child = cur.left != null ? cur.left : cur.right;
// Remove node cur
if (cur != root) {
if (pre.left == cur)
pre.left = child;
else
pre.right = child;
} else {
// If the removed node is the root, assign the new root
root = child;
}
}
// Number of children = 2
else {
// Get the inorder successor of cur
TreeNode tmp = cur.right;
while (tmp.left != null) {
tmp = tmp.left;
}
// Recursively remove node tmp
remove(tmp.val);
// Replace the value of cur with the value of tmp
cur.val = tmp.val;
}
}
/* Remove a node */
void Remove(int num) {
// Return immediately if the tree is empty
if (root == null)
return;
TreeNode? cur = root, pre = null;
// Search in a loop, stopping if a null reference is reached
while (cur != null) {
// The node to remove was found; exit the loop
if (cur.val == num)
break;
pre = cur;
// The node to remove lies in the right subtree of cur
if (cur.val < num)
cur = cur.right;
// The node to remove lies in the left subtree of cur
else
cur = cur.left;
}
// Return immediately if the node to remove does not exist
if (cur == null)
return;
// Number of children = 0 or 1
if (cur.left == null || cur.right == null) {
// With 0 / 1 children, child is null / the child node, respectively
TreeNode? child = cur.left ?? cur.right;
// Remove node cur
if (cur != root) {
if (pre!.left == cur)
pre.left = child;
else
pre.right = child;
} else {
// If the removed node is the root, assign the new root
root = child;
}
}
// Number of children = 2
else {
// Get the inorder successor of cur
TreeNode? tmp = cur.right;
while (tmp.left != null) {
tmp = tmp.left;
}
// Recursively remove node tmp
Remove(tmp.val!.Value);
// Replace the value of cur with the value of tmp
cur.val = tmp.val;
}
}
/* Remove a node */
func (bst *binarySearchTree) remove(num int) {
cur := bst.root
// Return immediately if the tree is empty
if cur == nil {
return
}
// Track the parent of the node to remove
var pre *TreeNode = nil
// Continue searching while the current node is not nil
for cur != nil {
if cur.Val == num {
break
}
pre = cur
if cur.Val.(int) < num {
// The node to remove is in the right subtree
cur = cur.Right
} else {
// The node to remove is in the left subtree
cur = cur.Left
}
}
// Return immediately if the node to remove does not exist
if cur == nil {
return
}
// The node has 0 or 1 child
if cur.Left == nil || cur.Right == nil {
var child *TreeNode = nil
// Get the child of the node to remove
if cur.Left != nil {
child = cur.Left
} else {
child = cur.Right
}
// Remove node cur
if cur != bst.root {
if pre.Left == cur {
pre.Left = child
} else {
pre.Right = child
}
} else {
// If the removed node is the root, assign the new root
bst.root = child
}
// The node has 2 children
} else {
// Get the inorder successor of cur, the node to remove
tmp := cur.Right
for tmp.Left != nil {
tmp = tmp.Left
}
// Recursively remove node tmp
bst.remove(tmp.Val.(int))
// Replace the value of cur with the value of tmp
cur.Val = tmp.Val
}
}
/* Remove a node */
func remove(num: Int) {
// Return immediately if the tree is empty
if root == nil {
return
}
var cur = root
var pre: TreeNode?
// Search in a loop, stopping if cur becomes nil
while cur != nil {
// The node to remove was found; exit the loop
if cur!.val == num {
break
}
pre = cur
// If present, the node to remove lies in the right subtree of cur
if cur!.val < num {
cur = cur?.right
}
// If present, the node to remove lies in the left subtree of cur
else {
cur = cur?.left
}
}
// Return immediately if the node to remove does not exist
if cur == nil {
return
}
// Number of children = 0 or 1
if cur?.left == nil || cur?.right == nil {
// With 0 / 1 children, child is nil / the child node, respectively
let child = cur?.left ?? cur?.right
// Remove node cur
if cur !== root {
if pre?.left === cur {
pre?.left = child
} else {
pre?.right = child
}
} else {
// If the removed node is the root, assign the new root
root = child
}
}
// Number of children = 2
else {
// Get the inorder successor of cur
var tmp = cur?.right
while tmp?.left != nil {
tmp = tmp?.left
}
// Recursively remove node tmp
remove(num: tmp!.val)
// Replace the value of cur with the value of tmp
cur?.val = tmp!.val
}
}
/* Remove a node */
remove(num) {
// Return immediately if the tree is empty
if (this.root === null) return;
let cur = this.root,
pre = null;
// Search in a loop until reaching a null reference beyond a leaf
while (cur !== null) {
// The node to remove was found; exit the loop
if (cur.val === num) break;
pre = cur;
// The node to remove lies in the right subtree of cur
if (cur.val < num) cur = cur.right;
// The node to remove lies in the left subtree of cur
else cur = cur.left;
}
// Return immediately if the node to remove does not exist
if (cur === null) return;
// Number of children = 0 or 1
if (cur.left === null || cur.right === null) {
// With 0 / 1 children, child is null / the child node, respectively
const child = cur.left !== null ? cur.left : cur.right;
// Remove node cur
if (cur !== this.root) {
if (pre.left === cur) pre.left = child;
else pre.right = child;
} else {
// If the removed node is the root, assign the new root
this.root = child;
}
}
// Number of children = 2
else {
// Get the inorder successor of cur
let tmp = cur.right;
while (tmp.left !== null) {
tmp = tmp.left;
}
// Recursively remove node tmp
this.remove(tmp.val);
// Replace the value of cur with the value of tmp
cur.val = tmp.val;
}
}
/* Remove a node */
remove(num: number): void {
// Return immediately if the tree is empty
if (this.root === null) return;
let cur: TreeNode | null = this.root,
pre: TreeNode | null = null;
// Search in a loop, stopping if a null reference is reached
while (cur !== null) {
// The node to remove was found; exit the loop
if (cur.val === num) break;
pre = cur;
// The node to remove lies in the right subtree of cur
if (cur.val < num) cur = cur.right;
// The node to remove lies in the left subtree of cur
else cur = cur.left;
}
// Return immediately if the node to remove does not exist
if (cur === null) return;
// Number of children = 0 or 1
if (cur.left === null || cur.right === null) {
// With 0 / 1 children, child is null / the child node, respectively
const child: TreeNode | null =
cur.left !== null ? cur.left : cur.right;
// Remove node cur
if (cur !== this.root) {
if (pre!.left === cur) pre!.left = child;
else pre!.right = child;
} else {
// If the removed node is the root, assign the new root
this.root = child;
}
}
// Number of children = 2
else {
// Get the inorder successor of cur
let tmp: TreeNode | null = cur.right;
while (tmp!.left !== null) {
tmp = tmp!.left;
}
// Recursively remove node tmp
this.remove(tmp!.val);
// Replace the value of cur with the value of tmp
cur.val = tmp!.val;
}
}
/* Remove a node */
void remove(int _num) {
// Return immediately if the tree is empty
if (_root == null) return;
TreeNode? cur = _root;
TreeNode? pre = null;
// Search in a loop, stopping if a null reference is reached
while (cur != null) {
// The node to remove was found; exit the loop
if (cur.val == _num) break;
pre = cur;
// The node to remove lies in the right subtree of cur
if (cur.val < _num)
cur = cur.right;
// The node to remove lies in the left subtree of cur
else
cur = cur.left;
}
// Return if the node to remove does not exist
if (cur == null) return;
// Number of children = 0 or 1
if (cur.left == null || cur.right == null) {
// With 0 / 1 children, child is null / the child node, respectively
TreeNode? child = cur.left ?? cur.right;
// Remove node cur
if (cur != _root) {
if (pre!.left == cur)
pre.left = child;
else
pre.right = child;
} else {
// If the removed node is the root, assign the new root
_root = child;
}
} else {
// Number of children = 2
// Get the inorder successor of cur
TreeNode? tmp = cur.right;
while (tmp!.left != null) {
tmp = tmp.left;
}
// Recursively remove node tmp
remove(tmp.val);
// Replace the value of cur with the value of tmp
cur.val = tmp.val;
}
}
/* Remove a node */
pub fn remove(&mut self, num: i32) {
// Return immediately if the tree is empty
if self.root.is_none() {
return;
}
let mut cur = self.root.clone();
let mut pre = None;
// Search while cur refers to a node
while let Some(node) = cur.clone() {
match num.cmp(&node.borrow().val) {
// The node to remove was found; exit the loop
Ordering::Equal => break,
// The node to remove lies in the right subtree of cur
Ordering::Greater => {
pre = cur.clone();
cur = node.borrow().right.clone();
}
// The node to remove lies in the left subtree of cur
Ordering::Less => {
pre = cur.clone();
cur = node.borrow().left.clone();
}
}
}
// Return immediately if the node to remove does not exist
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()) {
// Number of children = 0 or 1
(None, None) | (Some(_), None) | (None, Some(_)) => {
// With 0 or 1 child, child is None or that child, respectively
let child = left_child.or(right_child);
let pre = pre.unwrap();
// Remove node 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 {
// If the removed node is the root, assign the new root
self.root = child;
}
}
// Number of children = 2
(Some(_), Some(_)) => {
// Get the inorder successor of 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 tmp_val = tmp.unwrap().borrow().val;
// Recursively remove node tmp
self.remove(tmp_val);
// Replace the value of cur with the value of tmp
cur.borrow_mut().val = tmp_val;
}
}
}
/* Remove a node */
// The name remove conflicts with the function declared in stdio.h
void removeItem(BinarySearchTree *bst, int num) {
// Return immediately if the tree is empty
if (bst->root == NULL)
return;
TreeNode *cur = bst->root, *pre = NULL;
// Search in a loop until reaching a null reference beyond a leaf
while (cur != NULL) {
// The node to remove was found; exit the loop
if (cur->val == num)
break;
pre = cur;
if (cur->val < num) {
// The node to remove is in cur's right subtree
cur = cur->right;
} else {
// The node to remove is in cur's left subtree
cur = cur->left;
}
}
// Return immediately if the node to remove does not exist
if (cur == NULL)
return;
// Check whether the node to remove has children
if (cur->left == NULL || cur->right == NULL) {
/* Number of children = 0 or 1 */
// With 0 or 1 child, child is NULL or that child, respectively
TreeNode *child = cur->left != NULL ? cur->left : cur->right;
// Remove node cur
if (pre->left == cur) {
pre->left = child;
} else {
pre->right = child;
}
// Free memory
free(cur);
} else {
/* Number of children = 2 */
// Get the inorder successor of cur
TreeNode *tmp = cur->right;
while (tmp->left != NULL) {
tmp = tmp->left;
}
int tmpVal = tmp->val;
// Recursively remove node tmp
removeItem(bst, tmp->val);
// Replace the value of cur with the value of tmp
cur->val = tmpVal;
}
}
/* Remove a node */
fun remove(num: Int) {
// Return immediately if the tree is empty
if (root == null)
return
var cur = root
var pre: TreeNode? = null
// Continue searching while cur is not null
while (cur != null) {
// The node to remove was found; exit the loop
if (cur._val == num)
break
pre = cur
// The node to remove lies in the right subtree of cur
cur = if (cur._val < num)
cur.right
// The node to remove lies in the left subtree of cur
else
cur.left
}
// Return immediately if the node to remove does not exist
if (cur == null)
return
// Number of children = 0 or 1
if (cur.left == null || cur.right == null) {
// With 0 / 1 children, child is null / the child node, respectively
val child = if (cur.left != null)
cur.left
else
cur.right
// Remove node cur
if (cur != root) {
if (pre!!.left == cur)
pre.left = child
else
pre.right = child
} else {
// If the removed node is the root, assign the new root
root = child
}
// Number of children = 2
} else {
// Get the inorder successor of cur
var tmp = cur.right
while (tmp!!.left != null) {
tmp = tmp.left
}
// Recursively remove node tmp
remove(tmp._val)
// Replace the value of cur with the value of tmp
cur._val = tmp._val
}
}
### Remove a node ###
def remove(num)
# Return immediately if the tree is empty
return if @root.nil?
# Search in a loop, stopping if cur becomes nil
cur, pre = @root, nil
while !cur.nil?
# The node to remove was found; exit the loop
break if cur.val == num
pre = cur
# The node to remove lies in the right subtree of cur
if cur.val < num
cur = cur.right
# The node to remove lies in the left subtree of cur
else
cur = cur.left
end
end
# Return immediately if the node to remove does not exist
return if cur.nil?
# Number of children = 0 or 1
if cur.left.nil? || cur.right.nil?
# With 0 / 1 children, child is nil / the child node, respectively
child = cur.left || cur.right
# Remove node cur
if cur != @root
if pre.left == cur
pre.left = child
else
pre.right = child
end
else
# If the removed node is the root, assign the new root
@root = child
end
# Number of children = 2
else
# Get the inorder successor of cur
tmp = cur.right
while !tmp.left.nil?
tmp = tmp.left
end
# Recursively remove node tmp
remove(tmp.val)
# Replace the value of cur with the value of tmp
cur.val = tmp.val
end
end
Visualize execution
4. Inorder traversal produces sorted data¶
As shown below, inorder traversal visits “left \(\rightarrow\) root \(\rightarrow\) right,” while a binary search tree obeys “left child \(<\) root \(<\) right child.”
Thus, inorder traversal always visits the next smallest node, giving an important property: the inorder traversal of a binary search tree is in ascending order.
This lets us retrieve sorted data from the tree in just \(O(n)\) time, with no additional sorting.
Figure 7-22 Inorder traversal of a binary search tree
Description of Figure 7-22: Inorder traversal of a binary search tree
Visit the left subtree, record the current node, then visit the right subtree to output all values in ascending order.
The binary search trees in this book have no duplicate values. At every node, all values in its left subtree are smaller and all values in its right subtree are larger.
Root 8 has left and right children 4 and 12. Node 4 has children 2 and 6; node 12 has children 10 and 14. Nodes 2, 6, 10, and 14 are leaves. A dashed green path traces the traversal. Blue dots mark the moments when values are recorded.
At each node recursively traverse the smaller values on the left, record the node, then traverse the larger values on the right. The left subtree of root 8 emits 2, 4, 6; then comes 8, followed by the right subtree’s 10, 12, 14. The final result on the right is [2, 4, 6, 8, 10, 12, 14].
Inorder traversal yields ascending values without a separate sort. The increase is strict because the book excludes duplicate node values.
7.4.2 Efficiency¶
Suppose we want to store a dataset in an array or a binary search tree. As Table 7-2 shows, the tree's operations take logarithmic time, offering consistent and efficient performance. An array is faster only when insertions are frequent but lookups and deletions are infrequent.
Table 7-2 Efficiency of arrays and search trees
| Unsorted array | Binary search tree | |
|---|---|---|
| Find an element | \(O(n)\) | \(O(\log n)\) |
| Insert an element | \(O(1)\) | \(O(\log n)\) |
| Delete an element | \(O(n)\) | \(O(\log n)\) |
Ideally, the tree is balanced, allowing any node to be found within \(\log n\) iterations.
However, repeated insertions and deletions may cause it to degenerate into a linked list, as shown below. The time complexity of its operations then degrades to \(O(n)\).
Figure 7-23 Degeneration of a binary search tree
Description of Figure 7-23: A balanced binary search tree and a tree reduced to a chain
The same seven nodes satisfy the same search tree ordering, but the shape changes the length of a search path.
In a binary search tree, all values in a node’s left subtree are smaller than its value and all values in its right subtree are larger. Arrows point from parent to child.
The left figure is labeled “Balanced binary search tree.” Root 4 has left and right children 2 and 6; 2 has children 1 and 3, and 6 has children 5 and 7. Counting gives three levels and a height of 2 edges. The right figure, “Degenerate binary search tree,” starts at root 7 and follows only left children: 7 → 6 → 5 → 4 → 3 → 2 → 1. Every right link is empty. There are seven levels and a height of 6 edges.
Both trees contain the same values 1 through 7 and satisfy the strict binary search tree ordering. Searching for leaf 1 on the left visits just three nodes along 4 → 2 → 1. On the right it visits all seven along 7 → 6 → 5 → 4 → 3 → 2 → 1. The figure compares two structures without showing any insertions or deletions that would change one into the other.
In a balanced binary search tree, search, insertion, and deletion can take logarithmic time. In a single chain the worst case visits every node, taking O(n) time.
7.4.3 Common applications¶
- Multilevel indexes in systems, supporting efficient lookup, insertion, and deletion.
- The underlying data structure for certain search algorithms.
- Storing a data stream while maintaining its order.