平衡树

作者: iamsonormal2333 | 来源:发表于2016-11-13 17:22 被阅读20次

AVLtree.h

#ifndef _AVLTREE_H_
#define _AVLTREE_H_

#include <iostream>

namespace model_AVLtree {
using std::cout;
#define nullptr 0
  class AVLtree {
      private:  
    struct node {
        int item;
        int bf;
        node *rchild,*lchild;
    };
    void visit( node *p );
    void destory( node *p );
    void R_Rotate( node *&p );
    void L_Rotate( node *&p );
    void AVL_insert( node *&p, int num, bool &tall );
    bool AVL_find( node *p, int n );
    void Left_balance( node *&p );
    void Right_balance( node *&p );
    node *head;
      public:
    AVLtree(){ head = nullptr; }
    ~AVLtree();
    void output();
    void insert( int n);
    bool find( int n);
  };

  void AVLtree::R_Rotate( node *&p ) {
      node *q = p->lchild;
      p->lchild = q->rchild;
      q->rchild = p;
      p = q;
  }

  void AVLtree::L_Rotate( node *&p ) {
      node *q = p->rchild;
      p->rchild = q->lchild;
      q->lchild = p;
      q = p;
  }

  void AVLtree::Left_balance( node *&p ) {
      node *q = p->lchild;
      switch (q->bf) {
    case 1:
       p->bf = q->bf = 0;
       R_Rotate(p);
       break;
    case -1:
       node *k = q->rchild;
       switch (k->bf) {
          case 1:p->bf = -1; q->bf = 0;break;
          case -1:p->bf = 0; q->bf = -1;break;
           }
       k->bf = 0;
       L_Rotate(q);
       R_Rotate(p);
      }
  }

  void AVLtree::Right_balance( node *&p ) {
      node *q = p->rchild;
      switch (q->bf) {
    case -1:
       p->bf = q->bf = 0;
       L_Rotate(p);
       break;
    case 1:
      node *k = q->lchild;
       switch (k->bf) {
          case 1:p->bf = 0;q->bf = -1;break;
          case -1:p->bf = 1;q->bf = 0;break;
       }
       k->bf = 0;
       R_Rotate(q);
       L_Rotate(p);
      }
  }


  void AVLtree::AVL_insert( node *&now, int num, bool &tall) {
      if(now == nullptr){
         now = new node;
         now->item = num;
         now->rchild = now->lchild = nullptr;
         now->bf = 0;
     tall = true;
         return ;
      }
      if(num < now->item) {
         AVL_insert( now->lchild, num, tall);
         if(tall) 
        switch (now->bf) {
           case 1:Left_balance(now); tall = false; break;
           case 0:now->bf = 1; break;
           case -1:now->bf = 0; tall = false; break;
        }
      }
      else {
     AVL_insert( now->rchild, num, tall );
     if(tall)
        switch (now->bf) {
               case -1:Right_balance(now); tall = false; break;
               case 0:now->bf = 1; break;
               case 1:now->bf = 0; tall = false;  break;
            }
      }
  }

  AVLtree::~AVLtree() {
     destory( head );
  }

  void AVLtree::destory( node *p ) {
     if(p == nullptr) return;
     destory(p->lchild);
     destory(p->rchild);
     delete p;
  }

  void AVLtree::insert( int n ) {
     bool tall = false;
     AVL_insert( head, n, tall );
  }
  
  bool AVLtree::find( int n ) {
     if(AVL_find( head, n ))return true;
     return false;;
  }

  bool AVLtree::AVL_find( node *p, int n ) {
     if(!p) return false;
     if(n == p->item) return true;
     if(AVL_find( p->lchild, n )) return true;
     if(AVL_find( p->rchild, n )) return true;
     return false;
  }

  void AVLtree::visit( node *p ) {
     if(!p) return;
     visit(p->lchild);
     cout<<p->item<<" ";
     visit(p->rchild); 
  }

  void AVLtree::output(){
     visit(head);
  }

}
#endif

测试文件AVLtree.cpp,头文件中只有插入,没有删除操作。

#include "AVLtree.h"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using namespace model_AVLtree;

int main() {
   int n,a;
   int i;
   AVLtree s;
   cin>>n;
   for( i=0; i<n; i++) {
      cin>>a;
      s.insert(a);
      if(s.find(a))cout<<"get"<<endl;
   }
   s.output(); 
   return 0;
}

相关文章

  • 数据结构和算法树的进阶(八)

    平衡树 常见平衡树:平衡二叉查找树,2-3查找树,AVL树, 红黑树 2-3查找树 概述:保证查找树的平衡性,我们...

  • 平衡二叉树与红黑树的对比

    AVL树(平衡二叉树) AVL树是带有平衡条件的二叉查找树,一般是用平衡因子差值判断是否平衡并通过旋转来实现平衡,...

  • 数据结构与算法(十三)平衡二叉树之AVL树

    本文主要包括以下内容: 平衡二叉树的概念 AVL树 插入操作保持AVL树的平衡 删除操作保持AVL树的平衡 平衡二...

  • 阿里腾讯面试官问为什么Mysql用B+树做索引而不用B-树或红黑

    说这个面试题,先来回顾一下B+树、B-树、平衡二叉树、红黑树的概念 平衡二叉树 平衡二叉树又被称为AVL树 平衡二...

  • 平衡树

    Binary Index Tree AVL Splay Treap Scapegoat Tree Treap(wi...

  • 平衡树

    平衡树(英语:Self-balancing binary search tree)Wiki 特点 平衡树是改进的二...

  • 平衡树

    百科定义 平衡二叉树(Balanced Binary Tree)具有以下性质:它是一 棵空树或它的左右两个子树的高...

  • 平衡树

    AVLtree.h 测试文件AVLtree.cpp,头文件中只有插入,没有删除操作。

  • 平衡树

    平衡二叉树 左右子树高度相差不超过1 适用于查询多,修改少 红黑树 没有一条路径比其他路径长出两倍:一条全黑,另一...

  • Balanced Binary Tree

    Easy 给定二叉树,判断其是否为平衡树。 Solution: 什么是平衡树? 空树平衡,非空二叉树满足下面条件时...

网友评论

    本文标题:平衡树

    本文链接:https://www.haomeiwen.com/subject/gxucpttx.html