二叉排序树 My_Bst && 二叉平衡树

#include<iostream>
#include<assert.h>
#include<vector>

using namespace std;

template<class Type>
class BST;

template<class Type>
class BSTNode
{
	friend class BST<Type>;
public:
	BSTNode(Type d = Type(), BSTNode<Type>* left = nullptr, BSTNode<Type>* right = nullptr)
		:data(d), leftChild(left), rightChild(right)
	{}
	~BSTNode()
	{}
private:
	Type data;
	BSTNode<Type>* leftChild;
	BSTNode<Type>* rightChild;
};

template<class Type>
class BST
{
public:
	BST() :root(nullptr)
	{}
	~BST()
	{
		Destroy();
	}
public:
	BSTNode<Type>* Find(const Type& key)const
	{
		return Find(root, key);
	}
	bool Insert(const Type& v)
	{
		return Insert(root, v);
	}
	bool Remove(const Type& key)
	{
		return Remove(root, key);
	}
	Type Min()const
	{
		return Min(root);
	}
	Type Max()const
	{
		return Max(root);
	}
	void SortPrint()const
	{
		SortPrint(root);
	}
	void Destroy()
	{
		Destroy(root);
	}
protected:
	BSTNode<Type>* Find(BSTNode<Type>* t, const Type& key)const
	{
		//空树或者传入的根节点就是我们要找的节点,返回节点
		if (t == nullptr || t->data == key)
			return t;
		//如果要找的data小于当前的节点  则在左树进行查找
		if (key < t->data)
			return Find(t->leftChild, key);
		else //大于则在右树寻找
			return Find(t->rightChild, key);
	}
	void SortPrint(BSTNode<Type>* t)const
	{
		if (t != nullptr)
		{
			//根据二叉排序树特性中序遍历后得到便是由小到的节点
			SortPrint(t->leftChild);
			cout << t->data << " ";
			SortPrint(t->rightChild);
		}
	}
	Type Min(BSTNode<Type>* t)const
	{
		//二叉搜索树最左边的节点是最小值
		assert(t != nullptr);
		while (t->leftChild != nullptr)
			t = t->leftChild;
		return t->data;
	}
	Type Max(BSTNode<Type>* t)const
	{
		//二叉搜索树最右边的节点是最大值
		assert(t != nullptr);
		while (t->rightChild != nullptr)
			t = t->rightChild;
		return t->data;
	}
	bool Insert(BSTNode<Type>*& t, const Type& v)
	{
		//First 查找插入位置
		//我们另一个先驱节点指针以记录父的位置,因为你如果用t一直跑,跑到最后为空了就不知道往哪里插入了
		BSTNode<Type>* p = t, * pre = nullptr;

		while (p != nullptr)
		{
			if (v == p->data)
				return false;

			pre = p;

			if (v < p->data)
				p = p->leftChild;
			else
				p = p->rightChild;
		}
		//生成插入节点
		p = new BSTNode<Type>(v);
		//判断传入的是否是空节点,是的话就将该节点给root

	  //判断是该插入pre  的左右子树
		if (pre == nullptr)
		{
			t = p;
			return true;
		}

		if (v < pre->data)
			pre->leftChild = p;
		else
			pre->rightChild = p;
		return true;
	}

	bool Remove(BSTNode<Type>*& t, const Type& key)
	{
		//如果根节点是空的直接返回false;
		if (t == nullptr)
			return false;
		if (key < t->data)
			Remove(t->leftChild, key);
		else if (key > t->data)
			Remove(t->rightChild, key);

		else
		{
			//找到 要删除的节点了
			if (t->leftChild != nullptr && t->rightChild != nullptr)
			{
				// 一,要删除的t节点 有左右子树 删除后要重新合成二叉树 
				//我们在待删除 节点的左子树找最大值覆盖t的值域 ,再将最大值的节点删除就完成了,删除与拼接 
				BSTNode<Type>* p = t->leftChild;
				while (p->rightChild != nullptr)
					p = p->rightChild;

				t->data = p->data;
				Remove(t->leftChild, p->data);
			}
			else
			{
				//删除的节点至少含有一个子树
				BSTNode<Type>* p = t;
				if (t->leftChild != nullptr)
					t = p->leftChild;
				else
					t = p->rightChild;
				delete p;
			}

			return true;
		}
	}

	void Destroy(BSTNode<Type>*& t)
	{
		if (t != nullptr)
		{
			Destroy(t->leftChild);
			Destroy(t->rightChild);
			delete t;
			t = nullptr;
		}
	}
private:
	BSTNode<Type>* root;
};

