AVL_Tree

一、AVL树的性质:

1、左子树和右子树的高度之差绝对值不超过1.

2、树中的每个左子树和右子树都是AVL树。

3、每个节点都有一个平衡因子,任一点的平衡因子是-1、0、1,(每个节点的平衡因子等于右子树的高度减去左子树的高度)

二、AVL树的效率:

一颗AVL树有N个节点,其高度可以保持在log2N,插入/删除/查找的时间复杂度也是log2N。


代码实现:

#pragma once


template<class K, class V>
struct AVLTreeNode
{
AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
AVLTreeNode<K, V>* _parent;


K _key;
V _value;


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 Insert(const K& key, const V& value)
{
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;
}


// todo
// 更新平衡因子
while (parent)
{
if (parent->_right == cur)
parent->_bf++;
else
parent->_bf--;


if (parent->_bf == 0)
{
break;
}
else if (parent->_bf == 1 || parent->_bf == -1)
{
cur = parent;
parent = cur->_parent;
}
else if (parent->_bf == 2 || parent->_bf == -2)
{
// 旋转
if (parent->_bf == -2)
{
if (cur->_bf == -1)
{
RotateR(parent);
}
else
{
RotateLR(parent);
}
}
else // 2
{
if (cur->_bf  == 1)
{
RotateL(parent);
}
else
{
RotateRL(parent);
}
}


break;
}
else
{
assert(false);
}
}



return true;
}


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


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


subR->_left = parent;
Node* parentParent = parent->_parent;
parent->_parent = subR;


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


subR->_parent = parentParent;
}


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

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


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


subL->_right = parent;
Node* parentParent = parent->_parent;
parent->_parent = subL;


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


subL->_parent = parentParent;
}


parent->_bf = subL->_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;
subRL->_bf = 0;
}
else if (bf == -1)
{
parent->_bf = 0;
subR->_bf = 1;
subRL->_bf = 0;
}
else
{
parent->_bf = subRL->_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 == 0)
{
parent->_bf = subL->_bf = subLR->_bf = 0;
}
else if (bf == 1)
{
parent->_bf = 0;
subL->_bf = -1;
subLR->_bf = 0;
}
else if (bf == -1)
{
subL->_bf = 0;
parent->_bf = 1;
subLR->_bf = 0;
}
else
{
assert(false);
}
}


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()
{
int height = 0;
return _IsBalance(_root, height);
}


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


int l = Height(root->_left);
int r = Height(root->_right);


return l > r ? l+1:r+1;
}


// 要求优化到O(N)
bool _IsBalance(Node* root)
{
if(root == NULL)
return true;


int leftHeight = Height(root->_left);
int rightHeight = Height(root->_right);


return abs(leftHeight-rightHeight) < 2
&& _IsBalance(root->_left)
&& _IsBalance(root->_right);
}


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


int leftHeight = 0;
if(_IsBalance(root->_left, leftHeight) == false)
return false;


int rightHeight = 0;
if(_IsBalance(root->_right, rightHeight) == false)
return false;


height = leftHeight > rightHeight ? leftHeight+1 : rightHeight+1;


if (rightHeight - leftHeight != root->_bf)
{
cout<<"平衡因子异常"<<root->_key<<endl;
return false;
}


return abs(leftHeight- rightHeight) < 2;
}


private:
Node* _root;
};


void TestAVLTree()
{
int a[] = {4, 2, 6, 1, 3, 5, 15, 7, 16, 14, 16, 30, 7, 11, 9, 26, 18, 14, 19};
AVLTree<int, int> tree;
for (size_t i = 0; i < sizeof(a)/sizeof(a[0]); ++i)
{
tree.Insert(a[i], i);
cout<<"IsBalance:"<<tree.IsBalance()<<"->Insert"<<a[i]<<endl;
}
tree.InOrder();
cout<<"IsBalance:"<<tree.IsBalance()<<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值