AVL二叉树

   在计算机科学中,AVL树是最先发明的自平衡二叉查找树。在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树。查找、插入和删除在平均和最坏情况下都是O(log n)。增加和删除可能需要通过一次或多次树旋转来重新平衡这个树。AVL树得名于它的发明者 G.M. Adelson-Velsky 和 E.M. Landis。AVL处理了搜索二叉树的特殊情况。

  AVL树中的每个左子树和右子树都是AVL树 ;每个节点都有一个平衡因子(balance factor--bf),任一节点的平衡因子是-1,0,1。(每个节点的平衡因子等于右子树的高度减去左子 树的高度 )    


#include<iostream>
#include<cstdlib>
using namespace std;
template<class K,class V>
struct AVLNode
{
 K _key;
 V value;
 AVLNode<K,V>* left;
 AVLNode<K,V>* right;
 AVLNode<K,V>* _parent;
 int _bf;
 AVLNode(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 AVLNode<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* cur=_root;
  Node* parent=NULL;
  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!=NULL)                                                    //在合适的位置插入节点
  {
  if(parent->_key<key)
  {
   parent->right=cur;
   
  }
  else if(parent->_key>key)
  {
   parent->left=cur;
   cur->_parent=parent;
  }
  }
  while(parent)                                                            //调节平衡因子
  {

if(parent->left==cur)
    parent->_bf--;
   else
    parent->_bf++;
   if(parent->_bf==0)
    return true;
   else if(parent->_bf==1||parent->_bf==-1)
   {
    cur=parent;
    parent=cur->_parent;
   }
   else
   {
    if(parent->_bf==2)                          //不满足情况时进行旋转
    {
     if(cur->_bf==1)
      RotateL(parent);
     if(cur->_bf==-1)
      RotateRL(parent);
    }
    else
    {
     if(cur->_bf==-1)
      RotateR(parent);
     if(cur->_bf==1)
      RotateLR(parent);

    }
    break;
   }
  }
  return true;
 }


bool IsAVLtree(Node* root,int& h)                             //递归判断是否满足AVL树
 {
  if(root==NULL)
   h=0;
  return true;
  int hR=0;
  int hL=0;
  if(IsAVLtree(root->left)==false)
   return false;
  if(IsAVLtree(root->right)==false)
   return false;
  h=hR>hL?hR:hL;
  return abs(hR-hL)<2;
 }


protected:

  void RotateL(Node* parent)
  {
   Node* subR=parent->right;
   Node* subRL=subR->left;
   parent->right=subR;
   parent->left=parent;
   if(subRL)
    subRL->_parent=parent;

    subR->left=parent;
    Node* pnode=parent->_parent;
    parent->_parent=subR;
    if(pnode==NULL)
    {
     _root=subR;
     subR->_parent=NULL;
    }
    else
    {
     if(pnode->left=parent)
      pnode->left=subR;
     else
      pnode->right=subR;
         subR->_parent=pnode;
    }
   parent->_bf=subR->_bf=0;
  }



  void RotateR(Node* parent)
  {
   Node* subL=parent->left;
   Node* subLR=subL->right;
   parent->left=subLR;
   subL->right=parent;
   if(subLR)
    subLR->_parent=parent;

    subL->right=parent;
    Node* pnode=parent->_parent;
    parent->_parent=subL;
    if(pnode==NULL)
    {
     _root=subL;
     subL->_parent=NULL;
    }
    else
    {
     if(pnode->left==parent)
     pnode->left=subL;
     else
      pnode->right=subL;
         subL->_parent=pnode;
    }
    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==0)
   subR->_bf=subRL->_bf=parent->_bf=0;
  if(bf==-1)
  {
   subR->_bf=1;
   parent->_bf=0;
   subRl->_bf=0;
  }
  if(bf==1)
  {
   subL->_bf=0;
   subLR->_bf=0;
   parent->_bf=-1;

  }
 }

 void RotateLR(Node* parent)
 {
  Node* subL=parent->left;
  Node* subLR=subL->right;
  int bf=subLR->_bf;
  RotateL(parent->left);
  RotateR(parent);
  if(bf==0)
   subL->_bf=subLR->_bf=parent->_bf=0;
  if(bf==-1)
  {
   subL->_bf=0;
   parent->_bf=1;
   subLR->_bf=0;
  }
  if(bf==1)
  {
   subL->_bf=-1;
   subLR->_bf=0;
   parent->_bf=0;

  }

 } 
private:
 Node* _root;
};
int main()
{
 int a[9]={16, 3, 7, 11, 9, 26, 18, 14, 15};
 AVLTree<int,int> tree;
 for(int i=0;i<9;i++)
 {
  tree.Insert(a[i],i);
 }
 system("pause");
 return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值