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

下面是源代码:

/*
the define of avltree's node
*/
class MyNode {
int key, height;
MyNode left, right; MyNode(int d) {
key = d;
height = 1;
}
} public class MyAvlTree {
MyNode root; /*
the function of get_tree_height
*/
int getHeight(MyNode node) {
if (node == null)
return 0;
return node.height;
} /*
the function of get the max of two numbers
*/
int max(int a, int b) {
return (a > b) ? a : b;
} /*
the function of right rotate subtree rooted y
*/
MyNode rightRoate(MyNode y) {
MyNode x = y.left;
MyNode t = x.right; /*
perform rotation
*/
x.right = y;
y.left = t; /*
update the heights
*/
y.height = max(getHeight(y.left), getHeight(y.right)) + 1;
x.height = max(getHeight(x.left), getHeight(x.right)) + 1;
// return new root
return x;
} /*
the function of left rotate subtree rooted x
*/
MyNode leftRoate(MyNode x) {
MyNode y = x.right;
MyNode t = y.left; /*
perform rotation
*/
y.left = x;
x.right = t; /*
update the heights
*/
x.height = max(getHeight(x.left), getHeight(x.right));
y.height = max(getHeight(y.left), getHeight(y.right));
//return new root
return x;
} /*
get balance factor of node n
*/
int getBalance(MyNode node) {
if (node == null)
return 0;
return getHeight(node.left) - getHeight(node.right);
} /*
the function of insert
*/
MyNode insertKey(MyNode n, int key) {
if (n == null)
return (new MyNode(key));
if (key > n.key)
n.right = insertKey(n.right, key);
else if (key < n.key)
n.left = insertKey(n.left, key);
else
return n; /*
update height
*/
n.height = 1 + max(getHeight(n.left), getHeight(n.right)); //get balance
int balance = getBalance(n); /*
there are four cases
*/
//left-left case
if (balance > 1 && key < n.left.key)
return rightRoate(n);
//right-right case
if (balance > 1 && key > n.right.key)
return leftRoate(n);
//left-right case
if (balance > 1 && key > n.left.key) {
n.left = leftRoate(n.left);
return rightRoate(n);
}
//right-left case
if (balance > 1 && key < n.right.key) {
n.right = rightRoate(n.right);
return leftRoate(n);
}
return n;
} /*
the functionn of preOrder
*/
void preOrder(MyNode node) {
if (node != null) {
System.out.print(node.key + " ");
preOrder(node.left);
preOrder(node.right);
}
} /*
the test example
*/
public static void main(String[] args) {
MyAvlTree tree = new MyAvlTree(); //the test
tree.root = tree.insertKey(tree.root, 10);
tree.root = tree.insertKey(tree.root, 20);
tree.root = tree.insertKey(tree.root, 30);
tree.root = tree.insertKey(tree.root, 40);
tree.root = tree.insertKey(tree.root, 50);
tree.root = tree.insertKey(tree.root, 25); System.out.println("Preorder traversal" +
" of constructed tree is : ");
tree.preOrder(tree.root);
}
}

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. Vue.js - Day1

    什么是Vue.js Vue.js 是目前最火的一个前端框架,React是最流行的一个前端框架(React除了开发网站,还可以开发手机App, Vue语法也是可以用于进行手机App开发的,需要借助于We ...

  2. vue2.0高仿饿了么better-scroll

    首先安装better-scroll npm i better-scroll -S goods页面模板 <template> <div class="goods"& ...

  3. dedecms无法下载远程jpeg图片 织梦不能提取文章内容中的jpeg图片生成缩略图

    文件:/dede/inc/inc_archives_functions.php 代码: preg_match_all("/(src)=[\"|'| ]{0,}([^>]*\. ...

  4. linux系统unzip文件报错的解决方案

    data.zip文件有4G多,解压的时候出问题了. Archive:  data.zip End-of-central-directorysignature not found.  Either th ...

  5. 解析Excel文件 Apache POI框架使用

    本文整理了,使用Apache POI 框架解析.读取Excel文件,过程中,程序代码出现的一些问题,并解决 1..xls 和 .xlsx 我们知道Excel文档,在Windows下,分为Excel20 ...

  6. phpStudy:使用localhost无法访问的解决方案

    今天遇到新的问题,很有意思,当使用Localhost时,发现报403错误: 百度找到问题所在:没有权限问题 所以我们打开phpStudy,找到配置文件“vhosts-conf”,看到的情况是这样的 接 ...

  7. 显示C++数据的数据类型

    #include <typeinfo> using namespace std; ... cout << typeid(d).name() << endl; 其中, ...

  8. 在ABAP里取得一个数据库表记录数的两种方法

    方法1:使用函数EM_GET_NUMBER_OF_ENTRIES 这个函数使用起来很简单,只需要将想查询的数据库表名称维护进输入参数IT_TABLES: 上图说明这个函数支持批量操作,我查询的两张表名 ...

  9. iOS开发:小技巧积累

    1.获取全局的Delegate对象,这样我们可以调用这个对象里的方法和变量: [(MyAppDelegate*)[[UIApplication sharedApplication] delegate] ...

  10. IDEA的常用操作(快捷键)

    IDEA的常用操作(快捷键) Alt+回车 导入包,自动修正 Ctrl+N 查找类 Ctrl+Shift+N 查找文件 Ctrl+Alt+L 格式化代码 Ctrl+Alt+O 优化导入的类和包 Alt ...