## Lecture Notes 12: More trees 3/11

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.

• Take the stack-based code for depth-first search.
• Change the stack to a FIFO queue.
• Add the children of c in forward order rather than backward order.
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)
```
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:
• All the values in the left-hand subtree of N are less than the value at N.
• All the values in the right-hand subtree of N are greater than the value at N.

#### 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:

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) {
else {
create new node P for V;
N.left = P;
}
}
else if (V > N.value)
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:
• Case 1: V isn't in the tree. Don't do anything.
• Case 2: V is at node P which is a leaf. Delete P.
• Case 3: V is at an internal node P with only one child C. Move C into P's position.

Case 4: V is at an internal node P with two children. Let Q be either the node immediately before P, which is the rightmost descendant of P.left, or the node immediately after P, which is the leftmost descendant of P.right. In either case, Q does not have two children (if it did, it wouldn't be leftmost/rightmost) so either:

• Case 4A: Q is a leaf. Move Q.value into P.value, and delete Q.
• Case 4B: Q has one child C. This must be a left child, since Q is the rightmost descendent. Move Q.value into P.value and move C into Q's position.
```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.