1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
#include <iostream>
#include "BinarySearchTree.h"
// PRIVATE HELPER METHODS - IMPLEMENT THESE
template<typename ItemType, typename KeyType>
void BinarySearchTree<ItemType, KeyType>::destroyTree(BinaryNode<ItemType>* subTreePtr)
{
if(rootPtr!=nullptr)
{
destroyTree(subTreePtr->getLeftChildPtr());
destroyTree(subTreePtr->getRightChildPtr());
if(subTreePtr->isLeaf())
delete subTreePtr;
}
}
template<typename ItemType, typename KeyType>
BinaryNode<ItemType>* BinarySearchTree<ItemType,KeyType>::insertInorder(BinaryNode<ItemType>* subTreePtr,
BinaryNode<ItemType>* newNodePtr)
{
if(newNodePtr->getItem()<=subTreePtr->getItem())
{
if(subTreePtr->getLeftChildPtr()==nullptr)
{
subTreePtr->setLeftChildPtr(newNodePtr);
return(newNodePtr);
}
else
return(insertInorder(subTreePtr->getLeftChildPtr(), newNodePtr));
}
else
{
if(subTreePtr->getRightChildPtr()==nullptr)
{
subTreePtr->setRightChildPtr(newNodePtr);
return(newNodePtr);
}
else
return(insertInorder(subTreePtr->getLeftChildPtr(), newNodePtr));
}
}
template<typename ItemType, typename KeyType>
BinaryNode<ItemType>* BinarySearchTree<ItemType, KeyType>::findNode(
BinaryNode<ItemType>* subTreePtr, const KeyType& target) const
{
if(rootPtr==nullptr)
{
return(nullptr);
}
else if(target==subTreePtr->getItem().getWord())
{
return subTreePtr;
}
else if(target<=subTreePtr->getItem().getWord())
{
if(subTreePtr->getLeftChildPtr()==nullptr)
{
return(nullptr);
}
else
return(findNode(subTreePtr->getLeftChildPtr(), target));
}
else
return(nullptr);
}
//////////////////////////////////////////////////////////////
// PUBLIC METHODS BEGIN HERE
//////////////////////////////////////////////////////////////
template<typename ItemType, typename KeyType>
BinarySearchTree<ItemType, KeyType>::BinarySearchTree() : rootPtr(nullptr)
{
}
template<typename ItemType, typename KeyType>
BinarySearchTree<ItemType, KeyType>::~BinarySearchTree()
{
this->destroyTree(rootPtr); // Call inherited method
} // end destructor
//////////////////////////////////////////////////////////////
// Public BinaryTreeInterface Methods Section - IMPLEMENT THESE
//////////////////////////////////////////////////////////////
template<typename ItemType, typename KeyType>
bool BinarySearchTree<ItemType, KeyType>::add(const ItemType& newData)
{
if(rootPtr==nullptr)
{
rootPtr=new BinaryNode<ItemType>(newData);
return true;
}
else
{
BinaryNode<ItemType>* newNode=new BinaryNode<ItemType>(newData);
insertInorder(rootPtr, newNode);
return(true);
}
return false;
}
template<typename ItemType, typename KeyType>
ItemType BinarySearchTree<ItemType, KeyType>::getEntry(const KeyType& aKey) const throw(NotFoundException)
{
try
{
ItemType i=findNode(rootPtr,aKey)->getItem();
return(i);
}
catch(exception& e)
{
cout << "Word not found" << endl;
}
}
template<typename ItemType, typename KeyType>
bool BinarySearchTree<ItemType, KeyType>::contains(const KeyType& aKey) const
{
if(findNode(rootPtr, aKey)==nullptr)
return false;
else
return true;
}
|