Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Programming. Please only answer if you can do the ENTIRE problem. DO NOT MAK

ID: 3853195 • Letter: C

Question

C++ Programming. Please only answer if you can do the ENTIRE problem. DO NOT MAKE YOUR OWN CPP FILES, VARIABLES, ETC. UPLOAD ALL OF THE EXACT .CPP AND .H FILES AS I HAVE THEM JUST COMPLETED. Reattach all 5 files I have below even if you didn't have to edit one. Program MUST compile and all these insructions followed in order for me to give a thumbs up.

To Do:

- using the linked approach implement the BST ADT, implement all the functions in the BSTree.cpp. (60 points)

- use recursive functions to traverse the tree - read the implementation notes on using helper functions.

- Programming Exercise 2 (20 points)

- Programming Exercise 3 (20 points)

test9.cpp:

//--------------------------------------------------------------------

//

// Laboratory 9 test9.cpp

//

// Test program for the operations in the Binary Search Tree ADT

//

//--------------------------------------------------------------------

#include <iostream>

using namespace std;

#include "BSTree.cpp"

#include "config.h"

void print_help();

//--------------------------------------------------------------------

// Declaration for the binary search tree data item class

//--------------------------------------------------------------------

class TestData

{

public:

void setKey ( int newKey )

{ keyField = newKey; } // Set the key

int getKey () const

{ return keyField; } // Returns the key

private:

int keyField; // Key for the data item

};

int main()

{

BSTree<TestData,int> testTree; // Test binary search tree

TestData testData; // Binary search tree data item

int inputKey; // User input key

char cmd; // Input command

print_help();

do

{

testTree.showStructure(); // Output tree

cout << endl << "Command: "; // Read command

cin >> cmd;

if ( cmd == '+' || cmd == '?' ||

   cmd == '-' || cmd == '<' )

   cin >> inputKey;

switch ( cmd )

{

case 'P' : case 'p' :

   print_help();

   break;

case '+' : // insert

   testData.setKey(inputKey);

   cout << "Insert : key = " << testData.getKey()

<< endl;

   testTree.insert(testData);

   break;

case '?' : // retrieve

   if ( testTree.retrieve(inputKey,testData) )

cout << "Retrieved : getKey = "

   << testData.getKey() << endl;

   else

cout << "Not found" << endl;

   break;

case '-' : // remove

   if ( testTree.remove(inputKey) )

cout << "Removed data item" << endl;

   else

cout << "Not found" << endl;

   break;

case 'K' : case 'k' : // writeKeys

   cout << "Keys:" << endl;

   testTree.writeKeys();

   break;

case 'C' : case 'c' : // clear

   cout << "Clear the tree" << endl;

   testTree.clear();

   break;

case 'E' : case 'e' : // empty

   if ( testTree.isEmpty() )

cout << "Tree is empty" << endl;

   else

cout << "Tree is NOT empty" << endl;

   break;

#if LAB9_TEST1

case 'G' : case 'g' : // Programming Exercise 2

   cout << "Tree nodes count = " << testTree.getCount() << endl;

   break;

#endif   // LAB9_TEST1

#if LAB9_TEST2

case 'H' : case 'h' : // Programming Exercise 2

   cout << "Tree height = " << testTree.getHeight() << endl;

   break;

#endif   // LAB9_TEST2

#if LAB9_TEST3

case '<' : // Programming Exercise 3

   cout << "Keys < " << inputKey << " : " << endl;

   testTree.writeLessThan(inputKey);

   cout << endl;

   break;

#endif   // LAB9_TEST3

case 'Q' : case 'q' : // Quit test program

   break;

default : // Invalid command

   cout << "Inactive or invalid command. 'P' prints help." << endl;

}

}

while ( cin && ( cmd != 'Q' ) && ( cmd != 'q' ) );

  

if ( !cin ) {

   cerr << "Error in console input. Exiting." << endl;

}

return 0;

}

//--------------------------------------------------------------------

