Lecture Notes 12: More trees 3/11

Breadth-first search

Start at the root; then go across the first row from left to right; then across the second row from left to right; and so on.

Mammal, Rodent, Carnivore, Primate, Squirrel, Rat, Cat, Dog, Skunk, Gorilla, Human.

That's it.
Initialize a FIFO queue Q to hold the root;
repeat {
    N = pop the first node off Q;
    print N; 
    add N's children to the end of Q;
} until (Q is empty)
BreadthFirstSearch.java
FIFOQueue.java

Unlike depth-first search, that's pretty much the only way to implement it. In particular, there's nothing reasonable that corresponds to the recursive implementation of DFS.

(You might think, "The recursive implementation of DFS is actually using a stack of activation records, so it's just hiding the stack. Maybe if you use a FIFO queue of activation records, you could get the same kind of thing for BFS?" But it turns out that it's hard to make sense of a programming languages that uses a FIFO queue of activation records.)

Binary Search Trees

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 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.