CS 112 - Lab 6
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 6 files into it. If you start off in your home directory you can do the following:

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

Section 2: Topics for Lecture

Section 3: Description of Assignment


You are to implement a binary search tree in which each node contains information about a student:

Last name
Id number
GPA


Three operations will be available for manipulating the list:

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

Examples which could be processed include:

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

As with Lab 5, 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 be given the following files:

  • tree.h (containing typedefs, structs and macros)
  • main.c (containing the function main( ))

    We suggest writing the following functions:

  • void create_tree(Tree *);
  • TreeNode *lookup(char *, TreeNode *); /* iterative */
  • void insert(TreeNode *, Tree *); /* iterative */
  • TreeNode *create_node(Record);
  • void traverse(TreeNode *); /* recursive */
  • void print_info(TreeNode *);
  • void replace(float, TreeNode *);
  • boolean tree_empty(Tree *);

    These prototypes are in tree.h.

    As you can see, there is no function set_position( ) so visit counts must be updated in insert( ) and lookup( ).

    Insert( ) will not only find the position the new node needs to go, but also do the linking required.

    Insert( ) and lookup( ) should be implemented as iterative functions while traverse( ) should be recursive.

    Some of your code from Lab 5 can be used with minimal modification; some functions, however, functions such as insert( ) and lookup( ) must be replaced.

    A search will always begin at the root since there is no current position pointer.

    Remember to use stubs in your top-down algorithm design.
    
    

    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/lab6 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 lab6

    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 lab6

    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 March 21 (no late submissions accepted).

    This week's questions

    For credit on this lab, your software must be received by midnight March 20 and your answers to the lab questions must be received at the beginning of lab on Friday, March 21.

    Note: Always bring your ID with you to lab.