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.
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 has been started for you. The following components have been provided for you...
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+
Download and edit the following file: lab6_report.txt. Then submit your editted file on Blackboard in the Lab 6 submission area.