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. Spring MVC-表单(Form)标签-单选按钮(RadioButton)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_radiobutton.htm 说明:示例基于Spring MVC 4.1.6. ...

  2. It&#39;s not a Bug, It&#39;s a Feature! (poj 1482 最短路SPFA+隐式图+位运算)

    Language: Default It's not a Bug, It's a Feature! Time Limit: 5000MS   Memory Limit: 30000K Total Su ...

  3. 2本Hadoop技术内幕电子书百度网盘下载:深入理解MapReduce架构设计与实现原理、深入解析Hadoop Common和HDFS架构设计与实现原理

    这是我收集的两本关于Hadoop的书,高清PDF版,在此和大家分享: 1.<Hadoop技术内幕:深入理解MapReduce架构设计与实现原理>董西成 著  机械工业出版社2013年5月出 ...

  4. BasePath问题-nginx负载均衡配置

    在配置nginx+tomcat好后.将项目加入到webapps中.发现訪问主页时,css与js訪问不到,导致主页布局出错.细致分析原因后发现css与js的地址是basePath得出的.而basePat ...

  5. C++对象内存布局 (二)

    在上一篇文章中讨论了C++单一一般继承的对象内存布局http://www.cnblogs.com/uangyy/p/4621561.html 接下来继续讨论第二种情况: 2.单一的虚拟继承:有成员变量 ...

  6. 在ubuntu中安装photoshop cs6

    对于很多专业的PS高手来说,真心难以找到顺手的且可以完美替代PS的软件,所以我们这里的解决办法就是用wine来安装. 虽然网上有很多的wine安装ps的方法,但是在使用过程往住会发生莫名其妙的崩溃,体 ...

  7. 使用ALSA编写自己的音频程序【转】

    本文转载自:http://blog.csdn.net/beyondioi/article/details/6994548 Alsa是Linux高级音频接口.面对众多的音频设备,Alsa为Linux音频 ...

  8. Appium + python - automator定位操作

    # coding:utf-8from appium import webdriverfrom time import sleep desired_caps = { 'platformName': 'A ...

  9. C/C++中的位运算符

    --------开始-------- 我自己都记不住这是第几次把这几个位运算符搞混了,刚好在刚用过来把这几个位运算符记下来,俗话说的好好记性不如个烂笔头. 运算符: 与           或    ...

  10. vue-pdf的使用方法及解决在线打印预览乱码

    最近在用vue做项目的时候,页面中需要展示后端返回的PDF文件,于是便用到了vue-pdf,其使用方法为 : npm install --save vue-pdf 官网地址:https://www.n ...