void print_help() {

cout << endl << "Commands:" << endl;

cout << " P : [P]rint Help (displays this message)" << endl;

cout << " +key : Insert (or update) data item (use integers)" << endl;

cout << " ?key : Retrieve data item" << endl;

cout << " -key : Remove data item" << endl;

cout << " K : Write keys in ascending order" << endl;

cout << " C : Clear the tree" << endl;

cout << " E : Empty tree?" << endl;

cout << " G : Get count of nodes "

#if LAB9_TEST1

   << "(Active : "

#else

   << "(Inactive : "

#endif

   << "In-lab Exercise 2)" << endl;

cout << " H : Height "

#if LAB9_TEST2

   << "(Active : "

#else

   << "(Inactive : "

#endif

   << "In-lab Exercise 2)" << endl;

cout << " <key : Write keys that are < key "

#if LAB9_TEST3

   << "(Active : "

#else

   << "(Inactive : "

#endif

   << "In-lab Exercise 3)" << endl;

cout << " Q : Quit the test program" << endl;

cout << endl;

}

show9.cpp:

#include "BSTree.h"

//--------------------------------------------------------------------

//

// Laboratory 9 show9.cpp

//

// Linked implementation of the showStructure operation for the

// Binary Search Tree ADT

//

//--------------------------------------------------------------------

//--------------------------------------------------------------------

template < typename DataType, typename KeyType >

void BSTree<DataType,KeyType>:: showStructure () const

// Outputs the keys in a binary search tree. The tree is output

// rotated counterclockwise 90 degrees from its conventional

// orientation using a "reverse" inorder traversal. This operation is

// intended for testing and debugging purposes only.

{

if ( root == 0 )

   cout << "Empty tree" << endl;

else

{

   cout << endl;

   showHelper(root,1);

   cout << endl;

}

}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

template < typename DataType, typename KeyType >

void BSTree<DataType,KeyType>:: showHelper ( BSTreeNode *p,

   int level ) const

// Recursive helper for showStructure.

// Outputs the subtree whose root node is pointed to by p.

// Parameter level is the level of this node within the tree.

{

   int j; // Loop counter

   if ( p != 0 )

   {

showHelper(p->right,level+1); // Output right subtree

for ( j = 0 ; j < level ; j++ ) // Tab over to level

cout << " ";

cout << " " << p->dataItem.getKey(); // Output key

if ( ( p->left != 0 ) && // Output "connector"

   ( p->right != 0 ) )

   cout << "<";

else if ( p->right != 0 )

   cout << "/";

else if ( p->left != 0 )

   cout << "\";

cout << endl;

showHelper(p->left,level+1); // Output left subtree

}

}

BSTree.cpp:

#include "BSTree.h"

template <typename DataType, class KeyType>

BSTree<DataType, KeyType>::BSTreeNode::BSTreeNode ( const DataType &nodeDataItem, BSTreeNode *leftPtr, BSTreeNode *rightPtr )

{

}

template < typename DataType, class KeyType >

BSTree<DataType, KeyType>::BSTree ()

{

   root = NULL;

}

template < typename DataType, class KeyType >

BSTree<DataType, KeyType>::BSTree ( const BSTree<DataType,KeyType>& other )

{

}

template < typename DataType, class KeyType >

BSTree<DataType, KeyType>& BSTree<DataType, KeyType>:: operator= ( const BSTree<DataType,KeyType>& other )

{

}

template < typename DataType, class KeyType >

BSTree<DataType, KeyType>::~BSTree ()

{

}

template < typename DataType, class KeyType >

void BSTree<DataType, KeyType>::insert ( const DataType& newDataItem )

{

}

template < typename DataType, class KeyType >

bool BSTree<DataType, KeyType>::retrieve ( const KeyType& searchKey, DataType& searchDataItem ) const

{

   return false;

}

template < typename DataType, class KeyType >

bool BSTree<DataType, KeyType>::remove ( const KeyType& deleteKey )

{

   return false;

}

template < typename DataType, class KeyType >

void BSTree<DataType, KeyType>::writeKeys () const

{

}

template < typename DataType, class KeyType >

void BSTree<DataType, KeyType>::clear ()

{

}

template < typename DataType, class KeyType >

bool BSTree<DataType, KeyType>::isEmpty () const

{

   return false;

}

template < typename DataType, class KeyType >

int BSTree<DataType, KeyType>::getHeight () const

{

   return -1;

}

template < typename DataType, class KeyType >

int BSTree<DataType, KeyType>::getCount () const

{

   return -1;

}

template < typename DataType, class KeyType >

void BSTree<DataType, KeyType>::writeLessThan ( const KeyType& searchKey ) const

{

}

#include "show9.cpp"

config.h:

/**

* BSTree class (Lab 9) configuration file.

* Activate test 'N' by defining the corresponding LAB9_TESTN to have the value 1.

* Deactive test 'N' by setting the value to 0.

*/

#define LAB9_TEST1   0       // Programming Exercise 2: getCount

#define LAB9_TEST2   0       // Programming Exercise 2: getHeight

#define LAB9_TEST3   0       // Programming Exercise 3: writeLessThan

BSTree.h:

//--------------------------------------------------------------------

//

// Laboratory 9 BSTree.h

//

// Class declarations for the linked implementation of the Binary

// Search Tree ADT -- including the recursive helpers of the

// public member functions

//

//--------------------------------------------------------------------

#ifndef BSTREE_H

#define BSTREE_H

#include <stdexcept>

#include <iostream>

using namespace std;

template < typename DataType, class KeyType > // DataType : tree data item

class BSTree // KeyType : key field

{

public:

// Constructor

BSTree (); // Default constructor

BSTree ( const BSTree<DataType,KeyType>& other ); // Copy constructor

BSTree& operator= ( const BSTree<DataType,KeyType>& other );

                          // Overloaded assignment operator

// Destructor

~BSTree ();

// Binary search tree manipulation operations

void insert ( const DataType& newDataItem ); // Insert data item

bool retrieve ( const KeyType& searchKey, DataType& searchDataItem ) const;

// Retrieve data item

bool remove ( const KeyType& deleteKey ); // Remove data item

void writeKeys () const; // Output keys

void clear (); // Clear tree

// Binary search tree status operations

bool isEmpty () const; // Tree is empty

// !! isFull() has been retired. Not very useful in a linked structure.

// Output the tree structure -- used in testing/debugging

void showStructure () const;

// In-lab operations

int getHeight () const; // Height of tree

int getCount () const;              // Number of nodes in tree

void writeLessThan ( const KeyType& searchKey ) const; // Output keys < searchKey

protected:

class BSTreeNode // Inner class: facilitator for the BSTree class

{

public:

  

// Constructor

BSTreeNode ( const DataType &nodeDataItem, BSTreeNode *leftPtr, BSTreeNode *rightPtr );

// Data members

DataType dataItem; // Binary search tree data item

BSTreeNode *left, // Pointer to the left child

   *right; // Pointer to the right child

};

// Recursive helpers for the public member functions -- insert

// prototypes of these functions here.

void insertHelper ( BSTreeNode *&p, const DataType &newDataItem );

bool retrieveHelper ( BSTreeNode *p, const KeyType& searchKey, DataType &searchDataItem ) const;

bool removeHelper ( BSTreeNode *&p, const KeyType& deleteKey );

// cutRightmose used in one implementation of remove.

void cutRightmost ( BSTreeNode *&r, BSTreeNode *&delPtr );

void writeKeysHelper ( BSTreeNode *p ) const;

void clearHelper ( BSTreeNode *p );

void showHelper ( BSTreeNode *p, int level ) const;

int getHeightHelper ( BSTreeNode *p ) const;

int getCountHelper ( BSTreeNode *p ) const;

void writeLTHelper ( BSTreeNode *p, const KeyType& searchKey ) const;

void copyTree ( const BSTree<DataType,KeyType> &otherTree );

void copyTreeHelper ( BSTreeNode *&p, const BSTreeNode *otherPtr );

// Data member

BSTreeNode *root; // Pointer to the root node

};

#endif   // define BSTREE_H

Explanation / Answer

test9.cpp

