一、基本概念
平衡二叉树也叫平衡二叉搜索树 (Self-balancingbinarysearch tree) 又被称为AVL树,可以保证查询效率较高。
具有以下特点:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1.并且左右两个子树都是一棵平衡二叉树。
二、原理图
左旋转示意
有旋转示意
需要双旋转(根节点左子树进行左旋转后,根节点进行右旋转,反之亦然)
三、代码
/** * 创建节点 */ public class Node { int value; Node left; Node right; public Node(int value) { this.value = value; } /** * 左旋转 */ private void leftRotate() { // 创建新的节点,以当前根节点的值 Node newNode = new Node(value); // 把新的节点的左子树,设置成为当前节点的左子树 newNode.left = left; // 把新的节点的右子树设置成,复制节点的右子树的左子树 newNode.right = right.left; // 把当前节点的值替换成右子节点的值 value = right.value; // 把当前节点的右子树,设置为右子树的右子树 right = right.right; // 吧当前节点的左子树设置新的节点 left = newNode; } /** * 有旋转 */ private void rightRotate() { Node newNode = new Node(value); newNode.right = right; newNode.left = left.right; value = left.value; left = left.left; right = newNode; } /** * 返回左子树的高度 * * @return */ public int leftHeight() { if (left != null) { return left.height(); } else { return 0; } } /** * 返回右子树的高度 * * @return */ public int rightHeight() { if (right == null) { return 0; } return right.height(); } /** * 返回当前节点的高度 * * @return */ public int height() { return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1; } /** * 添加节点 * * @param node */ public void add(Node node) { if (node == null) { return; } // 判断传入节点的值,和当前子树根节点的关系 if (node.value < this.value) { if (this.left != null) { //左子树递归 this.left.add(node); } else { this.left = node; } } else { if (this.right != null) { //右子树递归 this.right.add(node); } else { this.right = node; } } // 当添加完一个节点后,右子树高度-左子树高度>1 if (rightHeight() - leftHeight() > 1) { //当右子树的左子树高度大于它的右子树的高度 if(right!=null&&right.leftHeight()>right.rightHeight()){ right.rightRotate(); } leftRotate(); return; } //左子树高度>右子树高度 if (leftHeight() - rightHeight() > 1) { //当左子树的右子树高度大于它的左子树高度 if(left!=null&&left.rightHeight()>left.leftHeight()){ left.leftRotate(); } rightRotate(); } } /** * 查找要删除的节点的值 * * @param value 希望删除的值 * @return 返回该节点,默认返回null */ public Node search(int value) { if (value == this.value) { return this; } else if (value < this.value) { //查找的值小于当前节点的值 if (this.left == null) { return null; } return this.left.search(value); } else { //查找的值大于当前节点的值 if (this.right == null) { return null; } return this.right.search(value); } } /** * 查找要删除的父节点 * * @param value * @return */ public Node searchParent(int value) { // 如果当前节点就是要删除的节点的父节点,直接返回 if ((this.right != null && this.right.value == value) || (this.left != null && this.left.value == value)) { return this; } else { // 向左子树递归 if (value < this.value && this.left != null) { return this.left.searchParent(value); } else if (value >= this.value && this.right != null) { //向右子树递归 return this.right.searchParent(value); } else { return null; } } } /** * 中序遍历 */ public void infixOrder() { if (this.left != null) { this.left.infixOrder(); } System.out.println(this); if (this.right != null) { this.right.infixOrder(); } } @Override public String toString() { return "Node{" + "value=" + value + '}'; } }
public class AVLTree { private Node root; public void delNode(int value) { if (root == null) { return; } // 查找要删除的节点 Node targetNode = search(value); // 如果没有找到要删除的节点 if (targetNode == null) { return; } //二叉树只有一个节点,切删除的为此节点 if (root.left == null && root.right == null) { root = null; return; } //查找父节点 Node parentNode = searchParent(value); // 如果删除的是叶子节点 if (targetNode.left == null && targetNode.right == null) { // 如果是父节点的左子节点 if (parentNode.left != null && parentNode.left.value == value) { parentNode.left = null; } else if (parentNode.right != null && parentNode.right.value == value) { // 如果是父节点的右子节点 parentNode.right = null; } } else if (targetNode.left != null && targetNode.right != null) { // 删除节点有左右两个子树(删除当前节点右节点的最小值,或者当前节点左节点的最大值) int minVal = delRightTreeMin(targetNode.right); targetNode.value = minVal; } else { // 只有一颗子树的节点 // 如果要删除的节点有左子节点 if (targetNode.left != null) { // 如果targetNode是parent的左子节点 if (parentNode != null) { if (parentNode.left.value == value) { parentNode.left = targetNode.left; } else { parentNode.right = targetNode.left; } } else { root = targetNode.left; } } else { // 如果删除的节点有右子节点 if (parentNode != null) { if (parentNode.left.value == value) { parentNode.left = targetNode.right; } else { parentNode.right = targetNode.right; } } else { root = targetNode.right; } } } } /** * 返回以node根节点的二叉排序树的最小节点的值 * 删除最小节点,返回最小节点的值 * * @param node * @return */ public int delRightTreeMin(Node node) { Node target = node; // 循环找到左子节点,找到最小值 while (target.left != null) { target = target.left; } // 删除最小节点 delNode(target.value); return target.value; } /** * 查找要删除的节点 * * @param value * @return */ public Node search(int value) { if (root == null) { return null; } else { return root.search(value); } } /** * 查找父节点 * * @param value * @return */ public Node searchParent(int value) { if (root == null) { return null; } else { return root.searchParent(value); } } /** * 添加节点方法 * * @param node 新节点 */ public void add(Node node) { if (root == null) { root = node; } else { root.add(node); } } /** * 创建二叉排序树 */ public void infixOrder() { System.out.println("中序遍历二叉排序树"); if (root == null) { System.out.println("节点为空"); } else { root.infixOrder(); } } public Node getRoot() { return root; } }
四、测试
public class AVLTreeDemo { public static void main(String[] args) { //int[] arr = {4, 3, 6, 5, 7, 8}; //int[] arr = {10,12,8,9,7,6}; int[] arr = {10,11,7,6,8,9}; //创建一个AVLTree对象 AVLTree avlTree = new AVLTree(); // 添加节点 for (int i = 0; i < arr.length; i++) { avlTree.add(new Node(arr[i])); } // 遍历 avlTree.infixOrder(); System.out.println("树的高度;" + avlTree.getRoot().height()); System.out.println("左子树的高度;" + avlTree.getRoot().leftHeight()); System.out.println("右子树的高度;" + avlTree.getRoot().rightHeight()); } }
中序遍历二叉排序树 Node{value=6} Node{value=7} Node{value=8} Node{value=9} Node{value=10} Node{value=11} 树的高度;3 左子树的高度;2 右子树的高度;2