📄 c10p559.txt
字号:
/ *********************************************************// Implementation file BST.cpp.// *********************************************************#include "BST.h" // header file#include <cstddef> // definition of NULLBinarySearchTree::BinarySearchTree() : root(NULL){} // end default constructorBinarySearchTree::BinarySearchTree( const BinarySearchTree& tree){ copyTree(tree.root, root);} // end copy constructorBinarySearchTree::~BinarySearchTree(){ destroyTree(root);} // end destructorbool BinarySearchTree::isEmpty() const{ return (root == NULL);} // end searchTreeIsEmptyvoid BinarySearchTree::searchTreeInsert(const TreeItemType& newItem){ insertItem(root, newItem);} // end searchTreeInsertvoid BinarySearchTree::searchTreeDelete(KeyType searchKey) throw(TreeException){ deleteItem(root, searchKey);} // end searchTreeDeletevoid BinarySearchTree::searchTreeRetrieve(KeyType searchKey, TreeItemType& treeItem) const throw(TreeException){ // if retrieveItem throws a TreeException, it is // ignored here and passed on to the point in the code // where searchTreeRetrieve was called retrieveItem(root, searchKey, treeItem);} // end searchTreeRetrievevoid BinarySearchTree::preorderTraverse(FunctionType visit){ preorder(root, visit);} // end preorderTraversevoid BinarySearchTree::inorderTraverse(FunctionType visit){ inorder(root, visit);} // end inorderTraversevoid BinarySearchTree::postorderTraverse(FunctionType visit){ postorder(root, visit);} // end postorderTraversevoid BinarySearchTree::insertItem(TreeNode *& treePtr, const TreeItemType& newItem){ if (treePtr == NULL) { // position of insertion found; insert after leaf // create a new node treePtr = new TreeNode(newItem, NULL, NULL); } // else search for the insertion position else if (newItem.getKey() < treePtr->item.getKey()) // search the left subtree insertItem(treePtr->leftChildPtr, newItem); else // search the right subtree insertItem(treePtr->rightChildPtr, newItem);} // end insertItemvoid BinarySearchTree::deleteItem(TreeNode *& treePtr, KeyType searchKey) throw(TreeException)// Calls: deleteNodeItem.{ if (treePtr == NULL) throw TreeException("TreeException: delete failed"); // empty tree else if (searchKey == treePtr->item.getKey()) // item is in the root of some subtree deleteNodeItem(treePtr); // delete the item // else search for the item else if (searchKey < treePtr->item.getKey()) // search the left subtree deleteItem(treePtr->leftChildPtr, searchKey); else // search the right subtree deleteItem(treePtr->rightChildPtr, searchKey);} // end deleteItemvoid BinarySearchTree::deleteNodeItem(TreeNode *& nodePtr)// Algorithm note: There are four cases to consider:// 1. The root is a leaf.// 2. The root has no left child.// 3. The root has no right child.// 4. The root has two children.// Calls: processLeftmost.{ TreeNode *delPtr; TreeItemType replacementItem; // test for a leaf if ( (nodePtr->leftChildPtr == NULL) && (nodePtr->rightChildPtr == NULL) ) { delete nodePtr; nodePtr = NULL; } // end if leaf // test for no left child else if (nodePtr->leftChildPtr == NULL) { delPtr = nodePtr; nodePtr = nodePtr->rightChildPtr; delPtr->rightChildPtr = NULL; delete delPtr; } // end if no left child // test for no right child else if (nodePtr->rightChildPtr == NULL) { delPtr = nodePtr; nodePtr = nodePtr->leftChildPtr; delPtr->leftChildPtr = NULL; delete delPtr; } // end if no right child // there are two children: // retrieve and delete the inorder successor else { processLeftmost(nodePtr->rightChildPtr, replacementItem); nodePtr->item = replacementItem; } // end if two children} // end deleteNodeItemvoid BinarySearchTree::processLeftmost(TreeNode *& nodePtr, TreeItemType& treeItem){ if (nodePtr->leftChildPtr == NULL) { treeItem = nodePtr->item; TreeNode *delPtr = nodePtr; nodePtr = nodePtr->rightChildPtr; delPtr->rightChildPtr = NULL; // defense delete delPtr; } else processLeftmost(nodePtr->leftChildPtr, treeItem);} // end processLeftmostvoid BinarySearchTree::retrieveItem(TreeNode *treePtr, KeyType searchKey, TreeItemType& treeItem) const throw(TreeException){ if (treePtr == NULL) throw TreeException("TreeException: searchKey not found"); else if (searchKey == treePtr->item.getKey()) // item is in the root of some subtree treeItem = treePtr->item; else if (searchKey < treePtr->item.getKey()) // search the left subtree retrieveItem(treePtr->leftChildPtr, searchKey, treeItem); else // search the right subtree retrieveItem(treePtr->rightChildPtr, searchKey, treeItem);} // end retrieveItem// Implementations of copyTree, destroyTree, preorder, // inorder, postorder, setRootPtr, rootPtr, getChildPtrs, // setChildPtrs, and the overloaded assignment operator are // the same as for the ADT binary tree.// End of implementation file.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -