User Tools

Site Tools


haas:fall2019:data:projects:dlt0

Corning Community College

CSCS2320 Data Structures

Project: Doubly-Linked Trees (dlt0)

Errata

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

  • revision #: <description> (DATESTAMP)

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 single 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: left, parent, right
  • postorder: right, parent, left
  • inorder: parent, left, right

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:

105
//////////////////////////////////////////////////////////////////////
//
// 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 chars and unsigned chars (respectively). These can me seen in the typedef stanza.

In inc/tree.h

1
#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:

  1. mktree()
  2. addnode()
  3. traverse_*()
    • iterative will likely be most familiar
    • recursive will be algorithmically exotic but smaller
    • stack-based will help with your stack fluency
  4. 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

Project Submission

When you are done with the project and are ready to submit it, you simply run make submit:

lab46:~/src/data/PROJECT$ make submit
...

Submission Criteria

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

  • Project must be submit on time, by the posted deadline.
    • Early submissions will earn 1 bonus point per full day in advance of the deadline.
      • Bonus eligibility requires an honest attempt at performing the project (no blank efforts accepted)
    • Late submissions will lose 33% credit per day, with the submission window closing on the 3rd day following the deadline.
      • To clarify: if a project is due on Wednesday (before its end), it would then be 33% off on Thursday, 66% off on Friday, and worth 0% once it becomes Saturday.
      • Certain projects may not have a late grace period, and the due date is the absolute end of things.
  • all requested functions must be implemented in the related library
  • all requested functionality must conform to stated requirements (either on this project page or in comment banner in source code files themselves).
  • Output generated must conform to any provided requirements and specifications (be it in writing or sample output)
    • output obviously must also be correct based on input.
  • Processing must be correct based on input given and output requested
  • Project header files are NOT to be altered. During evaluation the stock header files will be copied in, which could lead to compile-time problems.
  • Code must compile cleanly.
    • Each source file must compile cleanly (worth 3 total points):
      • 3/3: no compiler warnings, notes or errors.
      • 2/3: one of warning or note present during compile
      • 1/3: two of warning or note present during compile
      • 0/3: compiler errors present (code doesn't compile)
  • Code must be nicely and consistently indented (you may use the indent tool)
    • You are free to use your own coding style, but you must be consistent
    • Avoid unnecessary blank lines (some are good for readability, but do not go overboard- double-spacing your code will get points deducted).
    • Indentation will be rated on the following scale (worth 3 total points):
      • 3/3: Aesthetically pleasing, pristine indentation, easy to read, organized
      • 2/3: Mostly consistent indentation, but some distractions (superfluous or lacking blank lines, or some sort of “busy” ness to the code)
      • 1/3: Some indentation issues, difficult to read
      • 0/3: Lack of consistent indentation (didn't appear to try)
  • Unless fundamentally required, none of your code should perform any inventory or manual counting. Basing your algorithms off such fixed numbers complicates things, and is demonstrative of a more controlling nature.
  • Code must be commented
    • Any “to be implemented” comments MUST be removed
      • these “to be implemented” comments, if still present at evaluation time, will result in points being deducted.
    • Commenting will be rated on the following scale (worth 3 total points):
      • 3/3: Aesthetically pleasing (comments aligned or generally not distracting), easy to read, organized
      • 2/3: Mostly consistent, some distractions or gaps in comments (not explaining important things)
      • 1/3: Light commenting effort, not much time or energy appears to have been put in.
      • 0/3: No original comments
      • should I deserve nice things, my terminal is usually 90 characters wide. So if you'd like to format your code not to exceed 90 character wide terminals (and avoid line wrapping comments), at least as reasonably as possible, those are two sure-fire ways of making a good impression on me with respect to code presentation and comments.
    • Sufficient comments explaining the point of provided logic MUST be present
  • Code must be appropriately modified
    • Appropriate modifications will be rated on the following scale (worth 3 total points):
      • 3/3: Complete attention to detail, original-looking implementation- also is not unnecessarily reinventing existing functionality
      • 2/3: Lacking some details (like variable initializations), but otherwise complete (still conforms, or conforms mostly to specifications), and reinvents some wheels
      • 1/3: Incomplete implementation (typically lacking some obvious details/does not conform to specifications)
      • 0/3: Incomplete implementation to the point of non-functionality (or was not started at all)
    • Implementation must be accurate with respect to the spirit/purpose of the project (if the focus is on exploring a certain algorithm to produce results, but you avoid the algorithm yet still produce the same results– that's what I'm talking about here).. worth 3 total points:
      • 3/3: Implementation is in line with spirit of project
      • 2/3: Some avoidance/shortcuts taken (note this does not mean optimization– you can optimize all you want, so long as it doesn't violate the spirit of the project).
      • 1/3: Generally avoiding the spirit of the project (new, different things, resorting to old and familiar, despite it being against the directions)
      • 0/3: entirely avoiding.
    • Error checking must be adequately and appropriately performed, according to the following scale (worth 3 total points):
      • 3/3: Full and proper error checking performed for all reasonable cases, including queries for external resources and data.
      • 2/3: Enough error checking performed to pass basic project requirements and work for most operational cases.
      • 1/3: Minimal error checking, code is fragile (code may not work in full accordance with project requirements)
      • 0/3: No error checking (code likely does not work in accordance with project requirements)
  • Any and all non-void functions written must have exactly 1 return statement
    • points will be lost for solutions containing multiple return statements in a function.
  • Absolutely, positively NO (as in ZERO) use of goto statements.
    • points will most definitely be lest for solutions employing such things.
  • No custom global variables! The header files provide all you need.
    • Do NOT edit the header files.
  • Track/version the source code in a repository
  • Filling out any submit-time questionnaires
  • Submit a copy of your source code to me using the submit tool (make submit will do this) by the deadline.
haas/fall2019/data/projects/dlt0.txt · Last modified: 2018/11/12 11:13 by 127.0.0.1