平衡二叉树的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实现 亲测可用的更多相关文章

  1. JProfiler 9版本注册码(亲测可用!!!)

    JProfiler 9版本注册码(亲测可用!!!) 按默认选择“Single or evaluation license” ,Name 和 Company 随意填!!! JProfiler 9.2  ...

  2. 配置多个JDK存在的问题与解决方案 (亲测可用)

    安装多个JDK时的技巧 (亲测可用) 我的电脑本来是JDK8的,后来的想在不同的JDK版本下测试JDK的垃圾回收器. 一开始的的思路是,先安装JDK,为每个JDK配置自己的家目录,然后在想用哪个版本的 ...

  3. mybatis自动生成代码插件mybatis-generator使用流程(亲测可用)

    mybatis-generator是一款在使用mybatis框架时,自动生成model,dao和mapper的工具,很大程度上减少了业务开发人员的手动编码时间 坐着在idea上用maven构建spri ...

  4. Jrebel & Xrebel 在线激活方法 (亲测可用)

    一开始用eclipse的时候虽然这是一个狂吃内存的家伙,但是调试代码是真的舒服,修改过的代码可以不用重启热加载,后来转idea,虽然idea很完美但是也有不足的地方,比如代码调试就不能热加载. 还好有 ...

  5. 阿里云服务器centos7,docker部署mysql+Redis+vue+springboot+Nginx+fastdfs,亲测可用

    一.购买云服务器 我是今年双十一期间在阿里云购买的服务器, 简单配置2核_4G_40G_3M,三年用了不到800块,不过当时我记得腾讯云更便宜,个人感觉,阿里的云服务器更加的稳定, 毕竟身经百战, 经 ...

  6. C#读取Excel设置(亲测可用)

    OpenFileDialog openFD = new OpenFileDialog(); openFD.FileName = ""; openFD.Filter = " ...

  7. IntelliJ13+tomcat+jrebel实现热部署(亲测可用)

       网上有很多介绍intellij idea整合jrebel插件实现热部署的文章,但是有的比较复杂,有的不能成功,最后经过各种尝试,实现了整合,亲测可用!步骤说明如下:   一.先下载jrebel安 ...

  8. Linux下通过crontab及expect实现自动化处理 --亲测可用

    #!/usr/bin/expect -fspawn /home/scripts/bckup.shexpect "Enter password: "  send "WWQQ ...

  9. 亲测可用!!!golang如何在idea中保存时自动进行代码格式化

    亲测可用,golang在idea中的代码自动格式化 1.ctrl+alt+s打开设置界面,选择[Plugins] -> [Install JetBrains plugin...] -> 搜 ...

随机推荐

  1. Analysis of variance(ANOVA)

    方差分析,也称为"变异数分析",用于两个及两个以上样本均值(group means)差别的显著性检验.在 ANOVA 的环境下,一个观测得到的方差视为是由不同方差的源组合而成.

  2. linux nano 命令

    linux nano一linux像pico文本编辑软件,功能少.但是,基本能满足要求

  3. 构建自己的PHP框架(路由)

    完整项目地址:https://github.com/Evai/Aier 上一篇中我们已经建立了一个空的 Composer 项目,本篇将讲述如何构建路由. 下面我们就开始自己来构建路由,先去 GitHu ...

  4. if-then和if-then-else声明

    1.使用if-then声明 结构化命令,主要类型为if-then声明.if-then例如,下面的语句格式: if command then commands fi 假设你在使用其它编程语言的if-th ...

  5. Python第一个基本教程4章 词典: 当指数不工作时也

    Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "copyri ...

  6. xmpp和OpenFire示例,即时聊天室,支持离线消息

    让我说说为什么写这个博客,这是因为我在上周末的研究XMPP和OpenFire,从互联网上下载Demo,但跑不起来.它花了很长的时间.它被改造.抬高.篇博文也是希望后边学习XMPP和OpenFire的同 ...

  7. MSRA专访摘要

    前段时间有幸参加微软亚洲研究院之旅,顺便投简历,没想到在两次访谈迎来,并且是连续 的两次被拒绝.严重的刺激到了我.导致我疯狂的复习刷Offer.如今最终算是告于段落.如今也最终有空沉下心来总结 总结近 ...

  8. Android Camera2拍照(一)——使用SurfaceView

    原文:Android Camera2拍照(一)--使用SurfaceView Camera2 API简介 Android 从5.0(21)开始,引入了新的Camera API Camera2,原来的a ...

  9. facebook javascript api 使用

    官方api文档:http://developers.facebook.com/docs 先简单的介绍下创建一个app(https://developers.facebook.com/apps),

  10. 同时使用SpringJUnit4ClassRunner和Parameterized进行参数化

    标题实际上是个不可能完成的任务,因为我们只能用一个Runwith注解,而且只能写一个类,但是我们可以曲线救国,插入下方的5到14行就可以注入了 @ContextConfiguration(classe ...