Table of Contents

Corning Community College

CSCS2320 Data Structures

~~TOC~~

Project: DLT0

Errata

This section will document any updates applied to the project since original release:

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 minimum of two connections- often these are called 'binary' trees. We will use that description interchangeably here with 'doubly linked trees'.

The premise behind a tree is that of a parent-child relationship. Each node can be a parent (of two child nodes), and each child can in turn be its own parent (of 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.

A slight Node modification

With the tree project, we see a modification to our venerable node struct, used consistently and throughout our adventures this semester.

We keep all the existing content (and changes) that have endured up to this point, and we merely add one additional member: an other pointer that can see use in our iterative and stack-based implementations of various tree operations.

This will also mean mild tweaks are needed to the node library functions, and updated unit tests that will need to be verified.

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:

As we will be placing nodes in the tree according to their stored values (lesser values go to the left, or previous; greater values go to the right, or next), our tree will naturally sort our data for us.

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.

In inc/node.h

1
#ifndef _NODE_H
#define _NODE_H
 
#include <stdlib.h>
 
struct node {
        char         data;
        struct node *other;
        struct node *prev;
        struct node *next;
};
typedef struct node Node;
 
Node *mknode(char  );     // allocate new node containing value
Node *rmnode(Node *);     // deallocate node
Node *cpnode(Node *);     // duplicate node
 
#endif

In inc/tree.h

1
#ifndef _TREE_H
#define _TREE_H
 
#include "list.h"
 
#define  RECURSIVE     0
#define  STACK_BASED   1
#define  ITERATIVE     2
 
#define  INORDER       0
#define  PREORDER      1
#define  POSTORDER     2
 
typedef struct tree Tree;
struct tree {
    Node *root;                     // pointer to root node of tree
    char  max_height;               // max height of tree (0 = unlimited)
    char  height;                   // current height of tree
    Tree *(*copy)(Tree *         ); // copy tree (function pointer)
    Tree *(*grab)(Tree *, Node **); // grab node (function pointer)
    List *(*walk)(Tree *, char   ); // walk tree (function pointer)
};
 
Tree *mktree    (char           );  // create new tree
 
Tree *set_mode  (Tree *, char   );  // set tree mode (recurse, stack, iter)
 
Tree *addnode   (Tree *, Node * );  // add given node to tree
 
Tree *copytree_s(Tree *         );  // copy/duplicate given tree (stack-based)
Tree *copytree_r(Tree *         );  // copy/duplicate given tree (recursion)
Tree *copytree_i(Tree *         );  // copy/duplicate given tree (iterative)
 
Node *searchtree(Tree *, char   );  // find node in tree
 
Tree *grabnode_s(Tree *, Node **);  // stack-based grabnode
Tree *grabnode_r(Tree *, Node **);  // recursive grabnode
Tree *grabnode_i(Tree *, Node **);  // iterative grabnode
 
List *traverse_s(Tree *, char   );  // stack-based traverse (in mode)
List *traverse_r(Tree *, char   );  // recursive traverse (in mode)
List *traverse_i(Tree *, char   );  // iterative traverse (in mode)
 
Tree *rmtree    (Tree *         );  // purge and deallocate tree
 
Tree *balance   (Tree *         );  // rebalanced tree (root is midpoint)
 
void  print_tree(Tree *         );  // display tree structure (to STDOUT)
 
#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.

Of particular focus in this project, in addition to the important tree concepts, is that of implementation. Until now we've only bothered to just get it done and working… now, not only do we care about functionality, but we look more closely at how we got there (ie the three different implementation approaches).

Reference Implementation

As the layers and complexities rise, narrowing down the source of errors becomes increasingly important.

If your tree copytree_i() isn't working, is it because of a problem with your tree? Or might it be in an underlying node operation it relies upon?

To aid you in your development efforts, you now have the ability to import functioning node, list, and stack implementations into your project for the purposes of testing your tree functionality.

This also provides another opportunity for those who have fallen behind to stay current, as they can work on this project without having needed to successfully get full node or stack (and by extension- list) functionality.

Using the test reference implementation

You'll notice that, upon running make help in the base-level Makefile, the following new options appear (about halfway in the middle):

**                                                                    **
** make use-test-reference  - use working implementation object files **
** make use-your-own-code   - use your node/list implementation code  **
**                                                                    **

In order to make use of it, you'll need to run make use-test-reference from the base of your dlt0 project directory, as follows:

lab46:~/src/data/dlt0$ make use-test-reference
...
NODE, LIST, and STACK reference implementation in place, run 'make' to build everything.
lab46:~/src/data/dlt0$ 

You'll see that final message indicating everything is in place (it automatically runs a make clean for you), and then you can go ahead and build everything with it:

lab46:~/src/data/dlt0$ make
...

Debugging: When using the test reference implementation, you will not be able to debug the contents of the node, list, and stack functions (the files provided do not have debugging symbols added), so you'll need to take care not to step into these functions (it would be just like stepping into printf(). You can still compile the project with debugging support and debug (as usual) those compiled functions (ie the stack functions).

Reverting back to using your code

If you were trying out the reference implementation to verify tree functionality, and wanted to revert back to your own code, it is as simple as:

lab46:~/src/data/dlt0$ make use-your-own-code
Local node/list/stack implementation restored, run 'make clean; make' to build everything.
lab46:~/src/data/dlt0$ 

Node Library unit tests

As a result of the addition of other, an updated set of unit tests will be released (this will mean a different number of total tests for list than previously).

Be sure to run the various node unit tests and verification scripts to see which functions have fallen out of compliance with the node struct specification changes issued in this project. The verify-node.sh script can be especially useful in getting a big picture view of what work is needed.

Tree library unit tests

In testing/tree/unit/, you will eventually find these files:

NOTE: At the time of project release, unit tests are not yet available; so they will be released as they are developed.

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:

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):

node library

Here is what you should get for node:

lab46:~/src/data/dlt0$ bin/verify-node.sh 
====================================================
=    Verifying Doubly-Linked Node Functionality    =
====================================================
  [mknode] Total:   6, Matches:   6, Mismatches:   0
  [cpnode] Total:   8, Matches:   8, Mismatches:   0
  [rmnode] Total:   2, Matches:   2, Mismatches:   0
====================================================
 [RESULTS] Total:  16, Matches:  16, Mismatches:   0
====================================================
lab46:~/src/data/dlt0$ 

With the addition of the other pointer, we have a few additional combinations to test.

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 Criteria

To be successful in this project, the following criteria must be met: