****C++**** QUESTION IS IN THE PICTURES ATTACHED :) PLEASE COMPLETE THE CODE PRO
ID: 3599430 • Letter: #
Question
****C++****
QUESTION IS IN THE PICTURES ATTACHED :)
PLEASE COMPLETE THE CODE PROVIDED! NEED URGENT PLEASE!!
===========================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
//
// Christopher Chouinard
// Raidah Ahmed
//
//--------------------------------------------------------------------
#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;
// Data member
BSTreeNode *root; // Pointer to the root node
};
#endif // define BSTREE_H
===========================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
===========================database.cpp=============================
//--------------------------------------------------------------------
//
// Laboratory 9 database.cpp
//
// Christopher Chouinard
// Raidah Ahmed
//
// Assignment 2 - Exercise 2
//
//--------------------------------------------------------------------
===========================test9.cpp=============================
//--------------------------------------------------------------------
//
// Laboratory 9 test9.cpp
//
// Test program for the operations in the Binary Search Tree ADT
//
//--------------------------------------------------------------------
using namespace std;
#include <iostream>
#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;
}
===========================BSTree.cpp=============================
//--------------------------------------------------------------------
//
// Laboratory 9 BSTree.cpp
//
// Christopher Chouinard
// Raidah Ahmed
//
//--------------------------------------------------------------------
#include <stdexcept>
#include <iostream>
#include "BSTree.h"
using namespace std;
BSTree<DataType, KeyType>::BSTree()
// Default constructor
{
}
template < typename DataType, typename KeyType >
BSTree<DataType, KeyType>::BSTree(const BSTree<DataType, KeyType>& other)
// Copy constructor
{
}
template < typename DataType, typename KeyType >
BSTree<DataType, KeyType>::BSTree& operator= (const BSTree<DataType, KeyType>& other)
// Overloaded assignment operator
{
}
// Destructor
template < typename DataType, typename KeyType >
BSTree<DataType, KeyType>::~BSTree()
{
}
//---- Binary search tree manipulation operations
template < typename DataType, typename KeyType >
void BSTree<DataType, KeyType>::insert(const DataType& newDataItem)
// Insert data item
{
}
template < typename DataType, typename KeyType >
bool BSTree<DataType, KeyType>::retrieve(const KeyType& searchKey, DataType& searchDataItem) const
// Retrieve data item
{
}
template < typename DataType, typename KeyType >
bool BSTree<DataType, KeyType>::remove(const KeyType& deleteKey)
// Remove data item
{
}
template < typename DataType, typename KeyType >
void BSTree<DataType, KeyType>::writeKeys() const
// Output keys
{
}
template < typename DataType, typename KeyType >
void BSTree<DataType, KeyType>::clear()
// Clear tree
{
}
//---- Binary search tree status operations
template < typename DataType, typename KeyType >
bool BSTree<DataType, KeyType>::isEmpty() const
// Tree is empty
// !! isFull() has been retired. Not very useful in a linked structure.
{
}
//---- In-lab operations
template < typename DataType, typename KeyType >
int BSTree<DataType, KeyType>::getHeight() const
// Height of tree
{
}
template < typename DataType, typename KeyType >
int BSTree<DataType, KeyType>::getCount() const
// Number of nodes in tree
{
}
template < typename DataType, typename KeyType >
void BSTree<DataType, KeyType>::writeLessThan(const KeyType& searchKey) const
// Output keys < searchKey
{
}
Explanation / Answer
#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;
// Data member
BSTreeNode *root; // Pointer to the root node
};
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.