AVL树


AVL树的性质
1. 左子树和右子树的高度之差的绝对值不超过1
2. 树中的每个左子树和右子树都是AVL树
3. 每个节点都有一个平衡因子(balance factor--bf),任一节点的平衡因子是-1,0,1。(每个节点的平衡因子等于右子树的高度减去左子
树的高度)

#pragma once



template<class K, class V>
struct AVLTreeNode
{
K _key;
V _value;


AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
AVLTreeNode<K, V>* _parent;


int _bf; // 平衡因子


AVLTreeNode(const K& key, const V& value)
:_key(key)
,_value(value)
,_left(NULL)
,_right(NULL)
,_parent(NULL)
,_bf(0)
{}
};


template<class K, class V>
class AVLTree
{
typedef AVLTreeNode<K, V> Node;
public:
AVLTree()
:_root(NULL)
{}


bool Remove(const K& key);


bool Insert(const K& key, const V& value)
{
// 1.插入节点
// 2.更新平衡因子,查看是否满足AVL树
// 3.不满足进行旋转
if (_root == NULL)
{
_root = new Node(key, value);
return true;
}


Node* parent = NULL;
Node* cur = _root;
while (cur)
{
if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else
{
return false;
}
}


cur = new Node(key, value);
if (parent->_key < key)
{
parent->_right = cur;
cur->_parent = parent;
}
else
{
parent->_left = cur;
cur->_parent = parent;
}


while(parent)
{
if (cur == parent->_left)
parent->_bf--;
else
parent->_bf++;


// 0, 子树的高度没变,不会对上层造成影响
if (parent->_bf == 0) 
{
break;
}
else if (parent->_bf == 1 || parent->_bf == -1)
{
cur = parent;
parent = cur->_parent;
}
else // 2 -2
{
if (parent->_bf == -2)
{
if(cur->_bf == -1)
RotateR(parent);
else
RotateLR(parent);


}


if (parent->_bf == 2)
{
if (cur->_bf == 1)
RotateL(parent);
else
RotateRL(parent);
}


break;
}
}


return true;
}


void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;


parent->_left = subLR;
if (subLR)
subLR->_parent = parent;


subL->_right = parent;


Node* ppNode = parent->_parent;
parent->_parent = subL;


if (ppNode == NULL)
{
_root = subL;
subL->_parent = NULL;
}
else
{
if (ppNode->_left == parent)
{
ppNode->_left = subL;
}
else
{
ppNode->_right = subL;
}


subL->_parent = ppNode;
}


subL->_bf = parent->_bf = 0;
}


void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;


parent->_right = subRL;
if (subRL)
subRL->_parent = parent;


subR->_left = parent;


Node* ppNode = parent->_parent;
parent->_parent = subR;


if (ppNode == NULL)
{
_root = subR;
subR->_parent = NULL;
}
else
{
if (ppNode->_left == parent)
{
ppNode->_left = subR;
}
else
{
ppNode->_right = subR;
}
subR->_parent = ppNode;
}


parent->_bf = subR->_bf = 0;
}


void RotateLR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
int bf = subLR->_bf;


RotateL(parent->_left);
RotateR(parent);


// 更正平衡因子
if (bf == 1)
{
parent->_bf = 0;
subL->_bf = -1;
}
else if (bf == -1)
{
subL->_bf = 0;
parent->_bf = 1;
}
else
{
subL->_bf = parent->_bf = 0;
}


subLR->_bf = 0;
}


void RotateRL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_bf;


RotateR(parent->_right);
RotateL(parent);


if (bf == 1)
{
subR->_bf = 0;
parent->_bf = -1;
}
else if (bf == -1)
{
parent->_bf = 0;
subR->_bf = 1;
}
else
{
parent->_bf = subR->_bf = 0;
}


subRL->_bf = 0;
}


void InOrder()
{
_InOrder(_root);
cout<<endl;
}


void _InOrder(Node* root)
{
if (root == NULL)
{
return;
}


_InOrder(root->_left);
cout<<root->_key<<" ";
_InOrder(root->_right);
}


// 腾讯
bool IsBalance()
{
return _IsBalance(_root);
}


int Height(Node* root)
{
if (root == NULL)
{
return 0;
}


int left = Height(root->_left);
int right = Height(root->_right);


return left > right ? left+1 : right+1;
}


// O(N^2)
bool _IsBalance(Node* root)
{
if (root == NULL)
{
return true;
}


int leftH = Height(root->_left);
int rightH = Height(root->_right);


if(rightH - leftH != root->_bf)
{
cout<<"平衡因子异常"<<root->_key<<endl;
}

return abs(rightH-leftH) < 2
&& _IsBalance(root->_left)
&& _IsBalance(root->_right);
}


// O(N)
bool _IsBalance(Node* root, int& height)
{
if (root == NULL)
{
height = 0;
return true;
}


int leftH, rightH;
if (false == _IsBalance(root->_left), leftH)
{
return false;
}


if (false == _IsBalance(root->_right, rightH))
{
return false;
}


if (abs(leftH-rightH) > 1)
{
return false;
}


height = leftH > rightH ? leftH+1:rightH+1;
return true;
}


int LeafNum(Node* root)
{
if (root == NULL)
return 0;


if (root->_left == NULL && root->_right==NULL)
return 1;


return LeafNum(root->_left) + LeafNum(root->_right);
}


protected:
Node* _root;
};


void TestTree()
{
int a[] = {16, 3, 7, 11, 9, 26, 18, 14, 15};
AVLTree<int, int> t;
for (size_t i = 0; i < sizeof(a)/sizeof(a[0]); ++i)
{
t.Insert(a[i], i);
}


t.InOrder();
cout<<"是否平衡?"<<t.IsBalance()<<endl;


// 特殊用例
int a1[] = {4, 2, 6, 1, 3, 5, 15, 7, 16, 14};


AVLTree<int, int> t1;
for (size_t i = 0; i < sizeof(a1)/sizeof(a1[0]); ++i)
{
t1.Insert(a1[i], i);
}


t1.InOrder();
cout<<"是否平衡?"<<t1.IsBalance()<<endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值