前言
AVL 是一个高度平衡的二叉搜索树,主要是为了减少查找的次数而降低树的高度。 关于二叉搜索树主要是关于二叉树的左旋和右旋以及无法平衡的四种状态 左旋 :主要是右子树的高度过高了,所以要左旋,就是根节点与其右孩子交换一下,此时右孩子当作了根节点了,而之间的根节点就作为右孩子的左孩子,但是之前的右孩子是可能存在它的左子树的,这时就可以把这左子树连接之前根节点的右边作为根节点的右子树 右旋与左旋是对称的,就不再赘述 不平衡的四种状态 LL :即在根节点的左孩子的左子树上添加导致不平衡了,此时需要根节点右旋即可。 RR :与上面对称,就不再赘述 LR :即在根节点的左孩子的右子树上添加导致不平衡,此时需要先对左孩子进行左旋,再对根节点进行右旋 RL :与上面类似,就不再赘述 以上都是自己为加深理解而做的些许陈述,想要更好的理解科参考这篇文章,当然代码可以参考我的。 AVL树详解篇
代码
#include<iostream>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
typedef int elemType;
#define nullptr NULL
#define endl "\n"
struct TreeNode{
elemType value;
TreeNode*left;
TreeNode*right;
TreeNode():value(0),left(nullptr),right(nullptr){};
TreeNode(elemType data):value(data),left(nullptr),right(nullptr){};
};
elemType TreeDeleteMin(TreeNode*);
TreeNode*root_node=nullptr;
int getTreeHight(TreeNode*root){
if(root==nullptr){
return 0;
}
return max(getTreeHight(root->left),getTreeHight(root->right))+1;
}
int getTreeBalanceFactor(TreeNode*root){
int L=0,R=0;
if(root->left)L=getTreeHight(root->left);
if(root->right)R=getTreeHight(root->right);
return L-R;
}
TreeNode*TreeRoateRight(TreeNode*root){
TreeNode*tree_ptr=root->left;
root->left=root->left->right;
tree_ptr->right=root;
return tree_ptr;
}
TreeNode*TreeRoateLeft(TreeNode*root){
TreeNode*tree_ptr=root->right;
root->right=root->right->left;
tree_ptr->left=root;
return tree_ptr;
}
TreeNode*RBalance(TreeNode*root){
int factor=getTreeBalanceFactor(root);
if(factor>1&&getTreeBalanceFactor(root->left)>0){
cout<<"LL型"<<endl;
return TreeRoateRight(root);
}else if(factor>1&&getTreeBalanceFactor(root->left)<0){
cout<<"LR型"<<endl;
root->left=TreeRoateLeft(root->left);
return TreeRoateRight(root);
}else if(factor<-1&&getTreeBalanceFactor(root->right)<0)
{
cout<<"RR型"<<endl;
return TreeRoateLeft(root);
}else if(factor<-1&&getTreeBalanceFactor(root->right)>0)
{
cout<<"RL型"<<endl;
root->right=TreeRoateRight(root->right);
return TreeRoateLeft(root);
}else return root;
}
TreeNode* insert(TreeNode*root,int value){
if(root==nullptr){
root=new TreeNode(value);
return root;
}else if(value<root->value){
root->left=insert(root->left,value);
}else{
root->right=insert(root->right,value);
}
return RBalance(root);
}
TreeNode*pre;
int deleteMin(TreeNode*root)
{
if(root->left==nullptr)
{
pre->left=root->right;
elemType ans=root->value;
delete root;
root=nullptr;
return ans;
}
pre=root;
return deleteMin(root->left);
}
TreeNode*deleteNode(TreeNode*root,int value)
{
if(root==nullptr)return nullptr;
if(root->value==value)
{
if(root->right)
{
if(root->right->left)
{
root->value=deleteMin(root->right);
}
else
{
root->value=root->right->value;
TreeNode*node=root->right;
root->right=root->right->right;
delete node;
node=nullptr;
}
}else
{
TreeNode*node=root;
root=root->left;
delete node;
node=nullptr;
}
return root;
}else if(root->value<value)
{
root->right=deleteNode(root->right,value);
}else{
root->left=deleteNode(root->left,value);
}
return RBalance(root);
}
void traverse(TreeNode*root)
{
if(root==nullptr)return;
traverse(root->left);
cout<<root->value<<" ";
traverse(root->right);
}
void dfs(TreeNode*root)
{
if(root==nullptr)return;
cout<<root->value<<" ";
dfs(root->left);
dfs(root->right);
}
void bsf(TreeNode*root)
{
if(root==nullptr)return;
queue<TreeNode*>que;
que.push(root);
while(que.size())
{
TreeNode*node=que.front();
cout<<node->value<<" ";
que.pop();
if(node->left)que.push(node->left);
if(node->right)que.push(node->right);
}
}
int main(){
while(1)
{
vector<elemType>vec;
int data=-1;
cout<<"请输入加入的节点值(-1结束):"<<endl;
while(1)
{
cin>>data;
if(data==-1)break;
vec.push_back(data);
}
for(int i=0;i<vec.size();i++)
{
root_node=insert(root_node,vec[i]);
}
traverse(root_node);
cout<<endl;
cout<<"平衡因子为:"<<getTreeBalanceFactor(root_node)<<endl;
cout<<"树的高度为:"<<getTreeHight(root_node)<<endl;
int deleteData;
while(1)
{
cout<<"请输入要要删除的节点的值(-1退出):";
cin>>deleteData;
if(deleteData==-1)break;
deleteNode(root_node,deleteData);
traverse(root_node);
cout<<endl;
cout<<"平衡因子为:"<<getTreeBalanceFactor(root_node)<<endl;
cout<<"树的高度为:"<<getTreeHight(root_node)<<endl;
}
root_node=nullptr;
}
return 0;
}
结果
在删除只剩两个节点的时候出现了错误,但也不知道错在哪里,就不搞了-.-
|