I desperately need help with a project I am working on. I need to create and implement a Binary Search Tree Header file. I am having great difficulty with this and was hoping I could find some help here.
This is what I have done(note: I am aware that the delete item function is not written):
#include <iostream>
#include <string>
using namespace std;
struct Node {
int item;
Node *leftChild;
Node *rightChild;
};
class BinarySearchTree {
public:
BinarySearchTree(); // constructor.
~BinarySearchTree(); // destructor.
void insertItem(Node *& curPtr, int newValue); // insert a value into BST.
bool SearchItem(Node *curPtr, int searchKey); // search the key in BST
void deleteItem(Node *& curPtr, int searchKey); // delete the key in BST
void preorderTraverse(Node *curPtr); // preorder traverse.
void inorderTraverse(Node *curPtr); // inorder traverse.
void postorderTraverse(Node *curPtr); // postoder traverse.
int numberOfNodes(Node *curPtr); // counting the number of nodes.
int heightOfTree(Node *curPtr); // computing the height of BST
int maxElement(Node *curPtr); // finding the max. value in BST
int averageOfElements(Node *curPtr); // computing the average value from BST
Node *getRoot(); // get the ptr to the root node
private:
Node *rootPtr;
}
//--------------------------------------------------------------------
//CONSTUCTOR
//--------------------------------------------------------------------
BinarySearchTree()
{
Node* root = NULL;
}
//--------------------------------------------------------------------
//DESTRUCTOR
//--------------------------------------------------------------------
~BinarySearchTree()
{
if (curPtr != NULL)
{
delete (*curPtr -> leftChild);
delete (*curPtr -> rightChild);
delete *curPtr;
}
}
//--------------------------------------------------------------------
//INSERT ITEM
//--------------------------------------------------------------------
void insertItem(Node*& curPtr, int newValue)
{
if (curPtr->item = NULL)
curPtr->item = newValue;
else if (newValue > curPtr->item)
insertItem(curPtr->rightChild, newValue);
else
insertItem(curPtr->leftChild, newValue);
}
//--------------------------------------------------------------------
//SEARCH ITEM
//--------------------------------------------------------------------
bool SearchItem(Node *curPtr, int searchKey);
{
if (curPtr == searchKey)
{
return true;
}
else if (curPtr < searchKey && curPtr->leftChild != NULL)
{
searchItem(curPtr->leftChild, searchKey);
}
else if (curPtr > searchKey && curPtr->rightChild != NULL)
{
searchItem(curPtr->rightChild, searchKey);
}
else
return false;
}
//--------------------------------------------------------------------
//DELETE ITEM
//--------------------------------------------------------------------
//--------------------------------------------------------------------
//PRE-ORDER TRAVERSAL
//--------------------------------------------------------------------
void preorderTraverse(Node *curPtr)
{
if (curPtr != NULL )
{
cout << curPtr->item << " ";
preorderTraverse( curPtr->leftChild );
preorderTraverse( curPtr->rightChild );
}
}
//--------------------------------------------------------------------
//IN-ORDER TRAVERSAL
//--------------------------------------------------------------------
void inorderTraverse(Node *curPtr)
{
if ( curPtr != NULL )
{
inorderTraverse( curPtr->leftChild );
cout << curPtr->item << " ";
inorderTraverse( curPtr->rightChild );
}
}
//--------------------------------------------------------------------
//POST-ORDER TRAVERSAL
//--------------------------------------------------------------------
void postorderTraverse(Node *curPtr)
{
if ( curPtr != NULL )
{
postorderTraverse( curPtr->leftChild );
postorderTraverse( curPtr->rightChild );
cout << curPtr->item << " ";
}
}
//--------------------------------------------------------------------
//NUMBER OF NODES
//--------------------------------------------------------------------
int numberOfNodes(Node *curPtr, int count)
{
if ( curPtr == NULL )
return 0;
else
{
count++;
count += numberOfNodes(curPtr->leftChild, count);
count += numberOfNodes(curPtr->rightChild, count);
return count;
}
}
//--------------------------------------------------------------------
//HEIGHT OF TREE
//--------------------------------------------------------------------
int heightOfTree(Node *curPtr, int heightCounter)
{
if(curPtr->item == NULL)
return heightCounter;
else if (curPtr->leftChild == NULL && curPtr->rightChild == NULL)
{
heightCounter++;
return heightCounter;
}
else
heightOfTree(curPtr->leftChild, heightCounter);
heightOfTree(curPtr->rightChild, heightCounter);
}
//--------------------------------------------------------------------
//MAX ELEMENT
//--------------------------------------------------------------------
int maxElement(Node *curPtr)
{
if(curPtr-> rightChild == NULL)
{
return curPtr->item;
}
else
{
maxElement(curPtr->rightChild);
}
}
//--------------------------------------------------------------------
//AVERAGE OF ELEMENTS
//--------------------------------------------------------------------
int averageOfElements(Node *curPtr)
{
int average, total=0, divsior=0;
numberOfNodes(curPtr, divisor);
if(curPtr->item != NULL)
{
total += curPtr->item;
total += averageOfElements(curPtr->leftChild);
total += averageOfElements(curPtr->rightChild);
}
else
return total/divisor;
}
NOTE: Must be recursive and I'm sorry for the weird spacing on code.