Java实现Avl树
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树的更多相关文章
- Java数据结构——AVL树
AVL树(平衡二叉树)定义 AVL树本质上是一颗二叉查找树,但是它又具有以下特点:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树,并且拥有自平衡机制.在AV ...
- JAVA数据结构--AVL树的实现
AVL树的定义 在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为1,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下的时间复杂度都是.增 ...
- AVL树(三)之 Java的实现
概要 前面分别介绍了AVL树"C语言版本"和"C++版本",本章介绍AVL树的Java实现版本,它的算法与C语言和C++版本一样.内容包括:1. AVL树的介绍 ...
- AVL树原理及实现(C语言实现以及Java语言实现)
欢迎探讨,如有错误敬请指正 如需转载,请注明出处http://www.cnblogs.com/nullzx/ 1. AVL定义 AVL树是一种改进版的搜索二叉树.对于一般的搜索二叉树而言,如果数据恰好 ...
- AVL树----java
AVL树----java AVL ...
- AVL树之 Java的实现
AVL树的介绍 AVL树是高度平衡的而二叉树.它的特点是:AVL树中任何节点的两个子树的高度最大差别为1. 上面的两张图片,左边的是AVL树,它的任何节点的两个子树的高度差别都<=1:而右边的不 ...
- AVL树的Java实现
AVL树:平衡的二叉搜索树,其子树也是AVL树. 以下是我实现AVL树的源码(使用了泛型): import java.util.Comparator; public class AVLTree< ...
- AVL树的JAVA实现及AVL树的旋转算法
1,AVL树又称平衡二叉树,它首先是一颗二叉查找树,但在二叉查找树中,某个结点的左右子树高度之差的绝对值可能会超过1,称之为不平衡.而在平衡二叉树中,任何结点的左右子树高度之差的绝对值会小于等于 1. ...
- 【Java】 大话数据结构(12) 查找算法(3) (平衡二叉树(AVL树))
本文根据<大话数据结构>一书及网络资料,实现了Java版的平衡二叉树(AVL树). 平衡二叉树介绍 在上篇博客中所实现的二叉排序树(二叉搜索树),其查找性能取决于二叉排序树的形状,当二叉排 ...
随机推荐
- extjs 6
因为最近公司要写一个项目前台所以开始学习extjs前端框架,希望一起共勉. 那么我们的教程就从 Hello World 讲起. helloWorld.js Ext.onReady(function ...
- linux 封禁ip
可以直接服务配置nginx.conf 添加 deny+IP 例如: 封禁单个IP deny 106.5.76.83; #封整个段即从123.0.0.1到123.255.255.254的命令deny 1 ...
- Linux 两组信号对比
博客逐步迁移到,独立博客,原文地址 http://www.woniubi.cn/two_groups_signal_difference/ 之前看信号的时候,没有太注意不同信号的对比.今天再次看到的时 ...
- Android无需权限显示悬浮窗
TYPE_TOAST一直都可以显示, 但是用TYPE_TOAST显示出来的在2.3上无法接收点击事件, 因此还是无法随意使用. 下面是我之前研究后台线程显示对话框的时候记得笔记, 大家可以看看我们项目 ...
- 易客CRM-3.0.4 (OpenLogic CentOS 6.5)
平台: CentOS 类型: 虚拟机镜像 软件包: apache1.3.8 centos6.5 mysql5.1.72 php5.2.17 commercial crm linux 服务优惠价: 按服 ...
- c++ stl sort example
c++ stl sort函数使用举例: #include <iostream> #include<vector> #include<algorithm> #incl ...
- Visual Studio 编辑器打开项目后,一直提醒Vs在忙,解决方法
今天打开VS2015后,因为这个解决中有很项目,突然就一直现在加载中,点击VS提示在忙,怎么破那?请往下看 第一种方法 1.关闭VS: 2.去C:\Users\<your users name& ...
- element-ui打包的坑爹之处 !!!必看三遍!!!
最近笔者打包element-ui出现如下问题: ERROR in static/js/0.4cad92088cb8dc6e7afd.js from UglifyJs Unexpected token: ...
- hp zbook15G2 nVidia K1100M显卡在ubuntu linux下闪屏问题
我的hp zbook15G2有一块nVidia K1100M显卡. 故障现象 安装ubuntu 16.4之后,屏幕出现闪烁现象. 重启后,进入bios,屏幕依然在闪烁. 再重启,进入另一块硬盘的win ...
- C++ new new[]详解
精髓: operator new()完成的操作一般只是分配内存:而构造函数的调用(如果需要)是在new运算符中完成的. operator new和new 运算符是不同的,operator new只分配 ...