二叉查找树的实现继承了 二叉树的实现。
//二叉查找树的头文件
#ifndef SEARCH_TREE_H_
#define SEARCH_TREE_H_
#include"tree_exception.h"
#include"binary_tree.h"
class SearchTree :public BinaryTree
{
public:
SearchTree();
SearchTree(const BinaryTree &_tree);
virtual ~SearchTree();
virtual void insert(const ItemType &newitem)
throw(TreeException);
virtual void remove(const ItemType &newitem)throw(TreeException);
protected:
void insertitem(TreeNode *&treePtr,const ItemType &newitem)
throw(TreeException);
void removeitem(TreeNode *&treePtr,const ItemType &keyitem)throw(TreeException);
void removenode(TreeNode *&treePtr);
void processleftmost(TreeNode *&treePtr,ItemType &_item);
};
#endif
//二叉查找树的实现文件
#include"search_tree.h"
SearchTree::SearchTree()
{
}
SearchTree::SearchTree(const BinaryTree &_tree)
:BinaryTree(_tree)
{
}
SearchTree::~SearchTree()
{}
void SearchTree::insert(const ItemType &newitem)
throw(TreeException)
{
try{
insertitem(rootPtr(),newitem);
}catch(TreeException &e)
{
throw;
}
}
void SearchTree::remove(const ItemType &keyitem)throw(TreeException)
{
try{
removeitem(rootPtr(),keyitem);
}catch(TreeException &e)
{
throw;
}
}
void SearchTree::insertitem(TreeNode *&treePtr,const ItemType &newitem)
throw(TreeException)
{
if(treePtr==NULL)
{
treePtr=new TreeNode(newitem,NULL,NULL);
if(treePtr==NULL)
throw TreeException("calling insert function failed :allocate memory failed ");
}
else if(treePtr->item>newitem)
{
insertitem(getLeftPtr(treePtr),newitem);
}
else
insertitem(getRightPtr(treePtr),newitem);
}
void SearchTree::removeitem(TreeNode *&treePtr,const ItemType &keyitem)
throw(TreeException)
{
if(treePtr==NULL)
throw TreeException("calling remove function failed:search key item failed ");
else if(treePtr->item==keyitem)
{
removenode(treePtr);
}
else if(treePtr->item>keyitem)
{
removeitem(getLeftPtr(treePtr),keyitem);
}
else
{
removeitem(getRightPtr(treePtr),keyitem);
}
}
void SearchTree::removenode(TreeNode *&treePtr)
{
ItemType replaceitem;
if((getLeftPtr(treePtr)==NULL)&&(getRightPtr(treePtr)==NULL))
{
delete treePtr;
treePtr=NULL;
}
else if(getLeftPtr(treePtr)==NULL)
{
TreeNode *temp=treePtr;
treePtr=getRightPtr(treePtr);
getRightPtr(temp)=NULL;
delete temp;
}
else if(getRightPtr(treePtr)==NULL)
{
TreeNode *temp=treePtr;
treePtr=getLeftPtr(treePtr);
getLeftPtr(temp)=NULL;
delete temp;
}
else
{
processleftmost(getRightPtr(treePtr),replaceitem);
treePtr->item=replaceitem;
}
}
void SearchTree::processleftmost(TreeNode *&treePtr,ItemType &_item)
{
if(getLeftPtr(treePtr)==NULL)
{
_item=treePtr->item;
TreeNode *temp=treePtr;
treePtr=getRightPtr(treePtr);
getRightPtr(temp)=NULL;
delete temp;
}
else
{
processleftmost(getLeftPtr(treePtr),_item);
}
}
//二叉查找树的测试
#include"search_tree.h"
#include"binary_tree.h"
#include<iostream>
using namespace std;
void display(ItemType &anitem);
int main()
{
BinaryTree tree1,tree2,left;
BinaryTree tree3("hello");
try{
tree1.setRootData("tetrach");
tree1.attachLeft("creature");
tree1.attachRight("vocation");
cout<<"tree1 :"<<endl;
tree1.postorderTraverse(display);
cout<<endl;
tree2.setRootData("prevail");
tree2.attachLeft("dormant");
tree2.attachRightSubtree(tree1);
cout<<endl;
cout<<"tree2 :"<<endl;
tree2.inorderTraverse(display);
cout<<endl;
BinaryTree binTree("povit",tree2,tree3);
cout<<"binTree :"<<endl;
binTree.inorderTraverse(display);
cout<<endl;
cout<<"binTree 's left subtree :"<<endl;
binTree.getLeftSubtree().preorderTraverse(display);
cout<<endl;
binTree.deleteLeftSubtree(left);
cout<<endl;
cout<<"binTree 's left subtree :"<<endl;
left.inorderTraverse(display);
cout<<endl<<"binTree after delete :"<<endl;
binTree.preorderTraverse(display);
cout<<endl;
SearchTree * atree=new SearchTree();
atree->insert("inheritor");
atree->insert("arrogate");
atree->insert("diverse");
atree->insert("sacrament");
cout<<endl;
cout<<"atree :"<<endl;
atree->inorderTraverse(display);
atree->remove("arrogate");
cout<<endl;
cout<<"atree after removing :"<<endl;
atree->inorderTraverse(display);
}catch(TreeException &e)
{
cout<<e.what()<<endl;
}
return 0;
}
void display(ItemType &anitem)
{
cout<<anitem<<" ";
}