平衡二叉树(Self-balancing Binary Search Tree)
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)的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- Method for balancing binary search trees
Method for balancing a binary search tree. A computer implemented method for balancing a binary sear ...
- 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 ...
- pat1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- 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 ...
- 【数据结构05】红-黑树基础----二叉搜索树(Binary Search Tree)
目录 1.二分法引言 2.二叉搜索树定义 3.二叉搜索树的CRUD 4.二叉搜索树的两种极端情况 5.二叉搜索树总结 前言 在[算法04]树与二叉树中,已经介绍过了关于树的一些基本概念以及二叉树的前中 ...
- LeetCode 108. 将有序数组转换为二叉搜索树(Convert Sorted Array to Binary Search Tree) 14
108. 将有序数组转换为二叉搜索树 108. Convert Sorted Array to Binary Search Tree 题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索 ...
随机推荐
- [bzoj2086][Poi2010]Blocks_单调栈_双指针
Blocks bzoj-2086 Poi-2010 题目大意:题目链接. 注释:略. 想法:首先,不难发现,如果连续的一段数的平均值不小于输入的k的话,这段数是满足题意的. 所以,我们再次简化一下:将 ...
- N天学习一个linux命令之umask
前言 umask不是linux命令,而是shell内置的指令,俗称用户权限掩码,用于对用户创建的文件和目录设置默认权限.默认的权限掩码是0022,也就是说新创建的文件权限是0644,新创建的目录权限是 ...
- Hibernate学习笔记(八) — 懒载入与抓取策略
懒载入(Load On Demand)是一种独特而又强大的数据获取方法,它可以在用户滚动页面的时候自己主动获取很多其它的数据,而新得到的数据不会影响原有数据的显示,同一时候最大程度上降低server端 ...
- cocos2dx编译安卓版本号查看C++错误
首先,在Mac以下相关软件路径,打开"终端",然后输入 pico .bash_profile 回车 export COCOS2DX_ROOT=/Users/bpmacmini0 ...
- Android实战简易教程-第十五枪(实现ListView中Button点击事件监听)
1.main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...
- 架构师速成5.1-小学gtd进阶
人生没有理想,那和咸鱼有什么差别. 有了理想怎样去实现.这就是gtd须要解决的问题.简单说一下gtd怎么做? 确定你的目标.假设不能确定长期目标,至少须要一个2年到3年的目标. 目标必须是能够衡量的, ...
- 《黑马程序猿》 cocos2d游戏引擎复习笔记一
/** ----------------------------游戏场景的搭建-------------------------------- 1首先创建一个surfaceview ,它能够在子线程中 ...
- SecureCRT——设置打印中文字符
1. 设置方法 使用SecureCRT打印由STM32发送的中文字符提示信息,显示乱码.在网上找了一些链接,再加上自己摸索,终于出了能够让SecureCRT打印中文的方法. 设置以下几个地方即可. 1 ...
- 没有终结点在侦听可以接受消息的 http://192.168.1.63:8085/LoginService。这通常是由于不正确的地址或者 SOAP 操作导致的
2016-04-08 09:15:05,581 [8] ERROR System.Threading.Thread - ErrorSystem.ServiceModel.EndpointNotFoun ...
- (转)dp动态规划分类详解
dp动态规划分类详解 转自:http://blog.csdn.NET/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间 ...