Corning Community College
CSCS2320 Data Structures
~~TOC~~
This section will document any updates applied to the project since original release:
In this project, we continue our conceptual journey and explore yet another data structure: trees.
A tree is our first foray into the realm of non-linear data structures. While it still consists of nodes, and those nodes are linked together, they are not linked together in a way providing a contiguous succession as we have in a list.
Truth be told, the simplest tree is merely a list- the underlying data structure we've worked with all semester. But now, under this new light, we can start to look at a list as merely a procession of singled childed nodes. The more things are different, the more they are the same…
The simplest conceptual tree is one which has a maximum of two connections- often these are called 'binary' trees. We will use that description interchangeably here with 'doubly linked trees'.
The premise behind a binary tree is that of a parent-child relationship. Each node can be a parent (of up to two child nodes), and each child can in turn be its own parent (of up to two child nodes). This creates a connection between relationships of nodes (ie a parent and its descendants), and ends up offering some interesting and useful algorithmic opportunities, along with implementation experience.
It is common to think of a tree as a vertical structure, upside-down from the item we commonly think of in nature. Here we will have the tree's “root” at the very top, and all the branches (nodes, and connections to nodes) growing down and out from the root.
The tree data structure presents certain advantages that encourages its use in solving problems, and we accomplish that by its compositional definition:
As we will be placing nodes in the tree according to their stored values (lesser values go to the left, or prior; greater values go to the right, or after), our tree will naturally sort our data for us, allowing us to search through its contents quite advantageously. As such, the tree we will be implementing is also known as a binary search tree.
For some operations (specifically, visualization), the height of the tree can be very important, and constraining the tree height can impose a nice upper bound on the total number of nodes put into the tree.
Just as with the stack's size and the queue's buffer, our tree will have the ability to set a max_height, which our adding logic needs to respect.
For this project, we're going to be implementing the tree data structure utilizing nodes. Isn't it neat how we've been able to use this same basic structure in so many different ways and configurations? That speaks to what we're exploring in Data Structures- ideas. It isn't as much about WHAT we're manipulating (nodes), but HOW.
Here we see some additional status code defines:
// Status codes for the tree implementation // #define DLT_SUCCESS 256 #define DLT_CREATE_FAIL 512 #define DLT_NULL 1024 #define DLT_EMPTY 2048 #define DLT_MAX 4096 #define DLT_DEFAULT_FAIL 16384 #define DLT_FAIL 32768
I also created the typedefs sc and uc to simplify the declaration of signed chars and unsigned chars (respectively). These can me seen just above the bottom of the file.
#ifndef _TREE_H #define _TREE_H #include "data.h" #include "list.h" #define INORDER 0 #define PREORDER 1 #define POSTORDER 2 struct tree { Node *root; // pointer to root node of tree uc max_height; // max height of tree (0 = unlimited) }; typedef struct tree Tree; code_t mktree (Tree **, uc ); // create new tree code_t cptree_r (Tree *, Tree ** ); // copy given tree (recursive) code_t rmtree (Tree ** ); // purge and deallocate tree code_t addnode (Tree **, Node * ); // add given node to tree code_t grabnode_r(Tree **, Node ** ); // get node from tree (recursive) code_t traverse_r(Tree *, List **,uc); // traverse tree by mode (recursive) code_t searchtree(Tree *, Node **,sc); // find node in tree (by value) #endif
In src/tree/, you will find skeletons of the above prototyped functions, hollowed out in anticipation of being made operational.
Figure out what is going on, the connections, and make sure you understand it.
To make your life easier, I recommend implementing tree library functions in the following order:
You will really want the later functions to make use of the earlier functions to facilitate implementation (ie cptree_r() will likely want to make use of traverse_r() and addnode(), and rmtree() could take advantage of traverse_r() and searchtree() and grabnode_r()).
Note that grabnode_r() can be implemented earlier, as it only really needs mktree(), but you may find its implementation more involved.
In testing/tree/unit/, you will eventually find these files:
NOTE: At the time of project release, not all unit tests are available/fully functional; so expect some updates.
There are also corresponding verify-FUNCTION.sh scripts that will output a “MATCH”/“MISMATCH” to confirm overall conformance with the pertinent tree functionality.
These are complete runnable programs (when compiled, and linked against the tree library, which is all handled for you by the Makefile system in place).
Of particular importance, I want you to take a close look at:
To assist you in verifying a correct implementation, a fully working implementation of the node and tree libraries should resemble the following (when running the respective verify script):
Here is what you should get for tree:
lab46:~/src/data/dlt0$ bin/verify-tree.sh coming soon lab46:~/src/data/dlt0$
To be successful in this project, the following criteria must be met: