Lecture Notes 11: Binary Search Tree

A binary search tree is a binary tree with the following property: If N is an interior node then:

Searching for a value

Compare to the root. If equal return {\tt true}. If less, recursively look in the left-hand subtree. If greater, recursively look in the right-hand subtree.
boolean search(V,N) { // search for value V in the subtree with root N;
   if (N==null) return false;
   else if (V= N.value) return true;
   else if (V < N.value) return search(V,N.left);
   else return search(V,N.right)     // V > N.value
Example:

Adding a value V

Search for V in the tree as above. If V is there already, do nothing. If it is not, add it at the point where the search ran into a null pointer.
void add(V,N) {
  if (V < N.value) { 
    if (N.left != null) add(V,N.left)
     else {
        create new node P for V;
        N.left = P;
        }
     }
  else if (V > N.value) 
    if (N.right!= null) add(V,N.right)
     else {
        create new node P for V;
        N.right= P;
        }
     }
 }

Deleting a value V

Find V in the tree under N, using the search. Now there are five cases:
void delete(V,N) {
   if (N == null) return;                  // Case 1
   if (V  < N.value) delete(V,N.left);     // recursively go down the tree
   else if (V >  N.value) delete(V,N.right) // recursively go down the tree
   else                                    // V == N.value
     if (N is a leaf) deleteLeaf(N);       // Case 2
     else if (N.left==null)                // Case 3
             replaceNode(N.right, N);
     else if (N.right==null)               // Case 3
             replaceNode(N.left, N);
     else {                                // Case 4
          Q = N.left.rightmostDescendant(); 
          N.value = Q.value;
          if (Q is a leaf)                 // Case 4.A
             deleteLeaf(Q);
          else                             // Case 4.B
             replaceNode(Q.left,Q)
          }
}

void deleteLeaf(L) {
   P = L.parent;
   if (L == P.left) L.left = null;
   else L.right=null;
}

void replaceNode(C,N)  {     // Replace N by C;
   P = N.parent;
   C.parent = P;
   if (N == P.left) 
       P.left = C;
    else P.right=C;
}

****************************************************************************

****************************************************************************

****************************************************************************

Time requirement

All three operations require climbing down from the root to a lower node in the tree. Therefore in the worst case they take time proportional to the height of the tree.

How is the height of the tree H related to the number of elements N

Well, that depends.

In the best case, every internal node (except possibly one in second-to-last row) has two children. Then N is between 2H-1 and 2H-1, so H is about log2(N)) plus or minus 1.

In the worst case, every internal node has exactly one child, and there is only one leaf in the tree. In that case the tree is essentially just a linked list and H = N.