#include <iostream>
using namespace std;
#include "BSTree.cpp"
#include "config.h"

void print_help();

//--------------------------------------------------------------------
// Declaration for the binary search tree data item class
//--------------------------------------------------------------------

class TestData
{
public:

    void setKey ( int newKey )
        { keyField = newKey; }   // Set the key

    int getKey () const
        { return keyField; }     // Returns the key

private:

    int keyField;                // Key for the data item
};

int main()
{
    BSTree<TestData,int> testTree;   // Test binary search tree
    TestData testData;               // Binary search tree data item
    int inputKey;                    // User input key
    char cmd;                        // Input command

    print_help();

    do
    {
        testTree.showStructure();                     // Output tree

        cout << endl << "Command: ";                  // Read command
        cin >> cmd;
        if ( cmd == '+' || cmd == '?' ||
             cmd == '-' || cmd == '<'     )
           cin >> inputKey;

        switch ( cmd )
        {
          case 'P' : case 'p' :
               print_help();
               break;

          case '+' :                                  // insert
               testData.setKey(inputKey);
               cout << "Insert : key = " << testData.getKey()
                    << endl;
               testTree.insert(testData);
               break;

          case '?' :                                  // retrieve
               if ( testTree.retrieve(inputKey,testData) )
                  cout << "Retrieved : getKey = "
                       << testData.getKey() << endl;
               else
                  cout << "Not found" << endl;
               break;

          case '-' :                                  // remove
               if ( testTree.remove(inputKey) )
                  cout << "Removed data item" << endl;
               else
                  cout << "Not found" << endl;
               break;

          case 'K' : case 'k' :                       // writeKeys
               cout << "Keys:" << endl;
               testTree.writeKeys();
               break;

          case 'C' : case 'c' :                       // clear
               cout << "Clear the tree" << endl;
               testTree.clear();
               break;

          case 'E' : case 'e' :                       // empty
               if ( testTree.isEmpty() )
                  cout << "Tree is empty" << endl;
               else
                  cout << "Tree is NOT empty" << endl;
               break;

#if   LAB9_TEST1
          case 'G' : case 'g' :                   // Programming Exercise 2
               cout << "Tree nodes count = " << testTree.getCount() << endl;
               break;
#endif   // LAB9_TEST1

#if   LAB9_TEST2
          case 'H' : case 'h' :                   // Programming Exercise 2
               cout << "Tree height = " << testTree.getHeight() << endl;
               break;
#endif   // LAB9_TEST2

#if   LAB9_TEST3
          case '<' :                              // Programming Exercise 3
               cout << "Keys < " << inputKey << " : " << endl;
               testTree.writeLessThan(inputKey);
               cout << endl;
               break;
#endif   // LAB9_TEST3

          case 'Q' : case 'q' :                   // Quit test program
               break;

          default :                               // Invalid command
               cout << "Inactive or invalid command. 'P' prints help." << endl;
        }
    }
    while ( cin && ( cmd != 'Q' ) && ( cmd != 'q' ) );
  
    if ( !cin ) {
   cerr << "Error in console input. Exiting." << endl;
    }

    return 0;
}

//--------------------------------------------------------------------

void print_help() {
    cout << endl << "Commands:" << endl;
    cout << " P    : [P]rint Help (displays this message)" << endl;
    cout << " +key : Insert (or update) data item (use integers)" << endl;
    cout << " ?key : Retrieve data item" << endl;
    cout << " -key : Remove data item" << endl;
    cout << " K    : Write keys in ascending order" << endl;
    cout << " C    : Clear the tree" << endl;
    cout << " E    : Empty tree?" << endl;
    cout << " G    : Get count of nodes          "
#if LAB9_TEST1
         << "(Active   : "
#else
         << "(Inactive : "
#endif
         << "In-lab Exercise 2)" << endl;

    cout << " H    : Height                      "
#if LAB9_TEST2
         << "(Active   : "
#else
         << "(Inactive : "
#endif
         << "In-lab Exercise 2)" << endl;

    cout << " <key : Write keys that are < key   "
#if LAB9_TEST3
         << "(Active   : "
#else
         << "(Inactive : "
#endif
         << "In-lab Exercise 3)" << endl;
    cout << " Q    : Quit the test program" << endl;
    cout << endl;
}

