二叉搜索树的c++实现

#include<iostream>
using namespace std;

template<class T>
class BSTree;

template <class T>
class BSTNode
{
	friend class BSTree<T>;
public:
	BSTNode():leftChild(NULL),rightChild(NULL)
	{
	}
	BSTNode(T d, BSTNode<T>* L=NULL, BSTNode<T>* R=NULL):data(d),leftChild(L),rightChild(R)
	{}
	BSTNode(BSTNode<T>* L=NULL, BSTNode<T>* R=NULL):data(T()),leftChild(L),rightChild(R)
	{}
	~BSTNode()
	{}
private:
	BSTNode<T>* leftChild;
	BSTNode<T>* rightChild;
	T           data;

};

template<class T>
class BSTree
{
public:
	BSTree():root(NULL):RefValue(0)
	{
	}
	BSTree(T value)
	{
		T x;
		root = NULL;
		Refvalue = value;
		cin>>x;
		while(x != Refvalue)
		{
			Insert(root,x);
			cin>>x;
		}
	}

	~BSTree()
	{}
public:
	void PrintTree()const
	{
		PrintTree(root);
	
	}
	bool Search(const T x)
	{
		return (Search(root, x)!=NULL) ? true : false; 
	}
	void Insert(const T& x)
	{
		Insert(root, x);
	}
	void MakeEmpty()
	{
		MakeEmpty(root);
		root = NULL;
	}
	bool Remove(const T x)
	{
		return Remove(root,x);
	}
	T Max()const
	{
		 return Max(root)->data;
	}
	T Min()const
	{
		return Min(root)->data;
	}
	size_t getSize()
	{
		return getSize(root);
	}
	void copy(BSTree<T> & p)
	{
		copy(root, p.root);
	}
protected:
	void copy(BSTNode<T>* &t, BSTNode<T>* &p)  //用p给t拷贝
	{
		
		if(p != NULL)
		{
			if(t !=	NULL)  //t节点存在,直接赋值
				t->data = p->data;
			else  //t为空,开辟p->data大小的节点
				t = new BSTNode<T>(p->data);
		    copy(t->leftChild,p->leftChild);
			
			copy(t->rightChild, p->rightChild);
		}
		else
		{
			if(t != NULL)  //拷贝完了t还不空,把t节点后的制空
			{
				MakeEmpty(t);
				t = NULL;
			}
		}
	}
	size_t getSize(BSTNode<T>* t)const
	{
		if(t != NULL)
			return 1+getSize(t->leftChild)+getSize(t->rightChild);
		else
			return 0;
	}

	BSTNode<T>* Min(BSTNode<T>* t)const
	{
		if(t == NULL)
			return NULL;
		if(t->leftChild != NULL)
			Min(t->leftChild);
		else
			return t;
	}

	BSTNode<T>* Max(BSTNode<T>* t)const
	{
		if(t == NULL)
			return NULL;
		if(t->rightChild != NULL)
			Max(t->rightChild);
		else
			return t;
	}

	bool Remove(BSTNode<T>* & t, const T x)
	{
		BSTNode<T>* q= NULL;
		if(t != NULL)
		{
			if(t->data > x)
				Remove(t->leftChild, x);
			else if(t->data < x)
				Remove(t->rightChild, x);

			else if(t->leftChild != NULL && t->rightChild !=NULL)
			{
				q = t->rightChild;
				while(q->leftChild != NULL)
					q = q->leftChild ;
				t->data = q->data;
				Remove(t->rightChild, t->data);
			}
			else
			{
				q = t;
				if(t->leftChild == NULL)
					t = t->rightChild;
				else 
					t = t->leftChild;
				delete q;
				return true;
			}
		}
		return false;
	}
	void MakeEmpty(BSTNode<T>* &t)
	{
 		if(t != NULL)
		{
			MakeEmpty(t->leftChild);
			MakeEmpty(t->rightChild);
			delete t;
		}
	}
	
	void PrintTree(BSTNode<T>* t)const
	{
		if(t != NULL)
		{
			PrintTree(t->leftChild);
			cout<<t->data<<" ";
			PrintTree(t->rightChild);
		}	
	}
	bool Insert(BSTNode<T>* & t, const T& x)
	{
		if(t == NULL)
		{
			t = new BSTNode<T>(x);
			return true;
		}
		else if(t->data > x)
			Insert(t->leftChild, x);
		else if(t->data < x)
			Insert(t->rightChild , x);
		else //插入元素与树中某个节点相等时,不插入
			return false;
	}

	BSTNode<T>* Search(BSTNode<T>* t, const T x)
	{
		if(t != NULL)
		{
			if(t->data > x)
				return Search(t->leftChild, x);
			else if(t->data < x)
				return Search(t->rightChild, x);
			else
				return t;
		}
		return NULL;
	}
private:
	BSTNode<T>* root;
	T           Refvalue;  //结束标志
};

void main()
{
	int a[] = {9,53,5,45,17,23,78,65,34,87};
	BSTree<int> bst;	
	for(int i=0; i<10; ++i)
		bst.Insert(a[i]);
	bst.PrintTree();
	cout<<endl;
	bst.Search(10);
//	BSTree<int> bst1(0);
//	bst1.PrintTree();
	cout<<endl;
	//bst.Max();
	cout<<bst.Max()<<endl;
	cout<<bst.Min()<<endl;
//	bst.Remove(78);
//	bst.MakeEmpty();
	bst.PrintTree();
	cout<<endl;
	cout<<bst.getSize()<<endl;
	BSTree<int> bst1;
	bst.copy(bst1);
	bst.PrintTree();
}


递归的使用要理清思路,删除一个节点时要考虑多种情况,指针的使用要明确。


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。