平衡二叉树 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...] -> 搜 ...
随机推荐
- dp_Pku1887
<span style="color:#000099;">/* A - 单纯dp 示例 Time Limit:1000MS Memory Limit:30000KB 6 ...
- Formview单文档或对话框项目接受不到按键消息的解决办法
当对话框或formview界面上有控件时,由于焦点在控件上,因此wm_char,wm_keydown等按键消息会被控件捕获,而导致对话框或formview无法接受该类按键消息.这时候通常的解决方法是在 ...
- 使用JScript脚本批量修改VC工程设置
作者:朱金灿 来源:http://blog.csdn.net/clever101 很多时候升级了第三方库,需要对很多工程修改设置.在VS中按住Ctrl键确实可以多选工程,但通过这样做也有麻烦的地方:一 ...
- backbone Model
requirejs.config({ baseUrl: 'js/lib', paths:{ app: '../app' } }) // Start the main app logic. //requ ...
- java多线程模拟生产者消费者问题,公司面试常常问的题。。。
package com.cn.test3; //java多线程模拟生产者消费者问题 //ProducerConsumer是主类,Producer生产者,Consumer消费者,Product产品 // ...
- hdu - 4971 - A simple brute force problem.(最大权闭合图)
题意:n(n <= 20)个项目,m(m <= 50)个技术问题,做完一个项目能够有收益profit (<= 1000),做完一个项目必须解决对应的技术问题,解决一个技术问题须要付出 ...
- Opencv中SVM样本训练、归类流程及实现
支持向量机(SVM)中最核心的是什么?个人理解就是前4个字--"支持向量",一旦在两类或多累样本集中定位到某些特定的点作为支持向量,就可以依据这些支持向量计算出来分类超平面,再依据 ...
- ubuntu 12.04 简单配置samba服务,实现主机与虚拟机互通(设置Windows虚拟盘)
环境: virtualbox ubuntu12.04 首先,如果你到这步了,说明你的window与linux的网络已经配好了,他们之间是可以互相Ping通的,如果没有,请看我以前的文章 由于我linu ...
- WPF 拖动多个文件到窗体 添加文件信息
将Window的AllowDrop属性设置为true window添加Drop事件 private void Window_Drop(object sender, DragEventArgs e) { ...
- WPF中获取TreeView以及ListView获取其本身滚动条的方法,可实现自行调节scoll滚动的位置(可相应获取任何控件中的内部滚动条)
原文:WPF中获取TreeView以及ListView获取其本身滚动条的方法,可实现自行调节scoll滚动的位置(可相应获取任何控件中的内部滚动条) 对于TreeView而言: TreeViewAut ...