平衡二叉树的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; }