平衡二叉树 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...] -> 搜 ...
随机推荐
- Hibernate——(4)Hibernate映射类型
一.常用的Hibernat映射类型有如下几种: string integer double date 日期,只表示年月日 datetime 日期,只表示年月日 timestamp 时间戳,存放 ...
- [Spring Boot 系列] 集成maven和Spring boot的profile 专题
maven中配置profile节点: <project> .... <profiles> <profile> <!-- 生产环境 --> <id& ...
- 深入理解ArrayList与LinkedList的区别
一.先来看看ArrayList与LinkedList 在JDK中所在的位置 从图中可以看出,ArrayList与LinkedList都是List接口的实现类,因此都实现了List的所有未实现的方法,只 ...
- Qt 的几个核心机制总结之 布局(QWidget可以设置setSizePolicy,而QSizePolicy有Fixed,minimum,maximum,preferred,expanding,ignore等7个属性,还可以横竖分开)
1.Qt布局的作用 Qt的布局是通过布局管理器来实现的,布局管理器负责在父类窗口部件区域构建子窗口部件,使得放置在窗体中的每个窗口部件都有一个适合的大小和位置,并且能够随着应用程序本身的变化而变化从而 ...
- Java实现查找二叉树&C++的做法
写了个Java的查找二叉树,用递归做的,不用递归的还没弄出来.先贴一下.回头再研究. BTreeTest.java: public class BTreeTest{ class Node{ Node ...
- Linux性能测试 top衍生命令 atop/htop/slaptop
1. Atop Atop 是一个类似 top 的工具,但比 top 更有料.通过 Atop,你能够监视 Linux 系统的性能状况,包括进程活动.CPU.内存.硬盘.网络等方面的使用情况等. 2. h ...
- 跟我学ASP.NET MVC之十一:URL路由
摘要: 在MVC框架之前,ASP.NET假定在请求的URLs和服务器硬盘文件之间有直接的关系.服务器的职责是接收浏览器请求,从相应的文件发送输出. 这种方法只能工作于Web表单,每一个ASPX页面既是 ...
- Linux性能测试 命令大全
Linux 系统出现问题时,我们不仅需要查看系统日志信息,而且还要使用大量的性能监测工 具来判断究竟是哪一部分(内存.CPU.硬盘……)出了问题.在 Linux 系统中,所有的运行 参数保存在虚拟目录 ...
- WPF用Blend写的交通信号灯
原文:WPF用Blend写的交通信号灯 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yangyisen0713/article/details/1 ...
- DRP-ThreadLocal简单的理解
简单就是jar一类套餐包.在一个简单的事情是一个工具类!该工具可以做?该工具被用来写多线程程序,行.多线程是有效的.你只能去网上找资料,由于今天我们仅仅来介绍ThreadLocal的知识. 我们来 ...