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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
public class AVLTree {
AVLNode root;
static class AVLNode { int key; Object value; AVLNode left; AVLNode right; int height = 1;
public AVLNode(int key, Object value) { this.key = key; this.value = value; }
public AVLNode(int key) { this.key = key; }
public AVLNode(int key, Object value, AVLNode left, AVLNode right) { this.key = key; this.value = value; this.left = left; this.right = right; } }
private int height(AVLNode node) { return node == null ? 0 : node.height; }
private void updateHeight(AVLNode node) { node.height = Integer.max(height(node.left), height(node.right)) + 1; }
private int bf(AVLNode node) { return height(node.left) - height(node.right); }
private AVLNode rightRotate(AVLNode node) { AVLNode temp = node.left; node.left = temp.right; temp.right = node; updateHeight(node); updateHeight(temp); return temp; }
private AVLNode leftRotate(AVLNode node) { AVLNode temp = node.right; node.right = temp.left; temp.left = node; updateHeight(node); updateHeight(temp); return temp; }
private AVLNode leftRightRotate(AVLNode node) { node.left = leftRotate(node.left); return rightRotate(node); }
private AVLNode rightLeftRotate(AVLNode node) { node.right = rightRotate(node.right); return leftRotate(node); }
private AVLNode balanced(AVLNode node) { if (node == null) { return null; } int bf = bf(node); if (bf > 1 && bf(node.left) >= 0) { return rightRotate(node); } else if (bf > 1 && bf(node.left) < 0) { return leftRightRotate(node); } else if (bf < -1 && bf(node.right) > 0) { return rightLeftRotate(node); } else if (bf < -1 && bf(node.right) <= 0) { return leftRotate(node); } return node; }
private AVLNode search(AVLNode node, int key) { if (node == null) { return null; } if (key < node.key) { return search(node.left, key); } else if (key > node.key) { return search(node.right, key); } else { return node; } }
public void put(int key, Object value) { root = doPut(root, key, value); }
private AVLNode doPut(AVLNode node, int key, Object value) { if (root == null) { return new AVLNode(key, value); } if (key == node.key) { node.value = value; return node; } if (key < node.key) { node.left = doPut(node.left, key, value); } else { node.right = doPut(node.right, key, value); } updateHeight(node); return balanced(node); }
public void remove(int key) { root = doRemove(root, key); }
private AVLNode doRemove(AVLNode node, int key) { if (node == null) { return null; } if (key < node.key) { node.left = doRemove(node.left, key); } else if (key > node.key) { node.right = doRemove(node.right, key); } else { if (node.left == null && node.right == null) { return null; } else if (node.left == null) { node = node.right; } else if (node.right == null) { node = node.left; } else { AVLNode temp = node.right; while(temp.left != null){ temp = temp.left; }
temp.right = doRemove(node.right,temp.key); temp.left = node.left;
node = temp; } } updateHeight(node); return balanced(node); }
}
|