CS-220 Spring 2016 Lab 6 Binary Trees

Background

A binary tree is a data structure that consists of a series of nodes. Each node in the tree may have a left sub-tree and/or a right sub-tree. We typically think of a tree upside down, so the "root" node is at the top of the tree. The root node is not a sub-tree of any other node. The nodes at the bottom of the tree do not have either a left sub-tree or a right sub-tree, and are considered "leaves" of the tree. See Wikipedia: Binary Tree for more details.

For this lab, we will use a binary tree to sort a list of numbers. You will be given a list of numbers that are not sorted. Each number in the list will get inserted into the binary tree. As we insert into the tree, we will enforce a rule which states that for any sub-tree of our binary tree, the root of the sub-tree has an arbitrary value, call it "r", and the value of the root of the left sub-tree of the root is less than "r" (if there is a left sub-tree), and the value of the right sub-tree of the root is greater than or equal to "r" (if there is a right sub-tree).

Inserting a new number into such a tree is fairly simple... If the tree is empty, make the new number the root of the tree, with no left or right sub-trees. If the tree is not empty, then compare the new number with the value at the root node. If the new number is less than the root, and the root does not have a left sub-tree, make the new number into a leaf node, and make that the left sub-tree of the root. If the root already has a left sub-tree, insert the new number into the left sub-tree using this same algorithm. If the new number is not less than the root, then do the same process, but with the right sub-tree of the root.

We describe a tree using a tree node data structure, and provide functions to create new nodes, get the value of a node, and to get and the left and right sub-trees of the node. Those details are described next.

Getting Started

Download lab6.tar.gz and untar it (I use the command tar -xvzf lab6.tar.gz.) This makes a new lab6 subdirectory of your current directory. There should be two files in the lab6 subdirectory, one called Makefile, and the program file, btree.c.

The Makefile is a standard Makefile. This builds the command btree, and has a test and a clean target.

The btree.c File

The btree.c file has been started for you. The following components have been provided for you...

Working the lab

Start by running make test to build and execute the code as delivered. Note that we are creating a valid single node tree from the first argument, and passing that into functions such as "insertVal", "prLNR", etc., but since these functions are not implemented yet, we don't do anything with them.

Edit btree.c, and start working on the insertTree function. This function should insert a node with the value as specified in the argument into tree t. Tree t must have at least a root node. The insertion should preserve the rule that values in left sub-trees are less than the value in the root node, and values in the right sub-trees are greater than or equal to the value in the root node. It's easiest to implement this recursively. In other words, if the argument is less than the value of the root, and the root already has a left sub-tree, then insertTree can invoke itself with the argument value, but passing in t->left as the tree in which to insert the argument value.

Once you have the insertTree function working, start working on one of the print functions. These are pretty simple if you invoke them recursively as well. Just remember that in your recursive invocations, you need to be sure you are passing in a non-null pointer for the tree argument.

The printTree function may be a little bit more challinging than the others. This function should print the tree in some graphical fashion that makes it clear what the tree looks like. The professor came up with a fairly simple implementation of this function that produces the following output (given the arguments to btree specified in the Makefile under the "test" target.)

Graphic Representation...
  |  |52+
  |  |  |  |34
  |  |  |22+
  |  |  |  |22
  |21+
  |  |18+
  |  |  |  |18
  |  |  |17+
  |  |  |  |  |  |16
  |  |  |  |  |14+
  |  |  |  |12+
10+

  

Hints

  1. Working on Binary trees is accomplished most easily with functions that can be invoked recursively. For instance, if you are inserting a value, and you figure out that the value has to get inserted in the left sub-tree, and the left sub-tree exists, then you can invoke insertValue(t->left,value).
  2. Write code in small chunks... build, test, and debug each small chunk of code. Run gdb and step through your code to make sure it's doing what you expect it to do.
  3. Have fun and be creative!

Lab Report

Download and edit the following file: lab6_report.txt. Then submit your editted file on Blackboard in the Lab 6 submission area.