6.5Binary Search Tree with Linked Nodes
Delete: a leaf and a node with one child
Deleting from a binary search tree splits into three cases depending on how many children the node has. In this lesson you will see the two easy ones: a leaf and a node with one child. In both, the node steps aside and its parent is linked to the node's child.
- How to find the node to delete by following the search path
- How a leaf and a node with one child are deleted with the same lines
- How the parent's link is updated through the recursive return
1Idea
Deletion first finds the node. This is the same path as search: go left if the value is smaller, right if it is larger. If you reach NULL, the value is not in the tree and the tree stays unchanged.
Once the node is found, its children are checked. If it is a leaf, it is deleted and the parent's field becomes NULL. If it has one child, the node is deleted and the child takes its place: the child's whole subtree moves up one level. The BST rule still holds, because that subtree was already on the same side of the parent.
The code handles both cases together. If there is no left child, the right child is returned: for a leaf that is NULL, for a node with only a right child it is that child. If there is no right child, the left child is returned. The returned address is written into the parent by the assignment root->left = deleteNode(...) in the call above. In C the node is released with free before returning.
This does not work for a node with two children: two children cannot both take its place. In this lesson the code returns without changing the tree in that case; the full solution is in the next lesson.
2Step by step
The drawing deletes 20 (a leaf) and then 60 (a node with one child) from a tree holding 50, 30, 70, 20, 40, 60, 80 and 65. The stack on the right shows the open calls.
- Start. First we will delete 20, which is a leaf.
- 20 is smaller than 50: a delete call opens on the left subtree.
- 20 is smaller than 30: go down into the left subtree of 30.
- 20 is found. It has no left child and no right child: a leaf.
- There is no left child, so the right child, NULL, is kept. The node is freed and NULL is returned.
- The call for 30 writes the returned NULL into its
leftfield. 20 is out of the tree. - Now we delete 60, which has one child. 60 is larger than 50: go right.
- 60 is smaller than 70: go down to the left of 70.
- 60 is found. It has no left child and 65 on its right: one child. The child to return is 65.
- 60 is freed and the address of 65 is returned.
- The call for 70 writes the returned address into its
leftfield. 65 took the place of 60 and moved up one level. - Result: 30, 40, 50, 65, 70, 80. The tree still follows the BST rule.
struct TreeNode { int data; struct TreeNode* left; struct TreeNode* right;}; struct TreeNode* deleteNode(struct TreeNode* root, int value) { if (root == NULL) return NULL; // value not in the tree if (value < root->data) { root->left = deleteNode(root->left, value); // look left } else if (value > root->data) { root->right = deleteNode(root->right, value); // look right } else if (root->left == NULL) { // a leaf or only a right child struct TreeNode* child = root->right; free(root); return child; // the parent links to the child } else if (root->right == NULL) { // only a left child struct TreeNode* child = root->left; free(root); return child; } // a node with two children: next lesson return root;}class TreeNode { int data; TreeNode left, right; TreeNode(int data) { this.data = data; }} TreeNode deleteNode(TreeNode root, int value) { if (root == null) return null; // value not in the tree if (value < root.data) { root.left = deleteNode(root.left, value); // look left } else if (value > root.data) { root.right = deleteNode(root.right, value); // look right } else if (root.left == null) { // a leaf or only a right child return root.right; // the parent links to the child } else if (root.right == null) { // only a left child return root.left; } // a node with two children: next lesson return root;}class TreeNode: def __init__(self, data): self.data = data self.left = None self.right = None def delete_node(root, value): if root is None: return None # value not in the tree if value < root.data: root.left = delete_node(root.left, value) # look left elif value > root.data: root.right = delete_node(root.right, value) # look right elif root.left is None: # a leaf or only a right child return root.right # the parent links to the child elif root.right is None: # only a left child return root.left # a node with two children: next lesson return rootThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
The node is found by a search: at most h steps. Changing the links takes constant time. O(log n) in a balanced tree, O(n) in the worst case.
Recursion keeps one call open per level: O(h).
The real cost of deletion is finding the node. For a leaf or a node with one child, only one link changes once the node is found.
4Common mistake
A common mistake is freeing the node and then reading its field:
free(root);return root->right; // reads freed memoryThe fields of a freed node are no longer valid; the program sometimes works and sometimes crashes. That is why the code first copies the child into child and only then calls free. The other common mistake is not writing the returned value into the parent: with deleteNode(root->left, value); alone, the parent keeps pointing to the freed node.
5Try it
When a node with one child is deleted, what happens to the child's subtree?
The answer is A. The parent's pointer now points to the child instead of the deleted node. The child's subtree moves up one level as it is; no other link changes.
Show the answer
The answer is A. The parent's pointer now points to the child instead of the deleted node. The child's subtree moves up one level as it is; no other link changes.
What does the code in this lesson do when asked to delete a node with two children?
The answer is B. The code has no branch for two children; when the value is found neither condition holds and return root runs. That case is solved separately in the next lesson.
Show the answer
The answer is B. The code has no branch for two children; when the value is found neither condition holds and return root runs. That case is solved separately in the next lesson.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.