AVL树(三)之 Java的实现
2014-03-30 21:56
2086 查看
概要
前面分别介绍了AVL树"C语言版本"和"C++版本",本章介绍AVL树的Java实现版本,它的算法与C语言和C++版本一样。内容包括:1. AVL树的介绍
2. AVL树的Java实现
3. AVL树的Java测试程序
转载请注明出处:https://www.geek-share.com/detail/2607187761.html
更多内容: 数据结构与算法系列 目录
(01) AVL树(一)之 图文解析 和 C语言的实现
(02) AVL树(二)之 C++的实现
(03) AVL树(三)之 Java的实现
AVL树的介绍
AVL树是高度平衡的而二叉树。它的特点是:AVL树中任何节点的两个子树的高度最大差别为1。/** * Java 语言: AVL树 * * @author skywang * @date 2013/11/07 */ public class AVLTreeTest { private static int arr[]= {3,2,1,4,5,6,7,16,15,14,13,12,11,10,8,9}; public static void main(String[] args) { int i; AVLTree<Integer> tree = new AVLTree<Integer>(); System.out.printf("== 依次添加: "); for(i=0; i<arr.length; i++) { System.out.printf("%d ", arr[i]); tree.insert(arr[i]); } System.out.printf("\n== 前序遍历: "); tree.preOrder(); System.out.printf("\n== 中序遍历: "); tree.inOrder(); System.out.printf("\n== 后序遍历: "); tree.postOrder(); System.out.printf("\n"); System.out.printf("== 高度: %d\n", tree.height()); System.out.printf("== 最小值: %d\n", tree.minimum()); System.out.printf("== 最大值: %d\n", tree.maximum()); System.out.printf("== 树的详细信息: \n"); tree.print(); i = 8; System.out.printf("\n== 删除根节点: %d", i); tree.remove(i); System.out.printf("\n== 高度: %d", tree.height()); System.out.printf("\n== 中序遍历: "); tree.inOrder(); System.out.printf("\n== 树的详细信息: \n"); tree.print(); // 销毁二叉树 tree.destroy(); } }
View Code
AVL树的Java测试程序
AVL树的测试程序运行结果如下:== 依次添加: 3 2 1 4 5 6 7 16 15 14 13 12 11 10 8 9 == 前序遍历: 7 4 2 1 3 6 5 13 11 9 8 10 12 15 14 16 == 中序遍历: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 == 后序遍历: 1 3 2 5 6 4 8 10 9 12 11 14 16 15 13 7 == 高度: 5 == 最小值: 1 == 最大值: 16 == 树的详细信息: 7 is root 4 is 7's left child 2 is 4's left child 1 is 2's left child 3 is 2's right child 6 is 4's right child 5 is 6's left child 13 is 7's right child 11 is 13's left child 9 is 11's left child 8 is 9's left child 10 is 9's right child 12 is 11's right child 15 is 13's right child 14 is 15's left child 16 is 15's right child == 删除根节点: 8 == 高度: 5 == 中序遍历: 1 2 3 4 5 6 7 9 10 11 12 13 14 15 16 == 树的详细信息: 7 is root 4 is 7's left child 2 is 4's left child 1 is 2's left child 3 is 2's right child 6 is 4's right child 5 is 6's left child 13 is 7's right child 11 is 13's left child 9 is 11's left child 10 is 9's right child 12 is 11's right child 15 is 13's right child 14 is 15's left child 16 is 15's right child
下面,我们对测试程序的流程进行分析!
1. 新建AVL树
2. 依次添加"3,2,1,4,5,6,7,16,15,14,13,12,11,10,8,9" 到AVL树中。
2.01 添加3,2
添加3,2都不会破坏AVL树的平衡性。
2.02 添加1
添加1之后,AVL树失去平衡(LL),此时需要对AVL树进行旋转(LL旋转)。旋转过程如下:
2.03 添加4
添加4不会破坏AVL树的平衡性。
2.04 添加5
添加5之后,AVL树失去平衡(RR),此时需要对AVL树进行旋转(RR旋转)。旋转过程如下:
2.05 添加6
添加6之后,AVL树失去平衡(RR),此时需要对AVL树进行旋转(RR旋转)。旋转过程如下:
2.06 添加7
添加7之后,AVL树失去平衡(RR),此时需要对AVL树进行旋转(RR旋转)。旋转过程如下:
2.07 添加16
添加16不会破坏AVL树的平衡性。
2.08 添加15
添加15之后,AVL树失去平衡(RR),此时需要对AVL树进行旋转(RR旋转)。旋转过程如下:
2.09 添加14
添加14之后,AVL树失去平衡(RL),此时需要对AVL树进行旋转(RL旋转)。旋转过程如下:
2.10 添加13
添加13之后,AVL树失去平衡(RR),此时需要对AVL树进行旋转(RR旋转)。旋转过程如下:
2.11 添加12
添加12之后,AVL树失去平衡(LL),此时需要对AVL树进行旋转(LL旋转)。旋转过程如下:
2.12 添加11
添加11之后,AVL树失去平衡(LL),此时需要对AVL树进行旋转(LL旋转)。旋转过程如下:
2.13 添加10
添加10之后,AVL树失去平衡(LL),此时需要对AVL树进行旋转(LL旋转)。旋转过程如下:
2.14 添加8
添加8不会破坏AVL树的平衡性。
2.15 添加9
但是添加9之后,AVL树失去平衡(LR),此时需要对AVL树进行旋转(LR旋转)。旋转过程如下:
3. 打印树的信息
输出下面树的信息:
前序遍历: 7 4 2 1 3 6 5 13 11 9 8 10 12 15 14 16
中序遍历: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
后序遍历: 1 3 2 5 6 4 8 10 9 12 11 14 16 15 13 7
高度: 5
最小值: 1
最大值: 16
4. 删除节点8
删除操作并不会造成AVL树的不平衡。
删除节点8之后,再打印该AVL树的信息。
高度: 5
中序遍历: 1 2 3 4 5 6 7 9 10 11 12 13 14 15 16
相关文章推荐
- AVL树图解和Java实现
- [置顶] 平衡树 AVL树的JAVA实现
- AVL树之java实现
- AVL树原理及实现(C语言实现以及Java语言实现)
- AVL树图解和Java实现
- AVL树的java实现
- 二叉树、二叉搜索树、AVL树的java实现
- AVL树图解和Java实现
- AVL树(三)之 Java的实现
- 数据结构--AVL树的insert()的Java实现
- AVL树-自平衡二叉查找树(Java实现)
- java实现AVL树(一种自平衡二叉树)数据结构
- AVL树的java实现
- 自学java数据结构,实现了AVL树
- java数据结构与算法之平衡二叉树(AVL树)的设计与实现
- AVL树(三)之 Java的实现【转】
- AVL树的插入与删除---Java实现
- AVL树的 java 代码实现(待更)
- AVL树Java实现
- AVL树 之 Java的实现