数据结构算法——1071. 平衡二叉树

题目

在这里插入图片描述

思路

AVL树的实现,先用一下网上别人的代码,后续再自己完善

代码

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
 
template <typename keyType>//之后的类名一定要记得加上<typename> 
class BinaryTreeNode{
public:
	keyType value;
	BinaryTreeNode *m_pLeft = NULL;
	BinaryTreeNode *m_pRight = NULL;
	BinaryTreeNode(keyType v):value(v),m_pLeft(NULL),m_pRight(NULL){
        m_pLeft = NULL;
        m_pRight = NULL;    
    }
};
 
template <typename keyType>
class BT{
	typedef BinaryTreeNode<keyType> BTNode;//给结点定义别名 
public:
	BTNode * insertBTNode(BTNode *&root,keyType val);
	void createBT(keyType arr[],int n);//创建二叉排序树,通过*&来改变指针的值
 
	void midorder_showBT();//中序遍历 
	void midorder_showBT_core(const BTNode *root); 
	
	void level_showBT();//中序遍历 
	void level_showBT_core(BTNode *root);
	
	~BT();//析构函数 
	void release_BT_core(BTNode *root);	
	
	int _getHeight(BTNode *root);
	int getHeight();//获取高度
	
	int diff(BTNode *root);//计算平衡因子
	
	BTNode *balance(BTNode *root);//平衡操作 
	
	BTNode *LL_Rotation(BTNode *root);
	BTNode *LR_Rotation(BTNode *root);
	BTNode *RL_Rotation(BTNode *root);
	BTNode *RR_Rotation(BTNode *root);
			
	BTNode *root;	
};
 
 
int main(){
	
    int n ;
    cin >> n;
	BT<int> bt;
	int* arr = new int[n];
    for(int i = 0; i < n; i++)
        cin >> arr[i];
	bt.createBT(arr,n);//构建二叉排序树 
	
    cout << bt.root->value << " "<< bt._getHeight(bt.root);
	// cout<<"中序遍历:"<<endl;
	// bt.midorder_showBT();//中序遍历打印 
	// cout<<endl<<endl;
	
	// cout<<"层次遍历:"<<endl;
	// bt.level_showBT();//前序遍历打印 
	// cout<<endl<<endl;
	
	// cout<<"树高:"; 
	// cout<<bt.getHeight()<<endl<<endl;
	
	// cout<<"树的根结点平衡因子:"; 
	// cout<<bt.diff(bt.root)<<endl<<endl;//求平衡因子 
	
	return 0;
}
 
template <typename keyType>
void BT<keyType>::createBT(keyType arr[],int n){
	root = NULL;
	for(int i=0;i<n;i++)
		insertBTNode(root,arr[i]);	
}
 
template <typename keyType>
BinaryTreeNode<keyType> * BT<keyType>::insertBTNode(BTNode *&root,keyType val){
	if(root == NULL){
		root = new BTNode(val);
		return root;	
	}
	if(val == root->value){
		return root;
		
	}
	else if(val < root->value){
		//root->m_pLeft = insertBTNode(root->m_pLeft,val);
		insertBTNode(root->m_pLeft,val);
		root = balance(root); //注意这里是把平衡后的返回置temp赋值给root 
		return root;
	}
	else{
		//root->m_pRight = insertBTNode(root->m_pRight,val);
		insertBTNode(root->m_pRight,val);
		root = balance(root); 
		return root;	
	}			
}
 
template <typename keyType>
BinaryTreeNode<keyType> *BT<keyType>::balance(BTNode *root){
	int dis = diff(root);//计算结点的平衡因子 
	if(dis > 1){//左 
		if( diff(root->m_pLeft) > 0)
			return LL_Rotation(root);
		else
			return LR_Rotation(root);
	} 
	else if(dis < -1){//右 
		if( diff(root->m_pRight) < 0)
			return RR_Rotation(root);
		else
			return RL_Rotation(root);
	}
	
	return root;//无需转换时记得返回root 
	
}
 
template <typename keyType>
BinaryTreeNode<keyType> *BT<keyType>::LL_Rotation(BTNode *root){
	BTNode *temp = root->m_pLeft;
	root->m_pLeft = temp->m_pRight;
	temp->m_pRight = root;
	return temp;//返回要旋转子树的主结点 
}
 
template <typename keyType>
BinaryTreeNode<keyType> *BT<keyType>::RR_Rotation(BTNode *root){
	BTNode *temp = root->m_pRight;
	root->m_pRight = temp->m_pLeft;
	temp->m_pLeft = root;
	return temp;
}
 
template <typename keyType>
BinaryTreeNode<keyType> *BT<keyType>::LR_Rotation(BTNode *root){//先进行RR操作,再进行LL操作 
	
	//注意这里一定要对root->m_Pleft重新赋值 
	root->m_pLeft = RR_Rotation(root->m_pLeft);//先对root后的左结点进行RR操作 
	return LL_Rotation(root);//再对root进行LL操作 
}
 
template <typename keyType>
BinaryTreeNode<keyType> *BT<keyType>::RL_Rotation(BTNode *root){//先进行LL操作,再进行RR操作 
 
	//注意这里一定要对root->m_pRight重新赋值 
	root->m_pRight = LL_Rotation(root->m_pRight);//先对root后的右结点进行LL操作 
	return RR_Rotation(root);//再对root进行RR操作 
}
 
template <typename keyType>
void BT<keyType>::midorder_showBT(){
	midorder_showBT_core(root);
}
 
template <typename keyType>
void BT<keyType>::midorder_showBT_core(const BTNode *root){
	if(root == NULL){
		return;
	}	
	
	midorder_showBT_core(root->m_pLeft);
	cout<<root->value<<" ";
	midorder_showBT_core(root->m_pRight);
}
 
template <typename keyType>
void BT<keyType>::level_showBT()//中序遍历
{
	level_showBT_core(root);
}
 
template <typename keyType>
void BT<keyType>::level_showBT_core(BTNode *root){
	if(root == NULL)
		return;
	queue<BinaryTreeNode<keyType> *> que;
	que.push(root);
	while(!que.empty()){
		
		if( que.front()->m_pLeft != NULL)
			que.push(que.front()->m_pLeft);
			
		if(que.front()->m_pRight != NULL)
			que.push(que.front()->m_pRight);
			
		cout<<que.front()->value<<" ";
		que.pop();
	}
}
 
template <typename keyType>
BT<keyType>::~BT()//释放二叉树 
{
	release_BT_core(root);
}
 
template <typename keyType>
void BT<keyType>::release_BT_core(BTNode *root)//释放二叉树 
{
	if(root == NULL)
		return;
	release_BT_core(root->m_pLeft);
	release_BT_core(root->m_pRight);
	delete root;
	root = NULL;
	return ;
}
 
template <typename keyType>
int BT<keyType>::getHeight(){
	_getHeight(root);	 
} 
 
template <typename keyType>
int BT<keyType>::_getHeight(BTNode *root){
	if(root == NULL)
		return 0;

    int n = 0 , m = 0;
    if(root->m_pLeft != NULL)
        n = _getHeight(root->m_pLeft);
    if(root->m_pRight != NULL)
        m = _getHeight(root->m_pRight);
	return ( n > m ? n : m ) + 1;
} //
 
template <typename keyType>
int BT<keyType>::diff(BTNode *root){
	if(root == NULL)
		return 0;
	return _getHeight(root->m_pLeft) - _getHeight(root->m_pRight);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值