数据结构与算法-平衡二叉搜索树

平衡二叉搜索树

1.自平衡的二叉搜索树

2.平衡

(1)空树平衡

(2)非空树平衡

  • 左右子树平衡
  • 左右子树高度差绝对值 <= 1

3.平衡因子

左右子树的高度差的衡量值 -1 0 1


(一)平衡操作

1.左左单旋 LL:插入节点在左子树的左节点,导致二叉树失衡

LL单旋

template<class Elem>
BinNode<Elem>* AVLTree<Elem>::LLrotate(BinNode<Elem>* r){
	
	BinNode<Elem>* child;
	child = r->left;
	r->left = child->right;
	child->right = r;
	
	r->height = max(height(r->left),height(r->right))+1;
	child->height = max(height(r->left),height(r->right))+1;

	return child;
}

2.左右双旋 LR:插入节点在左子树的右节点,导致二叉树失衡

在这里插入图片描述

template<class Elem>
BinNode<Elem>* AVLTree<Elem>::LRrotate(BinNode<Elem>* r){
	
	r->left = RRrotate(r->left);
	r = LLrotate(r);
	return r;

}

3.右右单旋RR:与LL单旋对称

template<class Elem>
BinNode<Elem>* AVLTree<Elem>::RRrotate(BinNode<Elem>* r){

	BinNode<Elem>* child;
	child = r->right;
	r->right = child->left;
	child->left = r;
	
	r->height = max(height(r->left),height(r->right))+1;
	child->height = max(height(r->left),height(r->right))+1;

	return child;

}

4.右左双旋RL:与LR双旋对称

template<class Elem>
BinNode<Elem>* AVLTree<Elem>::RLrotate(BinNode<Elem>* r){
	
	r->right = LLrotate(r->right);
	r = RRrotate(r);
	return r;

}

(二)代码实现

#include <iostream>
#include <stack>
#include <queue>

using namespace std;



template<class Elem>
struct BinNode{

	Elem data;
	BinNode<Elem>* left;
	BinNode<Elem>* right;
	
	int height;
	BinNode(Elem x){
	
		data = x;
		left = right = NULL;
		height = 0;
		
	}
	
};

/********************************************************************************************/
/********************************************二叉树******************************************/
/********************************************************************************************/
template<class Elem>
class BinTree{
	
	public:
		BinNode<Elem>* root;
		
		void pre(BinNode<Elem>* root);
		void ipre(BinNode<Elem>* root);
		
		void mid(BinNode<Elem>* root);
		void imid(BinNode<Elem>* root);
		
		void pos(BinNode<Elem>* root);
		void ipos(BinNode<Elem>* root);
		
		void lay(BinNode<Elem>* root);
		
		BinNode<Elem>* find(Elem p,BinNode<Elem>* root);
	public:
		BinTree(){ root = NULL; };
		BinTree(Elem x){
			
			root = new BinNode<Elem>(x);
		}
		~BinTree(){}
		
		void preOrderTraversal();
		void midOrderTraversal();
		void posOrderTraversal();
		void layOrderTraversal();
		bool insert(Elem p,int LR,int data);
};

//前序递归:递和归(有去有回)
template<class Elem>
void BinTree<Elem>::pre(BinNode<Elem>* root){
	
	if(root == NULL)
		return;
	cout << root->data << " ";
	pre(root->left);
	pre(root->right);
}

//前序迭代(用栈数据结构实现):按顺序入栈,直到走不通出栈(入栈、出栈顺序按照前序方式)
template<class Elem>
void BinTree<Elem>::ipre(BinNode<Elem>* root) {

	stack<BinNode<Elem>*> st;
	
	while(root)
	{
		cout << root->data << " ";
		st.push(root);
		root = root->left;
		
		//不断判断节点是否为空(节点访问到头,回到前一个节点再判断)
		while(root == NULL && !st.empty())
		{
			root = st.top();
			st.pop();
			root = root->right;
		}
	}
}

//前序遍历
template<class Elem>
void BinTree<Elem>::preOrderTraversal(){

	cout << "1.递归方法:";
	pre(root);
	cout <<endl;
	cout << "2.迭代方法:";
	ipre(root);
}

//中序递归
template<class Elem>
void BinTree<Elem>::mid(BinNode<Elem>* root){

	if(root == NULL)
		return;
	mid(root->left);
	cout << root->data << " ";
	mid(root->right);

}

//中序迭代
template<class Elem>
void BinTree<Elem>::imid(BinNode<Elem>* root){
	
	stack<BinNode<Elem>*> st;
	
	while(root)
	{
		st.push(root);
		root = root->left;
		while(root == NULL && !st.empty())
		{
			root = st.top();
			st.pop();
			cout << root->data <<" ";
			root = root->right;
		}
	}

}

