The binary search tree usually provides the methods:

  • get(k): if the map has an entry with key , return its associated value , else null.
  • put(k,o): if does not have an entry then add it to the map and return null, else replace the existing value of the entry with key equal to with and return the old value.
  • remove(k): if the map has an entry with key , remove it from and return its associated value, else return null.

For the examples below, we assume that a binary tree supports:

  • insertAtExternal(w, (k,o)): insert the element at the external node and expand to be internal, having new (empty) external node children
  • removeExternal(w): remove an external node and its parent, replacing ‘s parent with ‘s sibling.

To perform get(k): we search for a key and trace a downward path starting from the root. The next node visited depends on the comparison of with the key of the current node. If we reach a leaf, the key is not found.

Insertion

To perform operation put(k,o), we search for key . We assume is not already in the tree, and let be the leaf reached by the search. We insert at node and expand into an internal node using insertAtExternal(w, (k, o)).

Deletion

To perform operation remove(k), we search for key . Assume key is in the tree, and let be the node storing . If node has a leaf child , we remove and from the tree with the operation removeExternal(w).

We may also have a case where the key to be removed is stored at a node whose children are both internal:

  • we find the internal node that follows in an in-order traversal
  • we copy into node
  • we remove node and left child (must be a leaf) by removeExternal(z)