Date: 2019-04-11 18:49:18

AVL树的基本操作

 //存储结构
struct node
{
int data;
int height; //记录当前子树的高度(叶子->根)
//存储平衡因子的话,无法通过其子树算得该树的平衡因子
node *lchild, *rchild;
}; //新建结点
node *newNode(int v)
{
node *root = new node;
root->data = v;
root->height = ;
root->lchild = root->rchild = NULL;
return root;
} //获取当前结点所在高度
int GetHeight(node *root)
{
if(root == NULL)
return ;
return root->height;
} //计算结点的平衡因子
int GetBalanceFactors(node *root)
{
return GetHeight(root->lchild)-GetHeight(root->rchild);
} //更新结点高度
void UpdataHeight(node *root)
{
root->height = max(GetHeight(root->lchild), GetHeight(root->rchild))+;
} //查找
void Search(node *root, int x)
{
if(root == NULL)
return;
if(x == root->data)
//visit
else if(x < root->data)
Search(root->lchild, x);
else
Search(root->rchild, x);
} //左右旋互为逆操作
//左旋
void LeftRotation(node *&root)
{
node *temp = root->lchild; //temp指向新的根结点B
root->rchild = temp->lchild; //B的左子树给A的右子树
temp->lchild = root; //B的左子树变为A
UpdataHeight(root); //更新结点高度
UpdataHeight(temp);
root = temp; //令B成为新的根结点
} //右旋
void RightRotation(node *&root)
{
node *temp = root->lchild;
root->lchild = temp->rchild;
temp->rchild = root;
UpdataHeight(root);
UpdataHeight(temp);
root = temp;
} /*
1.LL: A==+2, A->lchild=+1
A作为root进行右旋
2.LR: A==+2, A->lchild=-1
A->lchild作为root进行左旋 --> 转化为LL
A作为root进行右旋
3.RR: A==-2, A->rchild=-1
A作为root进行左旋
4.RL: A==-2, A->rchild=+1
A->rchild作为root进行右旋 --> 转化为RR
A作为root进行左旋
*/ //插入
void Insert(node *&root, int v)
{
if(root == NULL)
{
root = newNode(v);
return;
}
if(v < root->data)
{
Insert(root->lchild, v);
UpdataHeight(root); //更新树高
if(GetBalanceFactor(root) == )
{
if(GetBalanceFactor(root->lchild) == )
RightRotation(root);
else
{
LeftRotation(root->lchild);
RightRotation(root);
}
}
}
else
{
Insert(root->rchild, v);
UpdataHeight(root);
if(GetBalanceFactor(root) == -)
{
if(GetBalanceFactor(root->rchild) == -)
LeftRotation(root);
else
{
RightRotation(root->rchild);
LeftRotation(root);
}
}
}
} //建立
node *Create(int data[], int n)
{
node *root = NULL;
for(int i=; i<n; i++)
Insert(root, data[i]);
return root;
}

判断一棵树是否为AVL树

 #include <cstdio>
const int M = ;
int pre[M]={,,,,,,,,,};
int in[M]={,,,,,,,,,};
struct node
{
int data;
node *lchild, *rchild;
}; node *Create(int preL, int preR, int inL, int inR)
{
if(preL > preR)
return NULL;
node *root = new node;
root->data = pre[preL];
int k;
for(k=inL; k<=inR; k++)
if(in[k] == root->data)
break;
int numLeft = k-inL;
root->lchild = Create(preL+, preL+numLeft, inL, k-);
root->rchild = Create(preL+numLeft+, preR, k+, inR);
} int IsAvl = true;
int IsAVL(node *root)
{
if(root == NULL)
return -;
int bl = IsAVL(root->lchild)+;
int br = IsAVL(root->rchild)+;
if(bl-br> || bl-br<-)
IsAvl = false;
return bl>br?bl:br;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif node *root = Create(,M-,,M-);
IsAVL(root);
if(IsAvl)
printf("Yes.");
else
printf("No."); return ;
}

