主要完成了平衡二叉树插入的过程 LL,RR,LR,RL四种旋转方式
注意右旋是指结点左孩子向右旋转 左旋是结点的右孩子向左旋转
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| package 二叉树;
public class AVLTree { public AVLNode root;
public static class AVLNode { public int val; public int height = 1; public AVLNode left, right;
public AVLNode(int val) { this.val = val; }
public AVLNode(int val, AVLNode left, AVLNode right) { this.val = val; this.left = left; this.right = right; } }
private static int getHeight(AVLNode avlNode) { if (avlNode == null) return 0; int l = (avlNode.left == null ? 0 : avlNode.left.height); int r = (avlNode.right == null ? 0 : avlNode.right.height); return 1 + Math.max(l, r); }
private static int getBalanceFactor(AVLNode avlNode) { return (avlNode.left == null ? 0 : avlNode.left.height) - (avlNode.right == null ? 0 : avlNode.right.height); }
private AVLNode rightRotate(AVLNode y) {
AVLNode x = y.left; AVLNode xr = x.right; x.right = y; y.left = xr; y.height = getHeight(y); x.height = getHeight(x); return x; }
private AVLNode leftRotate(AVLNode y) {
AVLNode x = y.right; AVLNode xl = x.left; x.left = y; y.right = xl; y.height = getHeight(y); x.height = getHeight(x);
return x; }
public void insert(int data) { root = insert(root, data); }
private AVLNode insert(AVLNode root, int data) { if (root == null) return new AVLNode(data); if (data < root.val) root.left = insert(root.left, data); else if (data > root.val) root.right = insert(root.right, data); else return root;
root.height = getHeight(root); int bf = getBalanceFactor(root);
if (bf == 2) { if (getBalanceFactor(root.left) == -1) { root.left = leftRotate(root.left); } root = rightRotate(root); } if (bf == -2) { if (getBalanceFactor(root.right) == 1) { root.right = rightRotate(root.right); } root = leftRotate(root); } return root; }
@Override public String toString() { return root.toString(); }
public static void main(String[] args) { AVLTree tree = new AVLTree(); int[] nums = {15, 3, 7, 10, 9, 8, 16};
for (int num : nums) { tree.insert(num); } System.out.println(tree);
} }
|
完整的AVLNode 为 多了一个show 函数可以更方便的打印树的结构 但是不要添加太多结点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| public static class AVLNode { public int val; public int height = 1; public AVLNode left, right;
public AVLNode(int val) { this.val = val; }
public AVLNode(int val, AVLNode left, AVLNode right) { this.val = val; this.left = left; this.right = right; }
public static String show(AVLNode root) { return show(root, false); }
public static String show(AVLNode root, boolean showDepth) { if (root == null) return "EMPTY!"; int treeDepth = root.height; int arrayHeight = treeDepth * 2 - 1; int arrayWidth = (2 << (treeDepth - 2)) * 3 + 1; String[][] tree = new String[arrayHeight][arrayWidth]; for (int i = 0; i < arrayHeight; i++) { for (int j = 0; j < arrayWidth; j++) { tree[i][j] = " "; } } writeArray(root, 0, arrayWidth / 2, tree, treeDepth, showDepth);
StringBuilder res = new StringBuilder(); for (String[] line : tree) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < line.length; i++) { sb.append(line[i]); if (line[i].length() > 1 && i <= line.length - 1) { i += line[i].length() > 4 ? 2 : line[i].length() - 1; } } res.append(sb).append("\n"); } return res.delete(res.length() - 1, res.length()).toString(); }
private static void writeArray(AVLNode node, int rowIndex, int columnIndex, String[][] res, int treeDepth, boolean showDepth) { if (node == null) return;
if (showDepth) res[rowIndex][columnIndex] = node.val + "(" + node.height + ")"; else res[rowIndex][columnIndex] = String.valueOf(node.val);
int currLevel = ((rowIndex + 1) / 2); if (currLevel == treeDepth) return; int gap = treeDepth - currLevel - 1;
if (node.left != null) { res[rowIndex + 1][columnIndex - gap] = "/"; writeArray(node.left, rowIndex + 2, columnIndex - gap * 2, res, treeDepth, showDepth); }
if (node.right != null) { res[rowIndex + 1][columnIndex + gap] = "\\"; writeArray(node.right, rowIndex + 2, columnIndex + gap * 2, res, treeDepth, showDepth); } }
@Override public String toString() { return show(this); } }
|
版权声明: 此文章版权归BrownCutie所有,如有转载,请註明来自原作者