CSci 160 Lab 4: Recursion Removal in the BST Class: Part II

Objectives

A continuation of Lab 3. This week we will take care of deletion. In addition, we will get started on assignment 3.

Exercises

  1. Start with the BinarySearchTree code you wrote from lab 3.
  2. Modify this public method:

    public void remove( AnyType x )
    { root = remove( x, root ); }

    so that it doesn't use the recursive method:

    private BinaryNode<AnyType> remove( AnyType x, BinaryNode<AnyType> t ) { .... }

    Instead, it should a while-loop to search for the element (don't use the recursive find method) and then delete it properly. Note that there should be no use of recursion in this public method.

  3. Here is a pseudocode to get  you started:

    Starting from the ROOT;
    While ( the node is not null AND the node doesn't contain the element x ){
        go to its left or right child accordingly;
    }
    if the node is null then x is not found; //there is nothing to delete
    else //the node contains x
        handle the 3 cases separately; i.e., the node has no children or one child or two children;

  4. In case of two children, incorporate a non-recursive version of findMin (that uses a while-loop) to find the minimal element of the right subtree and use it to replace x, and then delete it - this is the simple zero or one child case, because the minimal element must be the leftmost node of the right subtree.
  5. We will discuss assignment 3 at length today as well, and you will begin work on it. The place to start is to modify the insert method so that it determines the parent of the node that is inserted. Various strategies (both iterative and recursive) for doing this will be discussed. This work will be continued in next week's lab.
Hand in: Electronic submissions of the modified programs, with changes indicated clearly via comments.
Last modified: 9/27/10 by Frederic Green.