数据结构——二叉搜索树

这篇博客详细介绍了二叉搜索树(BST)的数据结构及其基本操作,包括插入、删除、查找、获取最大值和最小值等。通过模板类实现了一棵二叉搜索树,并提供了完整的C++代码实现,特别强调了在实现过程中的一些意外收获。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不会写的,算导上有意外收获

#include<iostream>
template <class E,class K>
struct BSTNode
{
	E data;							
	BSTNode<E,K> *left,*right;		
	BSTNode():left(NULL),right(NULL){}
	BSTNode(const E d,BSTNode<E,K> *L=NULL,BSTNode<E,K> *R=NULL):data(d),left(L),right(R){}
	~BSTNode(){}
	void SetData(E d){data = d;}
	E GetData(){return data;}
	bool operator <(const E& x){return (data.key<x.key)?true:false;}
	bool operator >(const E& x){return (data.key>x.key)?true:false;}
	bool operator ==(const E& x){return (data.key==x.key)?true:false;}
};
 
template <class E,class K>
class BST{
public:
	BST():root(NULL){}
	BST(K value);
	~BST(){};
	bool Search(const K x)const
		{return (Search(x,root)!=NULL)?true:false;}
	BST<E,K>& operator =(const BST<E,K>& R);              
	void makeEmpty()
		{makeEmpty(root);root = NULL;}			
	void PrintTree()const
		{PrintTree(root);}               
	E Min()
		{return Min(root)->data;}					  
	E Max()
		{return Max(root)->data;}					  
	bool Insert(const E& e)
		{return Insert(e,root);}		  
	bool Remove(const E x)
		{return Remove(x,root);}        
 
private:
	BSTNode<E,K> *root;
	K Refvalue;                                           //标识输入停止 
	BSTNode<E,K> *Search(const K x,BSTNode<E,K> *ptr);           
	void makeEmpty(BSTNode<E,K> *&ptr);							
	void PrintTree(BSTNode<E,K> *ptr)const;						
	BSTNode<E,K> *Copy(const BSTNode<E,K> *ptr);				
	BSTNode<E,K> *Min(BSTNode<E,K> *ptr) const;					
	BSTNode<E,K> *Max(BSTNode<E,K> *ptr) const;					
	bool Insert(const E& e,BSTNode<E,K> *&ptr);					
	bool Remove(const E x,BSTNode<E,K> *&ptr);					
};

template <class E,class K>
BSTNode<E,K> *BST<E,K>::Search(const K x,BSTNode<E,K> *ptr){
	if (ptr==NULL)
		return NULL;
	if (x<ptr->data)
		return Search(x,ptr->left);
	else if (x>ptr->data)
		return Search(x,ptr->right);
	else
		return ptr;
};
 
template <class E,class K>
bool BST<E,K>::Insert(const E& e,BSTNode<E,K> *&ptr){
	if (ptr==NULL){
		ptr = new BSTNode<E,K>(e);
		if (ptr==NULL)
			{ cerr<<"空间溢出"<<endl;exit(1);}
		return true;
	}
	if (e>ptr->data)	
		return Insert(e,ptr->right);
	else if (e<ptr->data)
		return Insert(e,ptr->left);
	else return false;
};
 
template <class E,class K>//建立一颗二叉搜索树,边输入边建 
BST<E,K>::BST(K value){
	E x;
	root = NULL;
	Refvalue = value;
	cin>>x;
	while (x.key!=Refvalue)
		Insert(x,root);cin>>x;
};
 
template <class E,class K>
bool BST<E,K>::Remove(const E x,BSTNode<E,K> *&ptr){
	if (ptr==NULL)
		return false;
	BSTNode<E,K> *temp;
	if (x<ptr->data)
		return Remove(x,ptr->left);
	else if (x>ptr->data)
		return Remove(x,ptr->right);
	else{
		if (ptr->left!=NULL&&ptr->right!=NULL){
			temp = ptr->right;
			while (temp->left!=NULL)
				temp = temp->left;
			ptr->data = temp->data;
			Remove(ptr->data,ptr->right);
		}
		else{
			temp = ptr;
			if (temp->left==NULL)
				ptr = ptr->right;
			else ptr = ptr->left;
			delete temp;
			return true;
		}
	}
	return false; 
};
 
template <class E,class K>
void BST<E,K>::makeEmpty(BSTNode<E,K> *&ptr){
	if (ptr!=NULL){
		BSTNode<E,K> *temp=ptr;
		makeEmpty(ptr->left);
		makeEmpty(ptr->right);
		delete temp;
	}
};
 
template <class E,class K>
void BST<E,K>::PrintTree(BSTNode<E,K> *ptr)const{
	if (ptr!=NULL){
		PrintTree(ptr->left);
		cout<<ptr->data<<endl;
		PrintTree(ptr->right);
	}
};
 
template <class E,class K>
BSTNode<E,K> *BST<E,K>::Min(BSTNode<E,K> *ptr)const{
	if (ptr->left==NULL)
		return ptr;
	if (ptr->left!=NULL)
		return Min(ptr->left);
	return NULL;
};
 
template <class E,class K>
BSTNode<E,K> *BST<E,K>::Max(BSTNode<E,K> *ptr)const{
	if (ptr->right==NULL)
		return ptr;
	if (ptr->right!=NULL)
		return Max(ptr->right);
	return NULL;
};
 
template <class E,class K>
BSTNode<E,K> *BST<E,K>::Copy(const BSTNode<E,K> *ptr){
	if (ptr!=NULL){
		BSTNode<E,K> *temp=new BSTNode<E,K>(ptr->data);
		temp->left = Copy(ptr->left);
		temp->right = Copy(ptr->right);
		return temp;
	}
};
 
template <class E,class K>
BST<E,K>& BST<E,K>::operator =(const BST<E,K>& R){
	BST<E,K> b;
	b.root = R.Copy(R.root);
	return b;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值