00001
00016 #include <cstddef>
00017 #include <new>
00018 #include "BST.h"
00019
00020 using namespace std;
00021
00022 BinarySearchTree::BinarySearchTree() : root(NULL)
00023 {
00024 }
00025
00026 BinarySearchTree::BinarySearchTree(const BinarySearchTree& tree)
00027 throw(TreeException)
00028 {
00029 copyTree(tree.root, root);
00030 }
00031
00032 BinarySearchTree::~BinarySearchTree()
00033 {
00034 destroyTree(root);
00035 }
00036
00037 bool BinarySearchTree::isEmpty() const
00038 {
00039 return (root == NULL);
00040 }
00041
00042 void BinarySearchTree::searchTreeInsert(const TreeItemType& newItem)
00043 throw(TreeException)
00044 {
00045 insertItem(root, newItem);
00046 }
00047
00048 void BinarySearchTree::searchTreeDelete(KeyType searchKey)
00049 throw(TreeException)
00050 {
00051 deleteItem(root, searchKey);
00052 }
00053
00054 void BinarySearchTree::searchTreeRetrieve(KeyType searchKey,
00055 TreeItemType& treeItem) const
00056 throw(TreeException)
00057 {
00058
00059
00060
00061 retrieveItem(root, searchKey, treeItem);
00062 }
00063
00064 void BinarySearchTree::preorderTraverse(FunctionType visit)
00065 {
00066 preorder(root, visit);
00067 }
00068
00069 void BinarySearchTree::inorderTraverse(FunctionType visit)
00070 {
00071 inorder(root, visit);
00072 }
00073
00074 void BinarySearchTree::postorderTraverse(FunctionType visit)
00075 {
00076 postorder(root, visit);
00077 }
00078
00079 void BinarySearchTree::insertItem(TreeNode *& treePtr,
00080 const TreeItemType& newItem)
00081 throw(TreeException)
00082 {
00083 if (treePtr == NULL)
00084 {
00085
00086
00087 try
00088 {
00089 treePtr = new TreeNode(newItem, NULL, NULL);
00090 }
00091 catch (bad_alloc e)
00092 {
00093 throw TreeException(
00094 "TreeException: insertItem cannot allocate memory");
00095 }
00096 }
00097
00098 else if (newItem.getKey() < treePtr->item.getKey())
00099
00100 insertItem(treePtr->leftChildPtr, newItem);
00101
00102 else
00103 insertItem(treePtr->rightChildPtr, newItem);
00104 }
00105
00106 void BinarySearchTree::deleteItem(TreeNode *& treePtr,
00107 KeyType searchKey)
00108 throw(TreeException)
00109
00110 {
00111 if (treePtr == NULL)
00112 throw TreeException("TreeException: delete failed");
00113
00114 else if (searchKey == treePtr->item.getKey())
00115
00116 deleteNodeItem(treePtr);
00117
00118
00119 else if (searchKey < treePtr->item.getKey())
00120
00121 deleteItem(treePtr->leftChildPtr, searchKey);
00122
00123 else
00124 deleteItem(treePtr->rightChildPtr, searchKey);
00125 }
00126
00127 void BinarySearchTree::deleteNodeItem(TreeNode *& nodePtr)
00128
00129
00130
00131
00132
00133
00134 {
00135 TreeNode *delPtr;
00136 TreeItemType replacementItem;
00137
00138
00139 if ( (nodePtr->leftChildPtr == NULL) &&
00140 (nodePtr->rightChildPtr == NULL) )
00141 { delete nodePtr;
00142 nodePtr = NULL;
00143 }
00144
00145 else if (nodePtr->leftChildPtr == NULL)
00146 { delPtr = nodePtr;
00147 nodePtr = nodePtr->rightChildPtr;
00148 delPtr->rightChildPtr = NULL;
00149 delete delPtr;
00150 }
00151
00152
00153 else if (nodePtr->rightChildPtr == NULL)
00154 { delPtr = nodePtr;
00155 nodePtr = nodePtr->leftChildPtr;
00156 delPtr->leftChildPtr = NULL;
00157 delete delPtr;
00158 }
00159
00160
00161
00162 else
00163 { processLeftmost(nodePtr->rightChildPtr,
00164 replacementItem);
00165 nodePtr->item = replacementItem;
00166 }
00167 }
00168
00169 void BinarySearchTree::processLeftmost(TreeNode *& nodePtr, TreeItemType& treeItem)
00170 {
00171 if (nodePtr->leftChildPtr == NULL)
00172 { treeItem = nodePtr->item;
00173 TreeNode *delPtr = nodePtr;
00174 nodePtr = nodePtr->rightChildPtr;
00175 delPtr->rightChildPtr = NULL;
00176 delete delPtr;
00177 }
00178
00179 else
00180 processLeftmost(nodePtr->leftChildPtr, treeItem);
00181 }
00182
00183 void BinarySearchTree::retrieveItem(TreeNode *treePtr,
00184 KeyType searchKey,
00185 TreeItemType& treeItem) const
00186 throw(TreeException)
00187 {
00188 if (treePtr == NULL)
00189 throw TreeException("TreeException: searchKey not found");
00190
00191 else if (searchKey == treePtr->item.getKey())
00192
00193 treeItem = treePtr->item;
00194
00195 else if (searchKey < treePtr->item.getKey())
00196
00197 retrieveItem(treePtr->leftChildPtr,
00198 searchKey, treeItem);
00199
00200 else
00201 retrieveItem(treePtr->rightChildPtr,
00202 searchKey, treeItem);
00203 }
00204
00205
00206
00207
00208
00209