Avl树即左右子树的深度【高度】相差不可超过1,所以在插入key的时候,就会出现需要旋转【更改根节点】的操作

下面是源代码:

  1. /*
  2. the define of avltree's node
  3. */
  4. class MyNode {
  5. int key, height;
  6. MyNode left, right;
  7.  
  8. MyNode(int d) {
  9. key = d;
  10. height = 1;
  11. }
  12. }
  13.  
  14. public class MyAvlTree {
  15. MyNode root;
  16.  
  17. /*
  18. the function of get_tree_height
  19. */
  20. int getHeight(MyNode node) {
  21. if (node == null)
  22. return 0;
  23. return node.height;
  24. }
  25.  
  26. /*
  27. the function of get the max of two numbers
  28. */
  29. int max(int a, int b) {
  30. return (a > b) ? a : b;
  31. }
  32.  
  33. /*
  34. the function of right rotate subtree rooted y
  35. */
  36. MyNode rightRoate(MyNode y) {
  37. MyNode x = y.left;
  38. MyNode t = x.right;
  39.  
  40. /*
  41. perform rotation
  42. */
  43. x.right = y;
  44. y.left = t;
  45.  
  46. /*
  47. update the heights
  48. */
  49. y.height = max(getHeight(y.left), getHeight(y.right)) + 1;
  50. x.height = max(getHeight(x.left), getHeight(x.right)) + 1;
  51. // return new root
  52. return x;
  53. }
  54.  
  55. /*
  56. the function of left rotate subtree rooted x
  57. */
  58. MyNode leftRoate(MyNode x) {
  59. MyNode y = x.right;
  60. MyNode t = y.left;
  61.  
  62. /*
  63. perform rotation
  64. */
  65. y.left = x;
  66. x.right = t;
  67.  
  68. /*
  69. update the heights
  70. */
  71. x.height = max(getHeight(x.left), getHeight(x.right));
  72. y.height = max(getHeight(y.left), getHeight(y.right));
  73. //return new root
  74. return x;
  75. }
  76.  
  77. /*
  78. get balance factor of node n
  79. */
  80. int getBalance(MyNode node) {
  81. if (node == null)
  82. return 0;
  83. return getHeight(node.left) - getHeight(node.right);
  84. }
  85.  
  86. /*
  87. the function of insert
  88. */
  89. MyNode insertKey(MyNode n, int key) {
  90. if (n == null)
  91. return (new MyNode(key));
  92. if (key > n.key)
  93. n.right = insertKey(n.right, key);
  94. else if (key < n.key)
  95. n.left = insertKey(n.left, key);
  96. else
  97. return n;
  98.  
  99. /*
  100. update height
  101. */
  102. n.height = 1 + max(getHeight(n.left), getHeight(n.right));
  103.  
  104. //get balance
  105. int balance = getBalance(n);
  106.  
  107. /*
  108. there are four cases
  109. */
  110. //left-left case
  111. if (balance > 1 && key < n.left.key)
  112. return rightRoate(n);
  113. //right-right case
  114. if (balance > 1 && key > n.right.key)
  115. return leftRoate(n);
  116. //left-right case
  117. if (balance > 1 && key > n.left.key) {
  118. n.left = leftRoate(n.left);
  119. return rightRoate(n);
  120. }
  121. //right-left case
  122. if (balance > 1 && key < n.right.key) {
  123. n.right = rightRoate(n.right);
  124. return leftRoate(n);
  125. }
  126. return n;
  127. }
  128.  
  129. /*
  130. the functionn of preOrder
  131. */
  132. void preOrder(MyNode node) {
  133. if (node != null) {
  134. System.out.print(node.key + " ");
  135. preOrder(node.left);
  136. preOrder(node.right);
  137. }
  138. }
  139.  
  140. /*
  141. the test example
  142. */
  143. public static void main(String[] args) {
  144. MyAvlTree tree = new MyAvlTree();
  145.  
  146. //the test
  147. tree.root = tree.insertKey(tree.root, 10);
  148. tree.root = tree.insertKey(tree.root, 20);
  149. tree.root = tree.insertKey(tree.root, 30);
  150. tree.root = tree.insertKey(tree.root, 40);
  151. tree.root = tree.insertKey(tree.root, 50);
  152. tree.root = tree.insertKey(tree.root, 25);
  153.  
  154. System.out.println("Preorder traversal" +
  155. " of constructed tree is : ");
  156. tree.preOrder(tree.root);
  157. }
  158. }

