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

AVL树的基本操作

  1. //存储结构
  2. struct node
  3. {
  4. int data;
  5. int height; //记录当前子树的高度(叶子->根)
  6. //存储平衡因子的话,无法通过其子树算得该树的平衡因子
  7. node *lchild, *rchild;
  8. };
  9.  
  10. //新建结点
  11. node *newNode(int v)
  12. {
  13. node *root = new node;
  14. root->data = v;
  15. root->height = ;
  16. root->lchild = root->rchild = NULL;
  17. return root;
  18. }
  19.  
  20. //获取当前结点所在高度
  21. int GetHeight(node *root)
  22. {
  23. if(root == NULL)
  24. return ;
  25. return root->height;
  26. }
  27.  
  28. //计算结点的平衡因子
  29. int GetBalanceFactors(node *root)
  30. {
  31. return GetHeight(root->lchild)-GetHeight(root->rchild);
  32. }
  33.  
  34. //更新结点高度
  35. void UpdataHeight(node *root)
  36. {
  37. root->height = max(GetHeight(root->lchild), GetHeight(root->rchild))+;
  38. }
  39.  
  40. //查找
  41. void Search(node *root, int x)
  42. {
  43. if(root == NULL)
  44. return;
  45. if(x == root->data)
  46. //visit
  47. else if(x < root->data)
  48. Search(root->lchild, x);
  49. else
  50. Search(root->rchild, x);
  51. }
  52.  
  53. //左右旋互为逆操作
  54. //左旋
  55. void LeftRotation(node *&root)
  56. {
  57. node *temp = root->lchild; //temp指向新的根结点B
  58. root->rchild = temp->lchild; //B的左子树给A的右子树
  59. temp->lchild = root; //B的左子树变为A
  60. UpdataHeight(root); //更新结点高度
  61. UpdataHeight(temp);
  62. root = temp; //令B成为新的根结点
  63. }
  64.  
  65. //右旋
  66. void RightRotation(node *&root)
  67. {
  68. node *temp = root->lchild;
  69. root->lchild = temp->rchild;
  70. temp->rchild = root;
  71. UpdataHeight(root);
  72. UpdataHeight(temp);
  73. root = temp;
  74. }
  75.  
  76. /*
  77. 1.LL: A==+2, A->lchild=+1
  78. A作为root进行右旋
  79. 2.LR: A==+2, A->lchild=-1
  80. A->lchild作为root进行左旋 --> 转化为LL
  81. A作为root进行右旋
  82. 3.RR: A==-2, A->rchild=-1
  83. A作为root进行左旋
  84. 4.RL: A==-2, A->rchild=+1
  85. A->rchild作为root进行右旋 --> 转化为RR
  86. A作为root进行左旋
  87. */
  88.  
  89. //插入
  90. void Insert(node *&root, int v)
  91. {
  92. if(root == NULL)
  93. {
  94. root = newNode(v);
  95. return;
  96. }
  97. if(v < root->data)
  98. {
  99. Insert(root->lchild, v);
  100. UpdataHeight(root); //更新树高
  101. if(GetBalanceFactor(root) == )
  102. {
  103. if(GetBalanceFactor(root->lchild) == )
  104. RightRotation(root);
  105. else
  106. {
  107. LeftRotation(root->lchild);
  108. RightRotation(root);
  109. }
  110. }
  111. }
  112. else
  113. {
  114. Insert(root->rchild, v);
  115. UpdataHeight(root);
  116. if(GetBalanceFactor(root) == -)
  117. {
  118. if(GetBalanceFactor(root->rchild) == -)
  119. LeftRotation(root);
  120. else
  121. {
  122. RightRotation(root->rchild);
  123. LeftRotation(root);
  124. }
  125. }
  126. }
  127. }
  128.  
  129. //建立
  130. node *Create(int data[], int n)
  131. {
  132. node *root = NULL;
  133. for(int i=; i<n; i++)
  134. Insert(root, data[i]);
  135. return root;
  136. }

