平衡二叉树的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. 【35.39%】【hdu 3333】Turing Tree

    Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  2. Cross-Domain Security For Data Vault

    Cross-domain security for data vault is described. At least one database is accessible from a plural ...

  3. C# await 高级用法

    原文:C# await 高级用法 本文告诉大家 await 的高级用法,包括底层原理. 昨天看到太子写了一段代码,我开始觉得他修改了编译器,要不然下面的代码怎么可以编译通过 await "林 ...

  4. numpy 代码优化(一)—— 常见手段

    选择使用 numpy 库的应有之义就在于:应当以矢量化的方式(vectorized operations)来避免迭代操作(iterations),numpy 下的迭代操作执行起来十分耗时. impor ...

  5. Gamma 函数及其应用

    1. Γ(⋅) 函数定义 Γ(α)=∫∞0tα−1e−tdt 可知以下基本性质: Γ(α+1)=αΓ(α)(分部积分法) Γ(1)=1 ⇒ Γ(n+1)=n! Γ(12)=π√ 2. 常见变形 对于 ...

  6. 【转】NIO与传统IO的区别

    转自:http://blog.csdn.net/zhouhl_cn/article/details/6568119 传统的socket IO中,需要为每个连接创建一个线程,当并发的连接数量非常巨大时, ...

  7. 随机森林与 GBDT

    随机森林(random forest),GBDT(Gradient Boosting Decision Tree),前者中的森林,与后者中的 Boosting 都在说明,两种模型其实都是一种集成学习( ...

  8. matlab 矢量化编程(四)—— 标量函数转化为能够处理矢量的函数

    1. 组合的矢量实现 nchoosek(n, k) 的第二个参数在 matlab 下是不支持矢量化的,必须是标量形式.但 matlab 下的 gamma 函数,却可支持,矢量形式,又因为,gamma ...

  9. WPF特效-实现弧形旋转轮播图

    原文:WPF特效-实现弧形旋转轮播图 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u013224722/article/details/77004 ...

  10. 张忠谋:3nm制程会出来 2nm后很难(摩尔定律还可维持10年)

    集微网消息,台积电董事长张忠谋表示,摩尔定律可能还可再延续10年,3nm制程应该会出来,2nm则有不确定性,2nm之后就很难了. 张忠谋表示,1998年英特尔总裁贝瑞特来台时,两人曾针对摩尔定律还可延 ...