UCR CS 14: Introduction to Data Structures and Algorithms

Spring Quarter 2003 / Class webpage




Lab 07

Remember:

In this lab, we will work with ADT binary search tree (chapter 10). Searching for a particular item is one operation for which the ADT binary tree is ill suited. The binary search tree is a binary tree that corrects this deficiency by organizing its data by value. Recall that each node n in a binary search tree satisfies the following three properties:

In this lab you will work using an implementation of a binary search tree. You will need the following files:

Figure 1. UML diagram for the class BinarySearchTree

Given the recursive nature of a binary (search) tree, a good strategy for writing a C++ function that operates on a binary (search) tree is often first to write a recursive definition of the task. Given such a recursive defition, a C++ implementation is often straightforward. Today, you will implement a set of functions considering an ADT Binary Search Tree. You may implement each of the exercises in one function and print their results as a report, for example:

Enter a positive value (negative value to stop): 10
Enter a positive value (negative value to stop): 8
Enter a positive value (negative value to stop): 12
Enter a positive value (negative value to stop): 7
Enter a positive value (negative value to stop): 9
Enter a positive value (negative value to stop): 11
Enter a positive value (negative value to stop): 13
Enter a positive value (negative value to stop): 15
Enter a positive value (negative value to stop): -1

-------------------------------------------------
IN ORDER TRAVERSE:
7
8
9
10
11
12
13
15

a) ROOT: 10

b) NUMBER OF NODES: 8

c) SUM OF NODES: 85

d) SEARCH AND COUNT FOR VALUE: 13
   NUMBER OF COMPARISONS = 3

e) HEIGHT: 4

f) SEARCH FOR VALUES BETWEEN: 8
   AND: 12
   RESULT:
     8
     9
     10
     11
     12

g) PRINT STRUCTURED TREE:
     10
      8
       7
       9
      12
       11
       13
        15

h) INORDER TRAVERSE2:
7
8
9
10
11
12
13
15

i) INSERT NEW ITEM: 14
   NEW TREE:
     10
      8
       7
       9
      12
       11
       13
        15
         14

j) SAVE TREE IN THE FILE: out.txt

k) RESTORE TREE FROM FILE: out.txt
   NEW TREE:
     10
      8
       7
       9
      12
       11
       13
        15
         14

Note that some of these functions (a, b, c, e, f) will be added to lab7.cc, and others (d, g, h, i, j) to the ADT files (BST.cc, BST.h). Also, even though you do not have enough time to finish all bonus exercises during the lab, you should try to do them by yourself. The practice you will get from these exercises will help you with the Ternary Search Tree assignment. Keep in mind that only those exercises that you turn in during the lab will be considered in your grade.


Turn-in

8 points possible (plus 2 from participation)

Bonus, 2 points each


Hints and considerations