//中序遍历
template<class Elem>
void BinTree<Elem>::midOrderTraversal(){
	cout << "1.递归方法:";
	mid(root);
	cout <<endl;
	cout << "2.迭代方法:";
	imid(root);
}

//后序递归
template<class Elem>
void BinTree<Elem>::pos(BinNode<Elem>* root){
	
	if(root == NULL)
		return;
	pos(root->left);
	pos(root->right);
	cout << root->data << " ";

}

//后序迭代
template<class Elem>
void BinTree<Elem>::ipos(BinNode<Elem>* root){

	stack<BinNode<Elem>*> st;
	BinNode<Elem>* pre = NULL;
	
	while(root)
	{
		st.push(root);
		root = root->left;
		while(root == NULL && !st.empty())
		{
			root = st.top();
			if(pre == root->right || !root->right)
			{
				cout << root->data << " ";
				st.pop();
			}
			pre = root;
			root = root->right;
		}
	}

}

//后序遍历
template<class Elem>
void BinTree<Elem>::posOrderTraversal(){
	
	cout << "1.递归方法:";
	pos(root);
	cout <<endl;
	cout << "2.迭代方法:";
	//ipos(root);
	
}

//层序遍历(用队列实现)
template<class Elem>
void BinTree<Elem>::lay(BinNode<Elem>* root){

	queue<BinNode<Elem>*> q;
	q.push(root);
	
	while(root)
	{
		cout << root->data << " ";
		if(root->left)
			q.push(root->left);
		if(root->right)
			q.push(root->right);
		q.pop();
		root = q.front();
	}
	
}

template<class Elem>
void BinTree<Elem>::layOrderTraversal(){

	lay(root);
}

template<class Elem>
BinNode<Elem>* BinTree<Elem>::find(Elem p,BinNode<Elem>* root){

	BinNode<Elem>* temp = NULL;

	if(root == NULL)
		temp = NULL;
	else
	{
		if(root->data == p)
		{
			temp = root;
		}
		else
		{
			return find(p,root->left);
			return find(p,root->right);
		}
	}
	
	return temp;

}

template<class Elem>
bool BinTree<Elem>::insert(Elem p,int LR,int data){

	bool flag = false;
	
	BinNode<Elem>* temp = find(p,root);
	BinNode<Elem>* node = new BinNode<Elem>(data);
	if(temp)
	{
		if(LR == 0)
		{
			if(temp->left == NULL)
			{
				temp->left = node;
				flag = true;
			}
				
		}
		if(LR == 1)
		{
			if(temp->right == NULL)
			{
				temp->right = node;
				flag = true;
			}
		}
	}
	
	return flag;
}



/********************************************************************************************/
/********************************************二叉搜索树**************************************/
/********************************************************************************************/
template<class Elem>
class BSTree:public BinTree<Elem>{

	private:
		BinNode<Elem>* rfindMax(BinNode<Elem>* root);
		virtual BinNode<Elem>* rinsert(BinNode<Elem>* root,Elem x);
		BinNode<Elem>* remove(BinNode<Elem>* root,Elem x);

	public:
		BSTree(){ this->root = NULL;}
		BinNode<Elem>* findMax();
		BinNode<Elem>* findMin();
		BinNode<Elem>* findX(Elem x);
		bool insert(Elem x);
		bool remove(Elem x);

};

template<class Elem>
BinNode<Elem>* BSTree<Elem>::rfindMax(BinNode<Elem>* root){

	BinNode<Elem>* temp = NULL;

	if(root->right == NULL)
		temp = root;
	else
		root = rfindMax(root->right);
		
	return temp;
	
	
	
}

//二叉搜索树查找最大值
template<class Elem>
BinNode<Elem>* BSTree<Elem>::findMax(){

	BinNode<Elem>* temp = this->root;
	
	while(temp->right)
	{
		temp = temp->right;
	}
	
	return temp;
	
}

//二叉搜索树查找最小值
template<class Elem>
BinNode<Elem>* BSTree<Elem>::findMin(){

	BinNode<Elem>* temp = this->root;
	
	while(temp->left)
	{
		temp = temp->left;
	}
	
	return temp;
	
}

//二叉搜索树查找某值
template<class Elem>
BinNode<Elem>* BSTree<Elem>::findX(Elem x){
	
	BinNode<Elem>* temp = this->root;
	
	while(temp && x!=temp->data)
	{
		if(x < temp->data)
			temp = temp->left;
		else
			temp = temp->right;
	}
	
	return temp;
}

//二叉树插入递归
template<class Elem>
BinNode<Elem>* BSTree<Elem>::rinsert(BinNode<Elem>* root,Elem x){

	if(root == NULL)
	{
		root = new BinNode<Elem>(x);
		if(!root)
			throw -1;
	}
	else if(x < root->data)
		root->left = rinsert(root->left,x);
	else if(x > root->data)
		root->right = rinsert(root->right,x);
	else
		throw -2;
	
	return root;
	
}

