39 / \ / \ / \ / \ / \ / \ / \ / \ 18 56 / \ / \ / \ / \ / \ / \ / \ / \ 5 30 47 70 \ / \ \ / \ 10 22 35 52 64 73 / \ \ / \ 7 15 25 50 54 / \ 12 28 \ 13 |
You need not reproduce the entire tree each time, but you should show enough surrounding context to make it clear which part of the tree was affected by the operation and what changes occurred there.
1. Insert 3.
Solution: A new leaf node containing 3 becomes the left child of
the 5-node.
2. Insert 65.
Solution: A new leaf node containing 65 becomes the right child of
the 64-node.
3. Delete 13.
Solution: The leaf node containing 13 disappears.
4. Delete 47.
Solution: As the node containing 47 has a child, we cannot
simply make it disappear without also taking measures to ensure
that its proper descendant nodes remain in the tree.
Because the 47-node has only one child, we connect its child node
directly to 47's parent. That is, the 52-node (47's right child)
becomes the left child of the 56-node.
5. Delete 56.
Solution: As the 56-node has two children, we place into that
node the smallest key in its right subtree (64) and remove that key from
that subtree. Here, that means that the 56-node becomes the 64-node
and the original 64-node, which is a leaf, disappears.
An alternative is to replace 56 by the largest key in its left subtree, which is 54. In that case, the 54-node (a leaf) disappears.
6. Delete 18.
Solution: As the 18-node has two children, we place into that
node the smallest key in its right subtree (22) and remove that key from
that subtree. Here, that means that 22 replaces 18 in the 18-node.
Because the original 22-node has one child, to remove 22 entails
connecting its child directly to its parent, here meaning that the
25-node becomes the left child of the 30-node.
An alternative would be to replace 18 in the 18-node by the largest key in its left subtree, which is 15. In that case, 15 is removed from that left subtree by making the 12-node the right child of the 10-node.