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 binary search tree indexes eight warehouse item IDs.


a) Search for item ID 6666. List the inspected keys and count the existing nodes inspected, counting one comparison per node.


Start at the root, 4848. Because 66>4866>48, move ________ to 7272. Because 66<7266<72, move ________ to 6060. Because 66>6066>60, move ________ to 6666.


The next comparison finds an equal key, so the search succeeds. The inspected keys are


48→72→60→66.48\to72\to60\to66.


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 4848, 7272, 6060, and 6666, for a total of 44 nodes.


b) Insert item ID 6363. State its parent key and whether it becomes a left or right child.


Compare 6363 with 4848, then 7272, then 6060, then 6666. This inspects ________ existing nodes. The comparisons take you right, left, right, and finally left, because 63<6663<66.


The left child position of 6666 is empty, so place the new node there. Check the ancestor restrictions: 63>4863>48, 63<7263<72, 63>6063>60, and 63<6663<66. Every restriction is satisfied.


The initial eight nodes remain, and insertion adds one node:


8+1=9.8+1=9.


Answer: Insert 6363 as the left child of 6666. The tree now contains 99 nodes.


c) Delete item ID 4848 using successor replacement. State the new root key, the new left child of 7272, and the remaining node count.


The root has two children. Its inorder successor is the smallest key in its right subtree. Starting at 7272, follow its ________ child to 6060. There is no further left child, so the successor key is 6060.


Replace the root key 4848 with 6060. Then remove the original node containing 6060. That original node has ________ child 6666, so reconnect 7272's left child link to 6666. The node containing 6363 stays attached as the left child of 6666.


This preserves the ordering: both 6363 and 6666 exceed the new root key 6060, and both remain smaller than 7272. Exactly ________ node is removed:


9−1=8.9-1=8.


Answer: The new root key is 6060, the left child of 7272 is 6666, and 88 nodes remain.




A warehouse searches the binary search tree shown for item ID 8484. All keys are distinct. Count one comparison for each existing node whose key is inspected, including the root. How many existing nodes are inspected before the search reports that the ID is absent? Give an exact integer number of nodes.


A binary search tree contains eight distinct integer keys.


Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Match each binary search tree term with its description. Assume every key stored in the tree is distinct and that keys are compared as integers.

Match each term
A.

The only node with no parent, where every search and insertion begins

B.

A node with no children

C.

The smallest key in the tree that is larger than a given key

D.

A shape in which every node has at most one child, so a search can visit all nn nodes

E.

The number of edges on the longest downward path from the root to a node with no children

F.

The largest key in the tree that is smaller than a given key

G.

For every node, all keys in its left subtree are smaller and all keys in its right subtree are larger

In-order predecessor

In-order successor

Root

BST property

Height

Leaf

Degenerate tree

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

An inventory system inserts the distinct keys 2727, 6161, and 5858, 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 00. Complete the parent key and depth for each inserted node using exact integers. Use the updated tree for each subsequent insertion.


A binary search tree has root 40 and six other nodes.


Inserted keyParent keyDepth (edges)
27
61
58

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice