Binary Search Trees
Example: Updating a Warehouse ID Index
Example: A warehouse stores distinct integer item IDs as keys in a binary search tree (BST). At every node, all keys in the left subtree are smaller than that node's key, and all keys in the right subtree are larger. The diagram shows the initial index.
Perform the three operations in order. Carry each change forward into the next part. Insert at an empty child position without rearranging existing nodes, and do not rebalance the tree.
a) Search for item ID 66. List the inspected keys and count the existing nodes inspected, counting one comparison per node.
Start at the root, 48. Because 66>48, move ________ to 72. Because 66<72, move ________ to 60. Because 66>60, move ________ to 66.
The next comparison finds an equal key, so the search succeeds. The inspected keys are
There are four keys on this path. The other branches do not need inspection: the ordering property excludes them at each comparison.
Answer: The search inspects 48, 72, 60, and 66, for a total of 4 nodes.
b) Insert item ID 63. State its parent key and whether it becomes a left or right child.
Compare 63 with 48, then 72, then 60, then 66. This inspects ________ existing nodes. The comparisons take you right, left, right, and finally left, because 63<66.
The left child position of 66 is empty, so place the new node there. Check the ancestor restrictions: 63>48, 63<72, 63>60, and 63<66. Every restriction is satisfied.
The initial eight nodes remain, and insertion adds one node:
Answer: Insert 63 as the left child of 66. The tree now contains 9 nodes.
c) Delete item ID 48 using successor replacement. State the new root key, the new left child of 72, and the remaining node count.
The root has two children. Its inorder successor is the smallest key in its right subtree. Starting at 72, follow its ________ child to 60. There is no further left child, so the successor key is 60.
Replace the root key 48 with 60. Then remove the original node containing 60. That original node has ________ child 66, so reconnect 72's left child link to 66. The node containing 63 stays attached as the left child of 66.
This preserves the ordering: both 63 and 66 exceed the new root key 60, and both remain smaller than 72. Exactly ________ node is removed:
Answer: The new root key is 60, the left child of 72 is 66, and 8 nodes remain.
An inventory system inserts the distinct keys 27, 61, and 58, in that order, into the binary search tree shown. Insert each key at the first empty child position reached by a normal BST search, without rearranging existing nodes.
A node's depth is the number of edges from the root to that node, so the root has depth 0. Complete the parent key and depth for each inserted node using exact integers. Use the updated tree for each subsequent insertion.
| Inserted key | Parent key | Depth (edges) |
| 27 | ||
| 61 | ||
| 58 |