int main()
{
	vector<int> iv = { 8, 4, 9, 2, 1, 3, 15, 18, 14, 7 };

	//vector<int> iv = {16, 3, 7, 11, 9, 26, 18 ,14, 15};

	BST<int> bst;   //binary search tree

	for (int i = 0; i < iv.size(); ++i)
	{
		bst.Insert(iv[i]);
	}

	cout << "min value = " << bst.Min() << endl;
	cout << "max value = " << bst.Max() << endl;
	bst.SortPrint();
	cout << endl;

	BSTNode<int>* p = bst.Find(15);
	bst.Remove(3);
	bst.Remove(2);

	return 0;
}
#include<iostream>
#include<stack>
using namespace std;

template<class Type>
class AVL;

//定义AVL节点类型
template<class Type>
class AVLNode
{
	friend class AVL<Type>;
public:
	AVLNode(Type d=Type(),AVLNode<Type>*left=nullptr,AVLNode<Type>*right=nullptr)
		:data(d),leftChild(left),rightChild(right),bf(0)
	{}
	~AVLNode()
	{}
private:
	Type data;
	AVLNode *leftChild;
	AVLNode *rightChild;
	int bf;
};

template<class Type>
class AVL
{
public:
	AVL() : root(nullptr)
	{}
public:
	bool Insert(const Type &v)
	{
		return Insert(root, v);
	}
	bool Remove(const Type &key)
	{
		return Remove(root, key);
	}
protected:
	bool Insert(AVLNode<Type> *&t, const Type &v);
	bool Remove(AVLNode<Type> *&t, const Type &key);
protected:
	void RotateR(AVLNode<Type> *&ptr);
	void RotateL(AVLNode<Type> *&ptr);
	void RotateLR(AVLNode<Type> *&ptr);
	void RotateRL(AVLNode<Type> *&ptr);
private:
	AVLNode<Type> *root;
};

//
template<class Type>
void AVL<Type>::RotateR(AVLNode<Type> *&ptr)
{
	AVLNode<Type> *subR = ptr;
	ptr = subR->leftChild;
	subR->leftChild = ptr->rightChild;
	ptr->rightChild = subR;

	ptr->bf = subR->bf = 0;
}
template<class Type>
void AVL<Type>::RotateL(AVLNode<Type> *&ptr)
{
	AVLNode<Type> *subL = ptr;
	ptr = subL->rightChild;
	subL->rightChild = ptr->leftChild;
	ptr->leftChild = subL;

	ptr->bf = subL->bf = 0;
}

template<class Type>
void AVL<Type>::RotateLR(AVLNode<Type> *&ptr)
{
	AVLNode<Type> *subR = ptr;
	AVLNode<Type> *subL = ptr->leftChild;
	ptr = subL->rightChild;

	subL->rightChild = ptr->leftChild;
	ptr->leftChild = subL;
	//调整subL的bf
	if(ptr->bf <= 0)
		subL->bf = 0;
	else
		subL->bf = -1;

	subR->leftChild = ptr->rightChild;
	ptr->rightChild = subR;
	//调整subR的bf
	if(ptr->bf >= 0)
		subR->bf = 0;
	else
		subR->bf = 1;

	ptr->bf = 0;
}

template<class Type>
void AVL<Type>::RotateRL(AVLNode<Type> *&ptr)
{
	AVLNode<Type> *subL = ptr;
	AVLNode<Type> *subR = ptr->rightChild;
	ptr = subR->leftChild;

	subR->leftChild = ptr->rightChild;
	ptr->rightChild = subR;
	//调整subR的bf
	//subR->bf = ?
	if(ptr->bf >= 0)
		subR->bf = 0;
	else
		subR->bf = 1;

	subL->rightChild = ptr->leftChild;
	ptr->leftChild = subL;
	//调整subL的bf
	if(ptr->bf <= 0)
		subL->bf = 0;
	else
		subL->bf = -1;

	ptr->bf = 0;
}