Java实现Avl树的更多相关文章

  1. Java数据结构——AVL树

    AVL树(平衡二叉树)定义 AVL树本质上是一颗二叉查找树,但是它又具有以下特点:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树,并且拥有自平衡机制.在AV ...

  2. JAVA数据结构--AVL树的实现

    AVL树的定义 在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为1,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下的时间复杂度都是.增 ...

  3. AVL树(三)之 Java的实现

    概要 前面分别介绍了AVL树"C语言版本"和"C++版本",本章介绍AVL树的Java实现版本,它的算法与C语言和C++版本一样.内容包括:1. AVL树的介绍 ...

  4. AVL树原理及实现(C语言实现以及Java语言实现)

    欢迎探讨,如有错误敬请指正 如需转载,请注明出处http://www.cnblogs.com/nullzx/ 1. AVL定义 AVL树是一种改进版的搜索二叉树.对于一般的搜索二叉树而言,如果数据恰好 ...

  5. AVL树----java

                                                                                        AVL树----java AVL ...

  6. AVL树之 Java的实现

    AVL树的介绍 AVL树是高度平衡的而二叉树.它的特点是:AVL树中任何节点的两个子树的高度最大差别为1. 上面的两张图片,左边的是AVL树,它的任何节点的两个子树的高度差别都<=1:而右边的不 ...

  7. AVL树的Java实现

    AVL树:平衡的二叉搜索树,其子树也是AVL树. 以下是我实现AVL树的源码(使用了泛型): import java.util.Comparator; public class AVLTree< ...

  8. AVL树的JAVA实现及AVL树的旋转算法

    1,AVL树又称平衡二叉树,它首先是一颗二叉查找树,但在二叉查找树中,某个结点的左右子树高度之差的绝对值可能会超过1,称之为不平衡.而在平衡二叉树中,任何结点的左右子树高度之差的绝对值会小于等于 1. ...

  9. 【Java】 大话数据结构(12) 查找算法(3) (平衡二叉树(AVL树))

    本文根据<大话数据结构>一书及网络资料,实现了Java版的平衡二叉树(AVL树). 平衡二叉树介绍 在上篇博客中所实现的二叉排序树(二叉搜索树),其查找性能取决于二叉排序树的形状,当二叉排 ...

随机推荐

  1. (开发)ESLint - 代码规范

    参考文档:http://eslint.cn/ ESLint 是在 ECMAScript/JavaScript 代码中识别和报告模式匹配的工具,它的目标是保证代码的一致性和避免错误.在许多方面,它和 J ...

  2. ECMAScript Regex

    Everything has its own regulation by defining its grammar. ECMAScript regular expressions pattern sy ...

  3. python模块详解 sys shutil

    sys模块 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的版本信息 sy ...

  4. 学习lucene5.5.4的笔记

    说说几个常用的类. OpenMode是一个枚举类,有三个元素,分别表示IndexWriter的打开模式. CREATE:每次打开IndexWriter时清空当前索引目录下的索引,再新建索引. APPE ...

  5. April 3 2017 Week 14 Monday

    Don't worry about finding your soul mate. Find yourself. 欲寻佳侣,先觅本心. You may fail to find your soul m ...

  6. IOS 拼接按钮文字

    NSMutableString *tempAnswerTitle=[[NSMutableString alloc]init]; for(UIButton *answerBtn in self.answ ...

  7. G711格式语音采集/编码/转码/解码/播放

    2019-05-01 语音g711格式和AMR格式类似,应用很简单,很多人已经整理过了,收录于此,以备不时之需,用别人现成的足矣,我们的时间应该用来干更有意义的事. 1.PCM to G711 Fas ...

  8. iframe的Dom操作

    我最近遇到这样一个需求, 抛开业务相关不谈,但从技术上说:页面中选择公司中的页面,在iframe中展示被选的页面,并且要对页面做一些Dom相关的处理.也就是说我们需要在父级页面中操作子页面(ifram ...

  9. left join后面加上where条件浅析

    select a.*,b.* from table1 a left join table2 b on b.X=a.X where XXX 如上:一旦使用了left join,没有where条件时,左表 ...

  10. C#使用ref和out传递数组

    C#使用ref和out传递数组 一.使用ref参数传递数组 数组类型的ref参数必须由调用方明确赋值.因此,接受方不需要明确赋值.接受方数组类型的ref参数能够修改调用方数组类型的结果.可以将接受方的 ...