Laboratory Exercises For Computer Science 153

Binary Search Trees

Binary Search Trees

Note: This lab is to be done individually, following the rules of a normal assignment; collaboration is not permitted.

Before Class: Read sections 9.2.2-9.2.6 in the text.

Goals: This laboratory exercise provides some experience using a binary search tree structure.

Discussion: As noted in the text, a binary search tree is a tree, in which

A schematic view of such a tree follows:
A binary search tree

  1. Answer exercise 9-5 in the text.
Since each node has a (possibly nil) left and right subtree, it is convenient to represent each node as a three-item list:
A binary search tree

As shown, the data for a node is kept in the first list item, while the second and third list items contain pointers to the left and right subtrees, respectively.

In addition, it is convenient for an external pointer to designate a cons cell -- as every other list node also is specified from a cons cell. As an example, the following logical tree

A binary search tree

would be represented by the following structure in Scheme.
A binary search tree

As a list, this might be represented as:

((5
    (2 
        (3 () ())
        ())
    (8
        ()
        (11 () ()))
))
  1. Following this pattern, draw a list structure for the tree given at the beginning of this lab.
File ~walker/153/labs/bst.ss contains code for a simple binary-search tree class, with implemented operations empty?, insert!, and print-all. The bst class also has unimplemented operations for count, find and print-leaves.
  1. Check that this code runs appropriately with the following commands, which are based on the tree at the start of this lab.
    (load "~walker/153/labs/bst.ss")
    (define maple (bst))
    (maple 'empty?)
    (maple 'insert! 123)
    (maple 'empty?)
    (maple 'insert! 37)
    (maple 'insert! 23)
    (maple 'insert! 48)
    (maple 'insert! 96)
    (maple 'insert! 285)
    (maple 'insert! 185)
    (maple 'insert! 200)
    (maple 'insert! 309)
    (maple 'print-all)
    (maple 'find 48)
    (maple 'count)
    (maple 'print-leaves)
    
The insert! procedure moves down the tree, moving left or right as dictated by the value of a new item, until it reaches the bottom of the tree. When a null position is found, a new node is created to insert a new node containing the new value.
  1. Follow a similar approach to implement the find operation.
The print-all procedure displays data from all nodes in the tree by recursively printing, for each node, the left subtree, the data at the node, and the right subtree. This is called an in-order traversal of the tree. For this procedure, spacing is added to indicate various levels of data within the tree. An asterisk * is printed at each null tree.

  1. An in-order traversal of a binary search tree always prints the data in order. Explain briefly why this should result.

  2. Use ideas from print-all to fill in the details for the print-leaves operation, which prints just the leaves within a tree. Here, one can still traverse the full tree -- but printing should occur only if a node has only null left and right subtrees.

  3. Ideas from print-all also can be used to count the number of (non null) nodes in a tree. Complete the details of the count operation.
Extra Credit:
  1. The height of a tree is the maximum number of levels of nodes within the tree; by convention, the height of a tree with only one node is 0. Thus, the first tree shown in the lab (with root 123) has height 3, while the next tree in this lab (with root 5) has height 2. Add to the bst class an operation height which computes the height of a tree.
Work to be turned in:


This document is available on the World Wide Web as

http://www.math.grin.edu/~walker/courses/153/lab-bst.html

created April 18, 1998
last revised April 24, 1998