show9.cpp

#include "BSTree.h"


//
// Linked implementation of the showStructure operation for the
// Binary Search Tree ADT
//


template < typename DataType, typename KeyType >
void BSTree<DataType,KeyType>:: showStructure () const

// Outputs the keys in a binary search tree. The tree is output
// rotated counterclockwise 90 degrees from its conventional
// orientation using a "reverse" inorder traversal. This operation is
// intended for testing and debugging purposes only.

{
    if ( root == 0 )
       cout << "Empty tree" << endl;
    else
    {
       cout << endl;
       showHelper(root,1);
       cout << endl;
    }
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

template < typename DataType, typename KeyType >
void BSTree<DataType,KeyType>:: showHelper ( BSTreeNode *p,
                               int level             ) const

// Recursive helper for showStructure.
// Outputs the subtree whose root node is pointed to by p.
// Parameter level is the level of this node within the tree.

{
     int j;   // Loop counter

     if ( p != 0 )
     {
        showHelper(p->right,level+1);         // Output right subtree
        for ( j = 0 ; j < level ; j++ )    // Tab over to level
            cout << " ";
        cout << " " << p->dataItem.getKey();   // Output key
        if ( ( p->left != 0 ) &&           // Output "connector"
             ( p->right != 0 ) )
           cout << "<";
        else if ( p->right != 0 )
           cout << "/";
        else if ( p->left != 0 )
           cout << "\";
        cout << endl;
        showHelper(p->left,level+1);          // Output left subtree
    }
}

BSTree.cpp

#include "BSTree.h"

template <typename DataType, class KeyType>
BSTree<DataType, KeyType>::BSTreeNode::BSTreeNode ( const DataType &nodeDataItem, BSTreeNode *leftPtr, BSTreeNode *rightPtr ): dataItem(nodeDataItem), left(leftPtr), right(rightPtr)
{
}

template < typename DataType, class KeyType >
BSTree<DataType, KeyType>::BSTree (): root(0)
{
   root = NULL;
}

template < typename DataType, class KeyType >
BSTree<DataType, KeyType>::BSTree ( const BSTree<DataType,KeyType>& other ): root(nullptr)
{
   root = clone(other.root, root);
}

template<typename DataType, class KeyType>
void BSTree<DataType, KeyType>::clone(BSTreeNode * p, BSTreeNode *& t)
{
   if (p == NULL)
       t = NULL;
   else
   {
       t = new ExprTreeNode(p->dataItem, 0, 0);
       clone(p->left, t->left);
       clone(p->right, t->right);
   }
}


template < typename DataType, class KeyType >
BSTree<DataType, KeyType>& BSTree<DataType, KeyType>:: operator= ( const BSTree<DataType,KeyType>& other )
{
   if (this != &other)
   {
       clear();
       clone(other.root, root);
   }
   return *this;
}

template < typename DataType, class KeyType >
BSTree<DataType, KeyType>::~BSTree ()
{
   clear();
}

template < typename DataType, class KeyType >
void BSTree<DataType, KeyType>::insert ( const DataType& newDataItem )
{
   insertHelper(root, newDataItem);
}

template < typename DataType, class KeyType >
void BSTree<DataType, KeyType>::insertHelper(BSTreeNode *&p,const DataType& newDataItem)
{
   if (p == 0)
       p = new BSTreeNode(newDataItem, 0, 0);
   else if (p->dataItem.getKey() > newDataItem.getKey()) //search left
       insertHelper(p->left, newDataItem);
   else if (p->dataItem.getKey() < newDataItem.getKey()) //search right
       insertHelper(p->right, newDataItem);
   else
       p->dataItem = newDataItem;
}

template < typename DataType, class KeyType >
bool BSTree<DataType, KeyType>::retrieve ( const KeyType& searchKey, DataType& searchDataItem ) const
{
   return retrieveHelper(root,searchKey,searchDataItem);
}
template < typename DataType, class KeyType >
bool BSTree<DataType, KeyType>::retrieveHelper(BSTreeNode *p,const KeyType& searchKey, DataType& searchDataItem) const
{
   bool result;

   if (p == 0)
       result = false;
   else if (searchKey < p->dataItem.getKey()) //search left
       result = retrieveHelper(p->left, searchKey, searchDataItem);
   else if (searchKey > p->dataItem.getKey()) //search right
       result = retrieveHelper(p->right, searchKey, searchDataItem);
   else
   {
       searchDataItem = p->dataItem;
       result = true;
   }
   return result;
}

template < typename DataType, class KeyType >
bool BSTree<DataType, KeyType>::remove ( const KeyType& deleteKey )
{
   return removeHelper(root,deleteKey);
}

template < typename DataType, class KeyType >
bool BSTree<DataType, KeyType>::removeHelper(BSTreeNode *&p,const KeyType& deleteKey)
{
   bool result;
   if (p == NULL)
       result = false;
   else if (deleteKey < p->dataItem.getKey()) //search left
           result = removeHelper(p->left, deleteKey);
   else if (deleteKey > p->dataItem.getKey()) //search right
           result = removeHelper(p->right, deleteKey);
   else
       {
       if (p->left == NULL && p->right == NULL)
       {
           delete p;
           p = NULL;
           result = true;
       }
       else if (p->left ==NULL)
       {
           BSTreeNode *temp = p;
           p = p->right;
           delete temp;
           result = true;
       }
       else if (p->right == NULL)
       {
           BSTreeNode *temp = p;
           p = p->left;
           delete temp;
           result = true;
       }
       else
       {
           BSTreeNode *temp = p->right;
           min(temp);
           p->dataItem = temp->dataItem;
           removeHelper(p->right, temp->dataItem.getKey());
           result = true;
       }
       }
   return result;
}

template<typename DataType, class KeyType>
void BSTree<DataType, KeyType>::min(BSTreeNode *& p)
{
   while (p->left != NULL)
   {
       p = p->left;
   }
}

template < typename DataType, class KeyType >
void BSTree<DataType, KeyType>::writeKeys () const
{
   writeHelper(root);
}

template<typename DataType, class KeyType>
void BSTree<DataType, KeyType>::writeHelper(BSTreeNode * p) const
{
   if (root == NULL)
   {
       cout << "empty tree" << endl;
   }
   if (p != NULL)
   {
       writeHelper(p->left);
       cout << p->dataItem.getKey() << " ";
       writeHelper(p->right);
   }
}
template < typename DataType, class KeyType >
void BSTree<DataType, KeyType>::clear ()
{
   makeEmpty(root);
}
template<typename DataType, class KeyType>
void BSTree<DataType, KeyType>::makeEmpty(BSTreeNode *& p)
{
   if (p != NULL)
   {
       makeEmpty(p->left);
       makeEmpty(p->right);
       delete p;
   }
   p = NULL;
}

template < typename DataType, class KeyType >
bool BSTree<DataType, KeyType>::isEmpty () const
{
   return (root == NULL);
}

template < typename DataType, class KeyType >
int BSTree<DataType, KeyType>::getHeight () const
{
   return getHeightHelper(root);
}

template<typename DataType, class KeyType>
int BSTree<DataType, KeyType>::getHeightHelper(BSTreeNode * p) const
{
  
   if (p == NULL)
       return 0;
   else
   {
       int l = getHeightHelper(p->left);
       int r = getHeightHelper(p->right);
       if (l > r)
           return (++l);
       else
           return (++r);
   }
}

template < typename DataType, class KeyType >
int BSTree<DataType, KeyType>::getCount() const
{
  
   return getCountHelper(root);
}

template<typename DataType, class KeyType>
int BSTree<DataType, KeyType>::getCountHelper(BSTreeNode * p) const
{
   int i = 1;
   if (p != NULL)
   {
       if (p->left != NULL)
           i += getCountHelper(p->left);
       if (p->right != NULL)
           i += getCountHelper(p->right);
       return i;
   }
   else
       return 0;
}

template < typename DataType, class KeyType >
void BSTree<DataType, KeyType>::writeLessThan(const KeyType& searchKey) const
{
   writeLessHelper(root, searchKey);
}

template<typename DataType, class KeyType>
void BSTree<DataType, KeyType>::writeLessHelper(BSTreeNode * p, const KeyType & searchKey) const
{
   if (searchKey < p->dataItem.getKey()) //if you go left just go left and leave printing to else function
   {
       writeLessHelper(p->left, searchKey);
   }
   else if (searchKey > p->dataItem.getKey()) //if you go right print the root and the left subtree before going right
   {//search right
       cout << p->dataItem.getKey() << " ";
       writeHelper(p->left); //i use my writekeyhelper to print
       writeLessHelper(p->right, searchKey);
   }
   else writeHelper(p->left); //i used my right key helper to print
}


config.h

/**
* BSTree class (Lab 9) configuration file.
* Activate test 'N' by defining the corresponding LAB9_TESTN to have the value 1.
* Deactive test 'N' by setting the value to 0.
*/

#define LAB9_TEST1   1       // Programming Exercise 2: getCount
#define LAB9_TEST2   1       // Programming Exercise 2: getHeight
#define LAB9_TEST3   1       // Programming Exercise 3: writeLessThan

BSTree.h

#ifndef BSTREE_H
#define BSTREE_H

#include <stdexcept>
#include <iostream>

using namespace std;

template < typename DataType, class KeyType >    // DataType : tree data item
class BSTree                                     // KeyType : key field
{
public:

    // Constructor
    BSTree ();                         // Default constructor
    BSTree ( const BSTree<DataType,KeyType>& other );   // Copy constructor
    BSTree& operator= ( const BSTree<DataType,KeyType>& other );
                          // Overloaded assignment operator

    // Destructor
    ~BSTree ();

    // Binary search tree manipulation operations
    void insert ( const DataType& newDataItem ); // Insert data item
    bool retrieve ( const KeyType& searchKey, DataType& searchDataItem ) const;
                                                  // Retrieve data item
    bool remove ( const KeyType& deleteKey );            // Remove data item
    void writeKeys () const;                      // Output keys
    void clear ();                                // Clear tree

    // Binary search tree status operations
    bool isEmpty () const;                        // Tree is empty
    // !! isFull() has been retired. Not very useful in a linked structure.

    // Output the tree structure -- used in testing/debugging
    void showStructure () const;

    // In-lab operations
    int getHeight () const;                       // Height of tree
    int getCount () const;              // Number of nodes in tree
    void writeLessThan ( const KeyType& searchKey ) const; // Output keys < searchKey

protected:

    class BSTreeNode                  // Inner class: facilitator for the BSTree class
    {
      public:
  
        // Constructor
        BSTreeNode ( const DataType &nodeDataItem, BSTreeNode *leftPtr, BSTreeNode *rightPtr );
        // Data members
        DataType dataItem;         // Binary search tree data item
        BSTreeNode *left,    // Pointer to the left child
                   *right;   // Pointer to the right child
    };

    // Recursive helpers for the public member functions -- insert
    // prototypes of these functions here.
    void showHelper( BSTreeNode *p, int level ) const;
   void insertHelper(BSTreeNode *&p, const DataType& newDataItem);
   bool retrieveHelper(BSTreeNode *p, const KeyType& searchKey, DataType& searchDataItem) const;
   bool removeHelper(BSTreeNode *&p, const KeyType& deleteKey);
   void makeEmpty(BSTreeNode *&p);
   void clone(BSTreeNode*p, BSTreeNode *&t);
   void min(BSTreeNode *&p);
   void writeHelper(BSTreeNode *p) const;
   int getCountHelper(BSTreeNode *p) const;
   int getHeightHelper(BSTreeNode*p) const;
   void writeLessHelper(BSTreeNode * p, const KeyType & searchKey) const;
    // Data member
    BSTreeNode *root;   // Pointer to the root node
};

#endif   // define BSTREE_H

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote