二叉查找树(binary search tree)的实现

二叉查找树的实现继承了 二叉树的实现

//二叉查找树的头文件

#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<<"  ";
}



 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值