平衡二叉树(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 题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索 ...
随机推荐
- code vs 3376 符号三角形
3376 符号三角形 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 如下图是由14个“+”和14个“-”组 ...
- 机器学习1k近邻
自己一直学习计算机视觉方面的东西,现在想学习一下数据挖掘跟搜索引擎,自己基础也有点薄弱,看朱明的那本数据挖掘,只能片面的了解这个数据挖掘.不过最近有一本书 机器学习实战,于是乎通过实战的形式了解一下基 ...
- webpack教程——css的加载
首先要安装css的loader npm install css-loader style-loader --save-dev 然后在webpack.config.js中配置如下代码 意思是先用css- ...
- jq 抽奖
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- boost::shared_ptr
boost::shared_ptr是boost库中用来管理指针的模板,使用它需要#include <boost/shared_ptr.hpp>.本文介绍它的一些基本用法. 第一,boost ...
- linux中shell命令test用法和举例
shell test命令 和 [ 是同一个命令的不同名称. 原文:http://www.cnblogs.com/Jeff-Tang/p/5776947.html ------------------- ...
- 前台JSON字符串,spring mvc controller也接收字符串
前台JSON字符串,spring mvc controller也接收字符串 前台: $.post(url, { data : JSON.stringify(obj) }, function(data) ...
- 最全Linux 与 Linux Windows 文件共享
前提说明: windows主机信息:192.168.1.100 帐号:abc password:123 共享目录:share linux主机信息:192.168.1.200 帐号:def passwo ...
- ubuntu如何完全卸载和安装 Java及android环境?【转】
本文转载自:https://my.oschina.net/lxrm/blog/110638 最近,迷上了java,一时间什么环境变量/虚拟机都猛然袭来,有点不适.环境配置在前,这所自然.平时搞PHP都 ...
- C# textbox中输入时加限制条件 // C#Winform下限制TextBox只能输入数字 // 才疏学浅(TextBox 小数点不能在首位+只能输入数字)
textbox中输入时加限制条件 分类: C# winform2008-08-26 08:30 306人阅读 评论(0) 收藏 举报 textbox正则表达式object 1.用正则表达式! 2.使用 ...