平衡二叉树(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 题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索 ...
随机推荐
- saprk里面的action - aggregate
上一篇讲到了spark里面的action函数: Action列表: reduce collect count first take takeSample takeOrdered saveAsTextF ...
- HDU 5293
树上DP题. 其实有点类似于01的问题.方程很容易想到.首先,因为一条链的节点其实都是在树上的,所以很容易想到应该先求一个LCA.然后,当某节点不是链的LCA时,它的转移就是: dp[i]=sum[i ...
- NPM 3 Beta为Windows用户带来利好消息
本文来源于我在InfoQ中文站翻译的文章,原文地址是:http://www.infoq.com/cn/news/2015/06/angular-2-react-native-roadmap 近日,np ...
- Java:Socket通信
Socket通常也称作"套接字".用于描写叙述IP地址和port,是一个通信链的句柄.应用程序通常通过"套接字"向网络发出请求或者应答网络请求. ServerS ...
- c++ 基于Policy 的 模板编程
在没真正接触c++ 模板编程之前.真的没有想到c++ 还能够这么用.最大的感触是:太灵活了,太强大了. 最初接触模板威力还是在Delta3d中,感觉里面的模板使用实在是灵活与方便,特别是dtAI中使 ...
- Openfire 配置连接SQL SERVER(非默认实例)
安装好Openfire之后,紧接着进行配置. 连接数据库的时候遇上问题. 打算用我本机上的一个SQL SERVER做为数据库.但是,我本机装了几个SQL SERVER实例,现在我打算使用的是那个非默认 ...
- Git 少用 Pull 多用 Fetch 和 Merge 【已翻译100%】【转】
本文转载自:https://www.oschina.net/translate/git-fetch-and-merge?lang=chs&page=1# 本文有点长而且有点乱,但就像Mark ...
- Coursera Algorithms week1 算法分析 练习测验: 3Sum in quadratic time
题目要求: Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case ...
- python-day2 切片,格式化输出,函数
1.切片:取元素 格式;变量名[M:N:K] M 表示开始元素索引值, N 表示结束元素索引值(不包含索引值本身) K 表示步长,隔几个切一次 例子:a='hello python' p ...
- Python 39 数据库的数据类型
一:整型 为什么需要 数据分类? 1.为了描述事物更加准确 2.描述起来更方便 3.节省内存空间 例:1 a 你 utf8 下 5个字节 1 a b c unicode 6个字节 mysq ...