平衡二叉树 JAVA实现 亲测可用
2017-04-18 14:06
447 查看
平衡二叉树的JAVA实现 亲测可用 包括LL LR RL RR四种情况的旋转算法 以及添加删除树结点之后对平衡二叉树的维护算法
都已经实现并测试过 没有问题。
代码地址可以直接上我的GIT clone:
https://github.com/bolddream/algorithm/tree/master/TreeHandler
以下附上class AVLTree 的实现代码:
树结点的class TreeNode 定义:
都已经实现并测试过 没有问题。
代码地址可以直接上我的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; }
相关文章推荐
- 平衡二叉树(AVL)--查找、删除、插入(Java实现)
- 平衡二叉树平衡二叉树(AVL)--查找、删除、插入(Java实现)
- java实现 二叉树的深度&判断二叉树是否是平衡二叉树
- 利用java实现发送邮件(这个亲测可用,找了好久)
- Java 实现批量重命名,亲测可用(精简版)
- java --平衡二叉树实现
- 数据结构与算法分析笔记与总结(java实现)--二叉树13:平衡二叉树
- java 平衡二叉树的实现
- 【LeetCode-面试算法经典-Java实现】【110-Balanced Binary Tree(平衡二叉树)】
- java数据结构与算法之平衡二叉树(AVL树)的设计与实现
- 平衡二叉树的java实现
- Java实现平衡二叉树(AVLTree)的构建
- Java实现平衡二叉树(AVLTree)的构建
- java实现AVL树(一种自平衡二叉树)数据结构
- 树——平衡二叉树插入和查找的JAVA实现(2):增加删除方法
- 平衡二叉树(AVL)--查找、删除、插入(Java实现)
- 平衡二叉树Java实现
- java 二维码生成与解析代码实现,程序源码下载可用
- 平衡二叉树(AVL)java实现
- 平衡二叉树的Java实现——插入操作【无泛型】