平衡二叉树(Self-balancing Binary Search Tree)的更多相关文章

  1. Leetcode No.108 Convert Sorted Array to Binary Search Tree(c++实现)

    1. 题目 1.1 英文题目 Given an integer array nums where the elements are sorted in ascending order, convert ...

  2. [LeetCode] 108. Convert Sorted Array to Binary Search Tree ☆(升序数组转换成一个平衡二叉树)

    108. Convert Sorted Array to Binary Search Tree 描述 Given an array where elements are sorted in ascen ...

  3. What is the difference between a binary tree, a binary search tree, a B tree and a B+ tree?

    Binary Tree : It is a tree data structure in which each node has at most two children. As such there ...

  4. Method for balancing binary search trees

    Method for balancing a binary search tree. A computer implemented method for balancing a binary sear ...

  5. Convert Sorted Array to Binary Search Tree leetcode java

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  6. pat1099. Build A Binary Search Tree (30)

    1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  7. LeetCode108——Convert Sorted Array to Binary Search Tree

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  8. 【数据结构05】红-黑树基础----二叉搜索树(Binary Search Tree)

    目录 1.二分法引言 2.二叉搜索树定义 3.二叉搜索树的CRUD 4.二叉搜索树的两种极端情况 5.二叉搜索树总结 前言 在[算法04]树与二叉树中,已经介绍过了关于树的一些基本概念以及二叉树的前中 ...

  9. LeetCode 108. 将有序数组转换为二叉搜索树(Convert Sorted Array to Binary Search Tree) 14

    108. 将有序数组转换为二叉搜索树 108. Convert Sorted Array to Binary Search Tree 题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索 ...

随机推荐

  1. LA 4794 状态DP+子集枚举

    状态压缩DP,把切割出的面积做状态压缩,统计出某状态下面积和. 设f(x,y,S)为在状态为S下在矩形x,y是否存在可能划分出S包含的面积.若S0是S的子集,对矩形x,y横切中竖切,对竖切若f(x,k ...

  2. HDU 4542

    T_T终于让我过了,坑啊,竟然时限是200ms... 我是预处理出不整除了个数的,因为这个较容易一点.利用算术基本定理,f=p1^a1*p2^a2...... 所以,整除它的个数就是(a1+1)*(a ...

  3. java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

    1.错误描写叙述 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String service. ...

  4. 怎样使用ListView实现一个带有网络请求,解析,分页,缓存的公共的List页面来大大的提高工作效率

    在寻常的开发中常常会有非常多列表页面.每做一个列表页就须要创建这个布局文件那个Adapter适配器文件等等一大堆与之相关的附属的不必要的冗余文件. 假设版本号更新迭代比較频繁,如此以往,就会使项目pr ...

  5. Python字符串转为字典方法大全

    方法一: 通过内置函数eval str_info = '{"name": "test", "age": 18}' dict_info = e ...

  6. Linux -- 内存控制之oom killer机制及代码分析

    近期,线上一些内存占用比較敏感的应用.在訪问峰值的时候,偶尔会被kill掉,导致服务重新启动.发现是Linux的out-of-memory kiiler的机制触发的. http://linux-mm. ...

  7. stack 栈

    其实今天我们主要讲的是搜索,但是留作业不知道怎么就突然全变成栈了. 其实栈和队列没什么区别,只是一个先进先出,一个先进后出.基本操作也是一样的. 栈(stack)又名堆栈,它是一种运算受限的线性表.其 ...

  8. php处理类

    Thomas Boutell 以及众多的开发者创造了以GD图形库闻名的一个图形软件库,用于动态的图形计算. GD提供了对于诸如C, Perl, Python, PHP, OCaml等等诸多编程语言的支 ...

  9. Comet反向ajax技术实现客服聊天系统

    说明:Comet反向Ajax是在看了燕十八老师的视频以后,结合他讲解的例子,自己用ajax+java实现了一遍.在这里把代码贴出来,以供大家学习.同时,ajax轮询技术也可以用在消息推送的功能中,下次 ...

  10. Python 29 异常处理, 元类

    所学内容 异常处理(常用) AttributeError ··························  试图访问一个对象没有的树形,比如foo.x,但是foo没有属性xIOError ··· ...