Corning Community College
CSCS2320 Data Structures
~~TOC~~
======Project: Doubly-Linked Trees (dlt0)======
=====Errata=====
This section will document any updates applied to the project since original release:
* __revision 1__: typo in rmtree unit test. FIXED. (20161117)
* __revision 2__: typo in mktree unit test. FIXED. (20161117)
* __revision 3__: typo in grabnode unit test. FIXED. (20161202)
=====Objective=====
In this project, we continue our conceptual journey and explore yet another data structure: trees.
=====Background=====
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.
====conceptualizing a tree====
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====
The tree data structure presents certain advantages that encourages its use in solving problems, and we accomplish that by its compositional definition:
* a tree has a **root** which forms the basis or starting point of the structure; all nodes are reachable (and related through means of descendance) from the root.
* to put an item on the tree, we insert or **add** it. When we add a node to the tree, it gets positioned in the appropriately available child link off some parent node.
* to get an item off of the tree, we **grab** it (my term- often times it is referred to as deleting a node from the tree). Just as with a list and its obtain action, disconnecting a node from the structure can involve a lot of work to maintain connections and overall integrity of the data structure.
* an important action that will be taking place is the traversal of the tree- ie walking it in certain orders to achieve desired results (sorting/ordering of values, among other things).
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//.
====traversal====
There are 3 common methods of tree traversal we will explore with respect to trees.
Our definitions may differ slightly from definitions found elsewhere, but that's okay! Just a little abstraction at play.
The three methods of traversal are:
* preorder: back, parent, there
* postorder: there, parent, back
* inorder: parent, back, there
====tree height can matter====
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.
=====Project Overview=====
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.
====In inc/data.h====
Here we see some additional status code defines:
//////////////////////////////////////////////////////////////////////
//
// Status codes for the doubly linked tree implementation
//
#define DLT_SUCCESS 0x0000010000000000
#define DLT_CREATE_FAIL 0x0000020000000000
#define DLT_NULL 0x0000040000000000
#define DLT_EMPTY 0x0000080000000000
#define DLT_MAX 0x0000100000000000
#define DLT_ERROR 0x0000200000000000
#define DLT_INVALID 0x0000400000000000
#define DLT_DEFAULT_FAIL 0x0000800000000000
I also created the typedefs **sc** and **uc** to simplify the declaration of **signed char**s and **unsigned char**s (respectively). These can me seen in the typedef stanza.
====In inc/tree.h====
#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)
};
code_t mktree (Tree **, uc ); // create new tree
code_t cptree (Tree *, Tree ** ); // copy given tree
code_t rmtree (Tree ** ); // purge and deallocate tree
code_t addnode (Tree **, Node * ); // add given node to tree
code_t grabnode (Tree **, Node ** ); // get node from tree
code_t traverse_i(Tree *, List **,uc); // traverse tree by mode (iterative)
code_t traverse_r(Tree *, List **,uc); // traverse tree by mode (recursive)
code_t traverse_s(Tree *, List **,uc); // traverse tree by mode (stacks)
code_t searchtree(Tree *, Node **,sc); // find node in tree (by value)
#endif
====tree library====
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.
===Suggested Implementation Order===
To make your life easier, I recommend implementing tree library functions in the following order:
- **mktree()**
- **addnode()**
- **traverse_*()**
* iterative will likely be most familiar
* recursive will be algorithmically exotic but smaller
* stack-based will help with your stack fluency
- then the others...
* obviously, you'll want **grabnode()** BEFORE doing **cptree()**
====Tree library unit tests====
In **unit/tree/**, you will //eventually// find these files:
* **unit-addnode.c** - unit test for the various **addnode()** library function
* **unit-traverse.c** - unit test for **traverse_s()** library function
**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:
* the source code to each of these unit tests
* the purpose of these programs is to validate the correct functionality of the respective library functions
* follow the logic
* make sure you understand what is going on
* ask questions to get clarification!
* the output from these programs once compiled and ran
* analyze the output
* make sure you understand what is going on
* ask questions to get clarification!
=====Expected Results=====
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):
====tree library====
Here is what you should get for tree:
lab46:~/src/data/dlt0$ bin/verify-tree.sh
coming soon
lab46:~/src/data/dlt0$
=====Submission=====
{{page>haas:fall2016:common:submitblurb#DATA&noheader&nofooter}}