当前位置:   article > 正文

Java实现AVL的一系列操作_avlnode

avlnode

1、定义AVL(带有平衡条件的二叉查找树)的结构的定义

  1. //定义平衡二叉树Avl的结构
  2. public class AvlNode<T extends Comparable<T>> {
  3. public AvlNode(T value){
  4. this(value, null, null);
  5. }
  6. public AvlNode(T value, AvlNode lt, AvlNode rt){
  7. data = value;
  8. left = lt;
  9. right = rt;
  10. height = 0;
  11. }
  12. T data; //键值
  13. AvlNode<T> left; //左孩子结点
  14. AvlNode<T> right; //右孩子结点
  15. int height; //树的高度
  16. }
2、AVL树中的一系列方法的实现(每种方法实现的过程都在注释中进行描述)

  1. package 树;
  2. /**
  3. * 平衡二叉树的一系列操作:
  4. * 1、计算树的高度height(AvlNode<T> t)
  5. * 2、四种旋转实现(LL,LR,RL,RR)
  6. * 3、AVL树中插入节点的操作,返回插入后的结点
  7. * 4、AVL树的删除结点操作
  8. * 5、AVL树中删除key值的结点
  9. * 6、非递归实现查找tree中的key值
  10. * 7、寻找最大节点&结点的最大值
  11. * 8、寻找最小节点&结点的最小值
  12. * 9、前序遍历Avl树---“根--左--右”(递归)
  13. * 10、中序遍历Avl树---“左--根--右”(递归)
  14. * 11、后序遍历Avl树---“左--右--根”(递归)
  15. * */
  16. public class AVL<T extends Comparable<T>> {
  17. //计算树的高度
  18. public int height(AvlNode<T> t){
  19. return t == null ? -1 : t.height;
  20. }
  21. //LL:单选转
  22. public AvlNode<T> leftLeftRotate(AvlNode<T> k2){
  23. AvlNode<T> k1 = k2.left;
  24. k2.left = k1.right;
  25. k1.right = k2;
  26. k2.height = Math.max(height(k2.left), height(k2.right)) + 1;
  27. k1.height = Math.max(height(k1.left), k2.height);
  28. return k1;
  29. }
  30. //RR:单旋转
  31. public AvlNode<T> rightRightRotate(AvlNode<T> k1){
  32. AvlNode<T> k2;
  33. k2 = k1.right;
  34. k1.right = k2.left;
  35. k2.left = k1;
  36. k1.height = Math.max(height(k1.left), height(k2.left)) + 1;
  37. k2.height = Math.max(k1.height, height(k2.right)) + 1;
  38. return k1;
  39. }
  40. //LR:双旋转-----先RR后LL
  41. public AvlNode<T> leftRightRotate(AvlNode<T> k3){
  42. k3.left = rightRightRotate(k3.left);
  43. return leftLeftRotate(k3);
  44. }
  45. //RL:双旋转-----先LL后RR
  46. public AvlNode<T> rightLeftRotate(AvlNode<T> k1){
  47. k1.left = leftLeftRotate(k1.left);
  48. return rightRightRotate(k1);
  49. }
  50. //AVL树中插入节点的操作,返回插入后的结点
  51. public AvlNode<T> insert(T key, AvlNode<T> tree){
  52. //创建一个新的结点
  53. if(tree == null){
  54. return new AvlNode<T>(key,null,null);
  55. }
  56. int cmp = key.compareTo(tree.data);
  57. //在tree的左结点上插入
  58. if(cmp < 0){
  59. tree.left = insert(key, tree.left);
  60. if(height(tree.left) - height(tree.right) == 2){
  61. if(key.compareTo(tree.left.data) < 0){
  62. //LL型:单旋转
  63. tree = leftLeftRotate(tree);
  64. }else{
  65. //LR型:双旋转
  66. tree = leftRightRotate(tree);
  67. }
  68. }
  69. }else if(cmp > 0){
  70. tree.right = insert(key, tree.right);
  71. if(height(tree.right) - height(tree.left) == 2){
  72. if(key.compareTo(tree.right.data) > 0){
  73. //RR型:单旋转
  74. tree = rightRightRotate(tree);
  75. }else{
  76. //RL型:双旋转
  77. tree = rightLeftRotate(tree);
  78. }
  79. }
  80. }else{
  81. //相等的情况
  82. return null;
  83. }
  84. // 插入后 树的高度
  85. tree.height = Math.max(height(tree.left), height(tree.right)) + 1;
  86. return tree;
  87. }
  88. //AVL树的删除操作,其中要删除的结点是del,tee是树的根结点
  89. public AvlNode<T> remove(AvlNode<T> tree, AvlNode<T> del){
  90. if(tree == null || del == null){//合法性的判断
  91. return null;
  92. }
  93. int cmp = del.data.compareTo(tree.data);//判断要删除结点的值和根节点值的大小
  94. if(cmp < 0){//del在根结点的左子树上
  95. tree.left = remove(tree.left , del);
  96. //删除结点del后,要判断tree的高度是否平衡,不平衡要调整
  97. if(height(tree.right) - height(tree.left) == 2){//tree的left删除结点后,高度有可能小于right的高度,故是用tree.right-tree.left==2来判断
  98. AvlNode<T> temR = tree.right;
  99. if(height(temR.left) > height(temR.right)){//针对tree来说是RL
  100. tree = rightLeftRotate(tree);
  101. }else{
  102. tree = rightRightRotate(tree);
  103. }
  104. }
  105. }else if(cmp > 0){//要删除的结点在树根的右子树上
  106. tree.right = remove(tree.right, del);
  107. //删除结点del后,要判断tree的高度是否平衡,不平衡要调整
  108. if(height(tree.left) - height(tree.right) == 2){//tree的right删除结点后,高度有可能是left大于right,若是则调整
  109. AvlNode<T> temL = tree.left;//要调整的是左子树
  110. if(height(temL.right) > height(temL.left)){//LR
  111. tree = leftRightRotate(tree);
  112. }else{
  113. tree = leftLeftRotate(tree);
  114. }
  115. }
  116. }else{//tree是对应要删除的结点
  117. //1.tree的左右子树都为非空
  118. if(tree.left != null && tree.right != null){
  119. if(height(tree.left) > height(tree.right)){
  120. AvlNode<T> max = maxNode(tree.left);//找出左子树的最大值
  121. tree.data = max.data;
  122. tree.left = remove(tree.left, max);
  123. }else{
  124. AvlNode<T> min = minNode(tree.right);//找出右子树的最小值替换tree
  125. tree.data = min.data;
  126. tree.right = remove(tree.right, min);
  127. }
  128. }else{
  129. AvlNode<T> temp = tree;
  130. tree = (tree.left != null) ? tree.left : tree.right;
  131. temp = null;
  132. }
  133. }
  134. return tree;
  135. }
  136. //删除tree中值为key的结点
  137. public void remove(AvlNode<T> tree, T key){
  138. AvlNode<T> temp;
  139. if((temp = search(tree, key)) != null){
  140. tree = remove(tree, temp);
  141. }
  142. }
  143. /*
  144. * 非递归实现查找tree中的key值
  145. * */
  146. public AvlNode<T> search(AvlNode<T> tree, T key){
  147. if(tree == null){
  148. return null;
  149. }
  150. while(tree != null){
  151. int cmp = key.compareTo(tree.data);
  152. if(cmp < 0){//在tree的左子树上
  153. tree = tree.left;
  154. }else if(cmp > 0){//在tree的右子树上
  155. tree = tree.right;
  156. }else{
  157. return tree;
  158. }
  159. }
  160. return null;
  161. }
  162. //寻找最大节点
  163. public AvlNode<T> maxNode(AvlNode<T> tree){
  164. if(tree == null){
  165. return null;
  166. }
  167. while(tree.right != null){
  168. tree = tree.right;
  169. }
  170. return tree;
  171. }
  172. //最大节点的value值
  173. public T maxValue(AvlNode<T> tree){
  174. AvlNode<T> p = maxNode(tree);
  175. if(p != null){
  176. return p.data;
  177. }
  178. return null;
  179. }
  180. //寻找最小结点
  181. public AvlNode<T> minNode(AvlNode tree){
  182. if(tree == null){
  183. return null;
  184. }
  185. while(tree.left != null){
  186. tree = tree.left;
  187. }
  188. return tree;
  189. }
  190. //最小结点的value值
  191. public T minValue(AvlNode<T> tree){
  192. AvlNode<T> p = minNode(tree);
  193. if(p != null){
  194. return p.data;
  195. }
  196. return null;
  197. }
  198. //前序遍历Avl树---“根--左--右”
  199. public void preOrder(AvlNode<T> tree){
  200. if(tree != null){
  201. System.out.print(tree.data + ",");
  202. preOrder(tree.left);
  203. preOrder(tree.right);
  204. }
  205. }
  206. //中序遍历Avl树---“左--根--右”
  207. public void inOrder(AvlNode<T> tree){
  208. if(tree != null){
  209. inOrder(tree.left);
  210. System.out.print(tree.data + ",");
  211. inOrder(tree.right);
  212. }
  213. }
  214. //后序遍历Avl树---“左--右--根”
  215. public void postOrder(AvlNode<T> tree){
  216. if(tree != null){
  217. postOrder(tree.left);
  218. postOrder(tree.right);
  219. System.out.print(tree.data + ",");
  220. }
  221. }
  222. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/632182
推荐阅读
相关标签
  

闽ICP备14008679号