CS 112 - Lab 9
Deletion from Binary Search Trees

Section 1: Introduction

The goal of this lab is to:

If you have not already done so, create a directory for this lab and copy the lab 9 files into it. If you start off in your home directory you can do the following:

	mkdir ~/cs112/lab9 
	cd ~/cs112/lab9
	cp ~cs112/lab9/* .
	cp ~/cs112/lab6/* .

Section 2: Topics for Lecture

Section 3: Description of Assignment

In Lab 6, you have implemented a binary search tree with insertion, lookup and replacement. In this lab, you will provide the deletion operation.

As in Lab 6, each node in the tree contains information about a student:

Last name
Id number

Four operations will be available for manipulating the tree:

Insert a node alphabetically by name (last name only)
Lookup a name and print associated information
Replace the GPA field of a person's record
Delete a node by name

Examples which could be processed include:

Insertion: I Roberts 12345 3.2
Lookup: L Roberts
Replacement: R Roberts 3.5
Deletion: D Roberts

As with Lab 6, your program will read the input from a file named input_file.

In addition to the personal info in the node structure, there will be one more field which holds the number of visits that node has received during the course of the program. You will need to increment it each time you visit a node.

At the end of your program, the tree must be traversed and the information in each (including the number of visits field) printed to stdout.

Example output:

        NAME     ID       GPA      VISITS

	Brown    12345    3.1      2
	Evans    54321    4.0      1
	Finch    12342    2.1      1
	Solo     32321    3.8      2

Section 4: Code Development

You will add a couple of functions to the program you have written for Lab 6, and the previous code should not need any changes.

We suggest writing the following functions for deletion:

  • void DeleteNodeTree(TreeNode **);
  • TreeNode **find(char *, Tree *);

    You need to add these two function prototypes to your tree.h file and add the actual function body to tree.c.

    DeleteNodeTree( ) takes one parameter - the address of a pointer which points to the node you want to delete. DeleteNodeTree( ) should delete a node from a node from the tree and re-organize the tree. After the deletion, the tree should still be a binary search tree (no balancing is required for this lab). The code for this function is available from Chapter 9 in your textbook.

    The function find( ) requires two parameters. The first parameter is the name field of the record you are searching for. The second parameter is a pointer to the Tree data structure. find( ) should return the address of a pointer. This pointer must be the left or right pointer of another certain tree node whether or not you actually find the target record. The address of this pointer should be passed to DeleteNodeTree( ). The function find( ) should increment the visit counts similar to the function lookup( ). You may elect to modify your previous lookup( ) function to use the function find( ) also.



    Section 5: Handing in your C code and answers

    When you have completed this lab assignment, you should have the following program(s) in your ~/cs112/lab9 directory:

  • main.c (containing the function main( ))
  • tree.c (containing all tree operation functions)
  • tree.h (containing all typedefs, prototypes, etc)
  • makefile

    Then, to submit your program(s) for grading, issue the command

    ~cs112/submit lab9

    at your UNIX prompt. This utility program will copy your programs for grading and an electronic mail message will be sent to you to acknowledge your submission. You may check the status of your submission at any time by typing the command

    ~cs112/query lab9

    at your UNIX prompt. You will automatically receive an electronic mail message confirming the status of your lab submission.

    You should also answer the questions below and submit them in hardcopy to the TAs at the beginning of lab on April 25 (no late submissions accepted).

    This week's questions

    For credit on this lab, your software must be received by midnight Wednesday, April 23 and your answers to the lab questions must be received at the beginning of lab on Friday, April 25.

    Note: Always bring your ID with you to lab.