判断一棵树是否为AVL树

  1. #include <cstdio>
  2. const int M = ;
  3. int pre[M]={,,,,,,,,,};
  4. int in[M]={,,,,,,,,,};
  5. struct node
  6. {
  7. int data;
  8. node *lchild, *rchild;
  9. };
  10.  
  11. node *Create(int preL, int preR, int inL, int inR)
  12. {
  13. if(preL > preR)
  14. return NULL;
  15. node *root = new node;
  16. root->data = pre[preL];
  17. int k;
  18. for(k=inL; k<=inR; k++)
  19. if(in[k] == root->data)
  20. break;
  21. int numLeft = k-inL;
  22. root->lchild = Create(preL+, preL+numLeft, inL, k-);
  23. root->rchild = Create(preL+numLeft+, preR, k+, inR);
  24. }
  25.  
  26. int IsAvl = true;
  27. int IsAVL(node *root)
  28. {
  29. if(root == NULL)
  30. return -;
  31. int bl = IsAVL(root->lchild)+;
  32. int br = IsAVL(root->rchild)+;
  33. if(bl-br> || bl-br<-)
  34. IsAvl = false;
  35. return bl>br?bl:br;
  36. }
  37.  
  38. int main()
  39. {
  40. #ifdef ONLINE_JUDGE
  41. #else
  42. freopen("Test.txt", "r", stdin);
  43. #endif
  44.  
  45. node *root = Create(,M-,,M-);
  46. IsAVL(root);
  47. if(IsAvl)
  48. printf("Yes.");
  49. else
  50. printf("No.");
  51.  
  52. return ;
  53. }

平衡二叉树(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. code vs 3376 符号三角形

    3376 符号三角形  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 如下图是由14个“+”和14个“-”组 ...

  2. 机器学习1k近邻

    自己一直学习计算机视觉方面的东西,现在想学习一下数据挖掘跟搜索引擎,自己基础也有点薄弱,看朱明的那本数据挖掘,只能片面的了解这个数据挖掘.不过最近有一本书 机器学习实战,于是乎通过实战的形式了解一下基 ...

  3. webpack教程——css的加载

    首先要安装css的loader npm install css-loader style-loader --save-dev 然后在webpack.config.js中配置如下代码 意思是先用css- ...

  4. jq 抽奖

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  5. boost::shared_ptr

    boost::shared_ptr是boost库中用来管理指针的模板,使用它需要#include <boost/shared_ptr.hpp>.本文介绍它的一些基本用法. 第一,boost ...

  6. linux中shell命令test用法和举例

    shell test命令 和 [ 是同一个命令的不同名称. 原文:http://www.cnblogs.com/Jeff-Tang/p/5776947.html ------------------- ...

  7. 前台JSON字符串,spring mvc controller也接收字符串

    前台JSON字符串,spring mvc controller也接收字符串 前台: $.post(url, { data : JSON.stringify(obj) }, function(data) ...

  8. 最全Linux 与 Linux Windows 文件共享

    前提说明: windows主机信息:192.168.1.100 帐号:abc password:123 共享目录:share linux主机信息:192.168.1.200 帐号:def passwo ...

  9. ubuntu如何完全卸载和安装 Java及android环境?【转】

    本文转载自:https://my.oschina.net/lxrm/blog/110638 最近,迷上了java,一时间什么环境变量/虚拟机都猛然袭来,有点不适.环境配置在前,这所自然.平时搞PHP都 ...

  10. C# textbox中输入时加限制条件 // C#Winform下限制TextBox只能输入数字 // 才疏学浅(TextBox 小数点不能在首位+只能输入数字)

    textbox中输入时加限制条件 分类: C# winform2008-08-26 08:30 306人阅读 评论(0) 收藏 举报 textbox正则表达式object 1.用正则表达式! 2.使用 ...