Binary Search Tree - Project Report #2 | CS 2605, Study Guides, Projects, Research of Data Structures and Algorithms

Material Type: Project; Class: Data Structs & OO Development; Subject: Computer Science; University: Virginia Polytechnic Institute And State University; Term: Unknown 1989;

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 02/13/2009

koofers-user-ftn-1
koofers-user-ftn-1 🇺🇸

8 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 2605 Project 2 Spring 2008
3/28/2008 1:39 PM 1
Binary Search Tree
This assignment involves implementing a standard binary search tree as a C++ template. Because this assignment will be
auto-graded using a test harness I will provide, your implementation must conform to the public interfaces below, and include
at least the private members that are shown:
// BinNodeT.h
// . . . includes omitted
template <typename T> class BinNodeT {
public:
T Element; // client data value
BinNodeT<T>* Left; // access to left child, if any
BinNodeT<T>* Right; // access to right child, if any
BinNodeT();
BinNodeT(T newData, BinNodeT<T>* newLeft = NULL,
BinNodeT<T>* newRight = NULL);
bool isLeaf() const; // useful for navigation in member fns
~BinNodeT(); // doesn't really have much to do...
};
// BST.h
// . . . includes omitted
template <typename T> class BST {
private:
BinNodeT<T>* Root; // access to root node, if any
// . . . the rest is up to you . . .
public:
BST(); // create empty BST
BST(const T& D); // create one-node BST with given data
BST(const BST<T>& Source); // deep copy support
BST<T>& operator=(const BST<T>& Source);
bool Insert(const T& D); // insert data into new node
bool Delete(const T& D); // delete node with matching data
T* const Find(const T& D); // return pointer to matching data,
// or NULL if not found
const T* const Find(const T& D) const; // same, but read-only access
void Display(ostream& Out) const; // write formatted tree contents
void Clear(); // reset tree to empty state
~BST (); // destroy all tree contents
friend class Monk; // give the test harness access
};
You may safely add features to the given interface, but if you omit or modify members of the given interface you will almost
certainly face compilation errors when you submit your implementation for testing. In particular, you may safely derive your
BST from a general tree class, as long as you do in fact implement the specified functionality.
You must place the declaration of your BST template in a header file named BST.h because Monk.h (one of the test harness
files) will explicitly #include that file. You may add iterator support, but it will not be tested. Note that if you do add an
iterator, or six, you must still have Find(), Insert(), and Delete() functions that conform exactly to the interfaces
given above.
pf3
pf4

Partial preview of the text

Download Binary Search Tree - Project Report #2 | CS 2605 and more Study Guides, Projects, Research Data Structures and Algorithms in PDF only on Docsity!

Binary Search Tree

This assignment involves implementing a standard binary search tree as a C++ template. Because this assignment will be auto-graded using a test harness I will provide, your implementation must conform to the public interfaces below, and include at least the private members that are shown:

// BinNodeT.h //... includes omitted

template class BinNodeT { public: T Element; // client data value BinNodeT* Left; // access to left child, if any BinNodeT* Right; // access to right child, if any

BinNodeT(); BinNodeT(T newData, BinNodeT* newLeft = NULL, BinNodeT* newRight = NULL);

bool isLeaf() const; // useful for navigation in member fns ~BinNodeT(); // doesn't really have much to do... };

// BST.h //... includes omitted

template class BST { private: BinNodeT* Root; // access to root node, if any //... the rest is up to you...

public: BST(); // create empty BST BST(const T& D); // create one-node BST with given data BST(const BST& Source); // deep copy support BST& operator=(const BST& Source);

bool Insert(const T& D); // insert data into new node bool Delete(const T& D); // delete node with matching data T* const Find(const T& D); // return pointer to matching data, // or NULL if not found const T* const Find(const T& D) const; // same, but read-only access void Display(ostream& Out) const; // write formatted tree contents void Clear(); // reset tree to empty state

~BST (); // destroy all tree contents

friend class Monk; // give the test harness access };

You may safely add features to the given interface, but if you omit or modify members of the given interface you will almost certainly face compilation errors when you submit your implementation for testing. In particular, you may safely derive your BST from a general tree class, as long as you do in fact implement the specified functionality.

You must place the declaration of your BST template in a header file named BST.h because Monk.h (one of the test harness files) will explicitly #include that file. You may add iterator support, but it will not be tested. Note that if you do add an iterator, or six, you must still have Find(), Insert(), and Delete() functions that conform exactly to the interfaces given above.

Design and implementation requirements

There are some explicit requirements, in addition to those on the Programming Standards page of the course website:

 You must implement C++ templates for the BST itself and also for the BST nodes, conforming to the given interface.  You must handle copy issues correctly. We will certainly test for this. Failures in your copy logic will almost certainly result in program crashes and scores of zero.  You may assume that any data type stored in your template will handle its own copy issues correctly (as it must).  You may assume that any data type stored in your template will provide operator<<().  You may assume that any data type stored in your template will support all the relational operators.  You must properly allocate and deallocate memory, as needed.  The insertion logic must not allow duplicate records to be inserted.  You must provide feedback to the client when an operation fails. The given prototypes of the member functions indicate where that is necessary. Under no circumstances should any of the template functions, other than Display() and its helper, write output.

The friend declaration provides a class that I will supply with my test harness privileged access to the internals of your BST object. You should not derive your BST from another template.

The Display() function must write the contents of the BST in the following format:






5





The hyphens identify the level in the tree at which a value is stored. Note that you must produce exactly three hyphens for each level down a value is in the tree. Each line of the display should end with a newline character. You also must not embed any blank lines or extra whitespace within the tree display. If the tree is empty, display the string "Tree is empty" on a line by itself. If you don't follow these instructions, the automated evaluation will conclude that your tree is not structured correctly.

Testing:

I will be testing your implementation with my own test driver. I may (or may not) release information about that driver before the assignment is due. In any case, it is your responsibility to design and carry out a sensible test of your implementation before submitting it. For that purpose, you may share test code (but absolutely no tree code!!) via the class Forum.

Be warned that your copy logic will be tested thoroughly, as will the operation of the destructor. Be sure you test all of the interface elements thoroughly, both in isolation and in interleaved fashion.

Also be sure you read and understand the test build instructions that will be posted on the website. The default build you would get if you're developing under Eclipse or KDevelop or .NET will often mask even serious errors in an implementation.

Note on deletion:

The test harness will expect you to handle deletion in a precisely-specified manner. Deletion of a leaf simply requires deallocating the node and setting the pointer to that node to NULL. Deletion of a node with one non-empty subtree simply requires setting the pointer to that node to point to the node’s subtree, and then deallocating the node.

Deletion of a node with two non-empty subtrees must be handled in the following manner. First, locate the node rMin that holds the minimum value in the right subtree of the node to be deleted. Then, remove rMin from the right subtree using the appropriate logic for a leaf or a one-subtree node. Next, replace the node to be deleted with rMin, by resetting pointers as necessary. Finally, deallocate the node that was to be deleted.

This technique is a modification of the delete-by-merging described in Drozdek and addresses the issue of increasing the height of the tree that his algorithm would suffer. It should be cheaper, on average, than the delete-by-copy technique he describes, since the data objects stored in the tree are likely to be much larger than a few pointers.