平衡二叉树 JAVA实现 亲测可用
平衡二叉树的JAVA实现 亲测可用 包括LL LR RL RR四种情况的旋转算法 以及添加删除树结点之后对平衡二叉树的维护算法
都已经实现并测试过 没有问题。
代码地址可以直接上我的GIT clone:
https://github.com/bolddream/algorithm/tree/master/TreeHandler
以下附上class AVLTree 的实现代码:
public class AVLTree<T extends Comparable<T>>
{ public TreeNode<T> rootNode; public int getMaxHeight(TreeNode<T> root)
{
if(root == null)
{
return 0;
} int height = 1;
int leftSonHeight = getMaxHeight(root.lson);
int rightSonHeight = getMaxHeight(root.rson);
if(leftSonHeight > rightSonHeight)
{
height += leftSonHeight;
}
else
{
height += rightSonHeight;
} return height;
} public TreeNode<T> singleRotateLeft(TreeNode<T> k2)
{
TreeNode<T> k1 = k2.lson;
if(k1 == null)
{
return null;
} TreeNode<T> temp = k1.rson;
k1.rson = k2;
k2.lson = temp; k2.height = getMaxHeight(k2);
k1.height = getMaxHeight(k1);
return k1;
} public TreeNode<T> singleRotateRight(TreeNode<T> k2)
{
TreeNode<T> k1 = k2.rson;
if(k1 == null)
{
return null;
} TreeNode<T> temp = k1.lson;
k1.lson = k2;
k2.rson = temp; k2.height = getMaxHeight(k2);
k1.height = getMaxHeight(k1);
return k1;
} public TreeNode<T> doubleRotateLeftRight(TreeNode<T> k3)
{
k3.lson = singleRotateRight(k3.lson);
return singleRotateLeft(k3);
} public TreeNode<T> doubleRotateRightLeft(TreeNode<T> k3)
{
k3.rson = singleRotateLeft(k3.rson);
return singleRotateRight(k3);
} public TreeNode<T> insertTreeNode(TreeNode<T> insertNode)
{
return insertTreeNode(insertNode, this.rootNode);
} public TreeNode<T> insertTreeNode(TreeNode<T> insertNode, TreeNode<T> currentNode)
{
if(insertNode == null)
{
return currentNode;
} TreeNode<T> rootNode = currentNode;
if(currentNode == null)
{
currentNode = insertNode;
currentNode.height = 1;
currentNode.freq = 1;
rootNode = currentNode;
return rootNode;
} if(insertNode.data.compareTo(currentNode.data) > 0)
{
currentNode.rson = insertTreeNode(insertNode, currentNode.rson);
currentNode.height = getMaxHeight(currentNode);
rootNode = ajustAVLTree(currentNode);
}
else if(insertNode.data.compareTo(currentNode.data) < 0)
{
currentNode.lson = insertTreeNode(insertNode, currentNode.lson);
currentNode.height = getMaxHeight(currentNode);
rootNode = ajustAVLTree(currentNode);
}
else
{
currentNode.freq ++;
rootNode = currentNode;
} return rootNode; } public TreeNode<T> ajustAVLTree(TreeNode<T> currentNode)
{
TreeNode<T> rootNode = currentNode; int leftSonHeight = (currentNode.lson != null) ? currentNode.lson.height : 0;
int rightSonHeight = (currentNode.rson != null) ? currentNode.rson.height : 0;
if(2 == rightSonHeight - leftSonHeight)
{
int rightLeftSonHeight = (currentNode.rson.lson != null) ? currentNode.rson.lson.height : 0;
int rightRightSonHeight = (currentNode.rson.rson != null) ? currentNode.rson.rson.height : 0; if(rightLeftSonHeight > rightRightSonHeight)
{
//RL
rootNode = doubleRotateRightLeft(currentNode);
}
else
{
//RR
rootNode = singleRotateRight(currentNode);
}
}
else if(2 == leftSonHeight - rightSonHeight)
{
int leftLeftSonHeight = (currentNode.lson.lson != null) ? currentNode.lson.lson.height : 0;
int leftRightSonHeight = (currentNode.lson.rson != null) ? currentNode.lson.rson.height : 0; if(leftLeftSonHeight > leftRightSonHeight)
{
//LL
rootNode = singleRotateLeft(currentNode);
}
else
{
//LR
rootNode = doubleRotateLeftRight(currentNode);
}
} return rootNode;
} public TreeNode<T> deleteTreeNode(TreeNode<T> deleteNode, TreeNode<T> currentNode)
{
if(deleteNode == null || currentNode == null)
{
return currentNode;
} TreeNode<T> rootNode; if(deleteNode.data.compareTo(currentNode.data) > 0)
{
currentNode.rson = deleteTreeNode(deleteNode, currentNode.rson);
currentNode.height = getMaxHeight(currentNode); rootNode = ajustAVLTree(currentNode);
}
else if(deleteNode.data.compareTo(currentNode.data) < 0)
{
currentNode.lson = deleteTreeNode(deleteNode, currentNode.lson);
currentNode.height = getMaxHeight(currentNode); rootNode = ajustAVLTree(currentNode);
}
else
{
if(currentNode.freq >=2)
{
currentNode.freq--;
}
else
{
TreeNode<T> temp = currentNode;
if(currentNode.lson != null && currentNode.rson != null)
{
//get the min value node of right son tree, then replace the currentNode;
TreeNode<T> minValueRightSon;
temp = currentNode.rson;
while(temp.lson != null)
{
minValueRightSon = temp;
temp = temp.lson;
} currentNode.data = temp.data;
currentNode.freq = temp.freq;
currentNode.rson = deleteTreeNode(temp, currentNode.rson);
}
else if(currentNode.lson == null)
{
currentNode = currentNode.rson;
}
else
{
currentNode = currentNode.lson;
} if(currentNode != null)
{
currentNode.height = getMaxHeight(currentNode);
currentNode = ajustAVLTree(currentNode);
} }
rootNode = currentNode;
} return rootNode;
} public TreeNode<T> middleSearchTreeNode(T searchData, TreeNode<T> currentNode)
{
if(currentNode == null)
{
return null;
} TreeNode<T> result = null;
if(searchData.equals(currentNode.data))
{
return currentNode;
}
else
{
result = middleSearchTreeNode(searchData, currentNode.lson);
if(result == null)
{
result = middleSearchTreeNode(searchData, currentNode.rson);
}
} return result;
} public void middleTraverseTree(TreeNode<T> rootNode)
{
if(rootNode == null)
{
return ;
} if(rootNode.data != null)
{
System.out.print(rootNode.data.toString() + " ");
} middleTraverseTree(rootNode.lson);
middleTraverseTree(rootNode.rson);
}
}
树结点的class TreeNode 定义:
public class TreeNode<T> {
public TreeNode(T data) {
this.data = data;
}
public T data;
public int freq;
public int height;
public TreeNode<T> lson;
public TreeNode<T> rson;
}
平衡二叉树 JAVA实现 亲测可用的更多相关文章
- JProfiler 9版本注册码(亲测可用!!!)
JProfiler 9版本注册码(亲测可用!!!) 按默认选择“Single or evaluation license” ,Name 和 Company 随意填!!! JProfiler 9.2 ...
- 配置多个JDK存在的问题与解决方案 (亲测可用)
安装多个JDK时的技巧 (亲测可用) 我的电脑本来是JDK8的,后来的想在不同的JDK版本下测试JDK的垃圾回收器. 一开始的的思路是,先安装JDK,为每个JDK配置自己的家目录,然后在想用哪个版本的 ...
- mybatis自动生成代码插件mybatis-generator使用流程(亲测可用)
mybatis-generator是一款在使用mybatis框架时,自动生成model,dao和mapper的工具,很大程度上减少了业务开发人员的手动编码时间 坐着在idea上用maven构建spri ...
- Jrebel & Xrebel 在线激活方法 (亲测可用)
一开始用eclipse的时候虽然这是一个狂吃内存的家伙,但是调试代码是真的舒服,修改过的代码可以不用重启热加载,后来转idea,虽然idea很完美但是也有不足的地方,比如代码调试就不能热加载. 还好有 ...
- 阿里云服务器centos7,docker部署mysql+Redis+vue+springboot+Nginx+fastdfs,亲测可用
一.购买云服务器 我是今年双十一期间在阿里云购买的服务器, 简单配置2核_4G_40G_3M,三年用了不到800块,不过当时我记得腾讯云更便宜,个人感觉,阿里的云服务器更加的稳定, 毕竟身经百战, 经 ...
- C#读取Excel设置(亲测可用)
OpenFileDialog openFD = new OpenFileDialog(); openFD.FileName = ""; openFD.Filter = " ...
- IntelliJ13+tomcat+jrebel实现热部署(亲测可用)
网上有很多介绍intellij idea整合jrebel插件实现热部署的文章,但是有的比较复杂,有的不能成功,最后经过各种尝试,实现了整合,亲测可用!步骤说明如下: 一.先下载jrebel安 ...
- Linux下通过crontab及expect实现自动化处理 --亲测可用
#!/usr/bin/expect -fspawn /home/scripts/bckup.shexpect "Enter password: " send "WWQQ ...
- 亲测可用!!!golang如何在idea中保存时自动进行代码格式化
亲测可用,golang在idea中的代码自动格式化 1.ctrl+alt+s打开设置界面,选择[Plugins] -> [Install JetBrains plugin...] -> 搜 ...
随机推荐
- Android中自定义View和自定义动画
Android FrameWork 层给我们提供了很多界面组件,但是在实际的商业开发中这些组件往往并不能完全满足我们的需求,这时候我们就需要自定义我们自己的视图和动画. 我们要重写系统的View就必须 ...
- Apache POI Word基本使用
Apache POI Word 1.什么是Apache POI? Apache POI是一个流行的API,使用Java程序创建,修改和显示MS-Office文件. 它是由Apache Software ...
- iOS 注册或登录页面(UILable,UITextField,UIButton)
注册或登录页面 例如下面的附图 1,为了在这里展示UITextField文本框关联的键盘设置.在这里,"password"和"判定password"关联键盘被设 ...
- Python Tricks(二十一)—— 排列组合的计算
使用循环: 阶乘的实现: def fac(n): r = 1. for i in range(1, n+1): r *= i return r 排列:Anm=m!n!=(m−n+1)⋯m def pe ...
- 电讯“情趣me”为什么命途多舛?
古人有句话叫做战争"鼓作气,再而衰,三而竭",意思是打仗必须"一气呵成".才干发挥最大实力,取得最好的战绩.所谓商场如战场,经商也是如此,近期,中国电信 ...
- C# WPF 左侧菜单右侧内容布局效果实现
原文:C# WPF 左侧菜单右侧内容布局效果实现 我们要做的效果是这样的,左侧是可折叠的菜单栏,右侧是内容区域,点击左侧的菜单项右侧内容区域则相应地切换. wpf实现的话,我的办法是用一个tabcon ...
- WPF中使用TranslateTransform3D修改CAD的3D旋转中心
原文:WPF中使用TranslateTransform3D修改CAD的3D旋转中心 前面一篇文章讲述了2D旋转功能的实现,文章提到了修改3D旋转中心,这一节主要总结一下具体的修改3D旋转 ...
- 百度地图 Android SDK - 新的版本号(v3.2.0)正式上线
百度地图 Android SDK v3.2.0 在版本号 2014 年 11 月 07 日本正式推出工作完成! watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...
- 在Linux下使用MinGW静态交叉编译带有zlib的libcurl(包括交叉编译openssl,即--cross-compile-prefix=i686-w64-mingw32- mingw)
在Linux下使用MinGW静态交叉编译带有zlib的libcurl libcurl是一个跨平台的.易用的.强大的网络库.在大部分Linux发行版中都有编译好的二进制包可供使用,Mac系统更是将其作为 ...
- 读BeautifulSoup官方文档之html树的搜索(1)
之前介绍了有关的四个对象以及他们的属性, 但是一般情况下要在杂乱的html中提取我们所需的tag(tag中包含的信息)是比较复杂的, 现在我们可以来看看到底有些什么搜索的方法. 最主要的两个方法当然是 ...