//二叉搜索树插入
template<class Elem>
bool BSTree<Elem>::insert(Elem x){
	
	bool flag = false;
	try{
		this->root = rinsert(this->root,x);
		flag = true;
	}
	catch(int error){
		
		flag = false;
	}
	
	return flag;
	
	

}

//二叉搜索树删除递归
template<class Elem>
BinNode<Elem>* BSTree<Elem>::remove(BinNode<Elem>* root,Elem x){

	BinNode<Elem>* temp = NULL;
	
	if(!root) throw -1;
	else
	{
		if(x < root->data)
			root->left = remove(root->left,x);
		else if(x > root->data)
			root->right = remove(root->right,x);
		else
		{
			if( root->left && root->right)
			{
				temp = rfindMax(root->left);
				root->data = temp->data;
				root->left = remove(root->left,x);
			}
			else
			{
				temp = root;
				root = root->left ? root->left : root->right;
				delete temp;
			}
		}
	}
	return root;
}

//二叉搜索树删除
template<class Elem>
bool BSTree<Elem>::remove(Elem x){
	
	bool flag = false;
	try {
		this->root = remove(this->root,x);
		flag = true;
	}
	catch(int error){
		flag = false;
	}
	
	return flag;

}

/********************************************************************************************/
/********************************************平衡二叉树**************************************/
/********************************************************************************************/
template<class Elem>
class AVLTree:public BSTree<Elem>{

	protected:
		int height(BinNode<Elem>* r);
		BinNode<Elem>* rinsert(BinNode<Elem>* r,Elem x);
	public:
		BinNode<Elem>* LLrotate(BinNode<Elem>* r);
		BinNode<Elem>* RRrotate(BinNode<Elem>* r);
		BinNode<Elem>* LRrotate(BinNode<Elem>* r);
		BinNode<Elem>* RLrotate(BinNode<Elem>* r);
		
		AVLTree(){ this->root = NULL; };
		

};

//树高度
template<class Elem>
int AVLTree<Elem>::height(BinNode<Elem>* r){
	if(!r) return -1;
	return r->height;
}

//AVL树插入
template<class Elem>
BinNode<Elem>* AVLTree<Elem>::rinsert(BinNode<Elem>* r,Elem x){
	
	if(!r)
	{
		r = new BinNode<Elem>(x);
		if(!r) throw -1;
	}
	else if(x < r->data)
	{
		r->left = rinsert(r->left,x);
		//失去平衡
		if(height(r->left) - height(r->right) == 2)
		{
			if(x < r->left->data)
				r = LLrotate(r);
			else
				r = LRrotate(r);
		}
	}
	else if(x > r->data)
	{
		r->right = rinsert(r->right,x);
		if(height(r->right) - height(r->left) == 2)
		{
			if(x > r->right->data)
				r = RRrotate(r);
			else
				r = RLrotate(r);
		}
	}
	else throw -2;
	
	r->height = max(height(r->left),height(r->right))+1;
	
	return r;

}

//LL单旋
template<class Elem>
BinNode<Elem>* AVLTree<Elem>::LLrotate(BinNode<Elem>* r){
	
	BinNode<Elem>* child;
	child = r->left;
	r->left = child->right;
	child->right = r;
	
	r->height = max(height(r->left),height(r->right))+1;
	child->height = max(height(r->left),height(r->right))+1;

	return child;

}

//RR单旋
template<class Elem>
BinNode<Elem>* AVLTree<Elem>::RRrotate(BinNode<Elem>* r){

	BinNode<Elem>* child;
	child = r->right;
	r->right = child->left;
	child->left = r;
	
	r->height = max(height(r->left),height(r->right))+1;
	child->height = max(height(r->left),height(r->right))+1;

	return child;

}

//LR双旋
template<class Elem>
BinNode<Elem>* AVLTree<Elem>::LRrotate(BinNode<Elem>* r){
	
	r->left = RRrotate(r->left);
	r = LLrotate(r);
	return r;

}

//RL双旋
template<class Elem>
BinNode<Elem>* AVLTree<Elem>::RLrotate(BinNode<Elem>* r){
	
	r->right = LLrotate(r->right);
	r = RRrotate(r);
	return r;

}

/********************************************************************************************/
/********************************************主函数******************************************/
/********************************************************************************************/

int main(int argc,const char* argv[]){
	
	AVLTree<int> avl;
	avl.insert(8);
	avl.insert(7);
	avl.insert(6);
	avl.insert(5);
	avl.insert(10);
	avl.insert(9);
	
	avl.layOrderTraversal();
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值