//
template<class Type>
bool AVL<Type>::Insert(AVLNode<Type> *&t, const Type &v)
{
	//1 按照bst规则进行节点插入
	AVLNode<Type> *p = t, *pr = nullptr;

	stack<AVLNode<Type>*> st;

	while(p != nullptr)
	{
		if(v == p->data)
			return false;

		pr = p;
		st.push(pr);

		if(v < p->data)
			p = p->leftChild;
		else
			p = p->rightChild;
	}

	p = new AVLNode<Type>(v);
	if(pr == nullptr)
	{
		t = p;
		return true;
	}

	if(p->data < pr->data)
		pr->leftChild = p;
	else
		pr->rightChild = p;

	//2 调整平衡
	while(!st.empty())
	{
		pr = st.top();
		st.pop();

		if(p == pr->leftChild)
			pr->bf--;
		else
			pr->bf++;

		if(pr->bf == 0) //结束调整
			break;
		if(pr->bf==1 || pr->bf==-1) //需要向根回溯
			p = pr;
		else
		{
			//发生不平衡,调整平衡
			if(pr->bf < 0)
			{
				if(p->bf < 0)  //    / 
				{
					RotateR(pr);
				}
				else   //   < 
				{
					RotateLR(pr);
				}
			}
			else
			{
				if(p->bf > 0)    //  \  
				{
					RotateL(pr);
				}
				else    //  >
				{
					RotateRL(pr);
				}
			}
			break;
		}
	}

	//重新链接
	if(st.empty())
		t = pr;
	else
	{
		AVLNode<Type> *ppr = st.top();
		if(pr->data < ppr->data)
			ppr->leftChild = pr;
		else
			ppr->rightChild = pr;
	}

	return true;
}
template<class Type>
bool AVL<Type>::Remove(AVLNode<Type> *&t, const Type &key)
{
	//1 按照bst的规则删除节点

	stack<AVLNode<Type>*> st;

	AVLNode<Type> *p = t, *pr = nullptr;
	while(p != nullptr)
	{
		if(key == p->data)
			break;
		
		pr = p;
		st.push(pr);
		
		if(key < p->data)
			p = p->leftChild;
		else
			p = p->rightChild;
	}

	if(p == nullptr)
		return false;

	AVLNode<Type> *q;
	if(p->leftChild!=nullptr && p->rightChild!=nullptr)
	{
		pr = p;
		st.push(pr);

		q = p->leftChild;
		while(q->rightChild != nullptr)
		{
			pr = q;
			st.push(pr);

			q = q->rightChild;
		}
		p->data = q->data;
		p = q;
	}
	
	if(p->leftChild != nullptr)
		q = p->leftChild;
	else
		q = p->rightChild;
	

	//摘除节点
	if(p->data < pr->data)
		pr->leftChild = q;
	else
		pr->rightChild = q;


	q = p;
	//2 调整平衡
	while(!st.empty())
	{
		pr = st.top();
		st.pop();

		if(q->data < pr->data)
			pr->bf++;
		else
			pr->bf--;

		if(pr->bf==1 || pr->bf==-1) //结束调整
			break;

		if(pr->bf==2 || pr->bf==-2)
		{
			//让q指向较高子树
			if(pr->bf < 0)
				q = pr->leftChild;
			else
				q = pr->rightChild;

			if(q->bf == 0)
			{
				if(pr->bf > 0)
				{
					RotateL(pr);
					pr->leftChild->bf = 1;
				}
				else
				{
					RotateR(pr);
					pr->rightChild->bf = -1;
				}
				break;   //调整完成,结束调整
			}
			else
			{
				if(pr->bf > 0)
				{
					if(q->bf > 0)
						RotateL(pr);
					else
						RotateRL(pr);
				}
				else
				{
					if(q->bf < 0)
						RotateR(pr);
					else
						RotateLR(pr);
				}
				//注意,需要继续向上回溯,因为子树的高度降低,同时重新链接节点
				if(st.empty())
					t = pr;
				else
				{

					AVLNode<Type> *ppr = st.top();
					if (pr->data < ppr->data)
						ppr->leftChild = pr;
					else
						ppr->rightChild = pr;
				}
			}
		}
		else
		{
			//回溯
			q = pr;
		}
	}

	if(st.empty())
		t = pr;
	else
	{
		
		AVLNode<Type> *ppr = st.top();
		if(pr->data < ppr->data)
			ppr->leftChild = pr;
		else
			ppr->rightChild = pr;
	}

	delete p;
}


void main()
{
	int ar[] = {16, 3, 7, 11, 9, 26, 18, 14, 15};
	int n = sizeof(ar) / sizeof(ar[0]);

	AVL<int> avl;
	for(int i=0; i<n; ++i)
	{
		avl.Insert(ar[i]);
	}

	avl.Insert(8);

	avl.Remove(3);
	avl.Remove(11);
	avl.Remove(7);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值