标签:black minimum 推荐 理解 maximum width class www. color
实现红黑树的编码,得先了解红黑树的性质,并结合性质理解红黑树的插入、删除等操作。这里推荐博客http://www.cnblogs.com/skywang12345/p/3245399.html,里面配有图文讲解,讲的非常详细具体。
以下是我自己封装实现的一个红黑树的类。
1 package com.example.lenovo.linehough; 2 3 public class GRBTree { 4 int iCount; 5 int[] treeValue; 6 GRBTreeNode root; 7 boolean BLACK = false; 8 boolean RED = true; 9 10 public GRBTree() { 11 root = null; 12 } 13 14 public GRBTree(int i) { 15 Free(); 16 } 17 18 public class GRBTreeNode { 19 int key; 20 boolean color; 21 GRBTreeNode left, right, parent; 22 23 public GRBTreeNode(int newKey, boolean newColor, GRBTreeNode newLeft, GRBTreeNode newRight) { 24 key = newKey; 25 color = newColor; 26 left = newLeft; 27 right = newRight; 28 if (left != null) 29 left.parent = this; 30 if (right != null) 31 right.parent = this; 32 parent = null; 33 } 34 35 public GRBTreeNode() { 36 if (left != null) 37 left = null; 38 if (right != null) 39 right = null; 40 } 41 } 42 43 void Free() { 44 if (root != null) 45 root = null; 46 } 47 48 void Dereference() { 49 root = null; 50 } 51 52 public GRBTreeNode Grandparent(GRBTreeNode n) { 53 if (n == null || n.parent == null) 54 return null; 55 return n.parent.parent; 56 } 57 58 public GRBTreeNode Sibling(GRBTreeNode n) { 59 if (n == null || n.parent == null) 60 return null; 61 if (n == n.parent.left) 62 return n.parent.right; 63 else 64 return n.parent.left; 65 } 66 67 public GRBTreeNode Uncle(GRBTreeNode n) { 68 if (n == null) 69 return null; 70 return Sibling(n.parent); 71 } 72 73 boolean ColorOf(GRBTreeNode n) { 74 return n == null ? BLACK : n.color; 75 } 76 77 public GRBTreeNode Find(int keyToFind) { 78 GRBTreeNode n = root; 79 while (n != null) { 80 if (keyToFind == n.key) 81 return n; 82 if (keyToFind < n.key) 83 n = n.left; 84 else 85 n = n.right; 86 } 87 return n; 88 } 89 90 void Replace(GRBTreeNode oldNode, GRBTreeNode newNode) { 91 if (oldNode.parent == null) 92 root = newNode; 93 else { 94 if (oldNode == oldNode.parent.left) 95 oldNode.parent.left = newNode; 96 else 97 oldNode.parent.right = newNode; 98 } 99 if (newNode != null) 100 newNode.parent = oldNode.parent; 101 } 102 103 void RotateLeft(GRBTreeNode n) { 104 GRBTreeNode r = n.right; 105 Replace(n, r); 106 n.right = r.left; 107 if (r.left != null) 108 r.left.parent = n; 109 r.left = n; 110 n.parent = r; 111 } 112 113 void RotateRight(GRBTreeNode n) { 114 GRBTreeNode l = n.left; 115 Replace(n, l); 116 n.left = l.right; 117 if (l.right != null) 118 l.right.parent = n; 119 l.right = n; 120 n.parent = l; 121 } 122 123 void Insert1(GRBTreeNode n) { 124 if (n.parent == null) 125 n.color = BLACK; 126 else 127 Insert2(n); 128 } 129 130 void Insert2(GRBTreeNode n) { 131 if (n.parent.color != BLACK) 132 Insert3(n); 133 } 134 135 void Insert3(GRBTreeNode n) { 136 GRBTreeNode u = Uncle(n); 137 GRBTreeNode g = Grandparent(n); 138 if (ColorOf(u) == RED) { 139 n.parent.color = BLACK; 140 u.color = BLACK; 141 g.color = RED; 142 Insert1(g); 143 } else 144 Insert4(n); 145 } 146 147 void Insert4(GRBTreeNode n) { 148 GRBTreeNode g = Grandparent(n); 149 if (n == n.parent.right && n.parent == g.left) { 150 RotateLeft(n.parent); 151 n = n.left; 152 } else if (n == n.parent.left && n.parent == g.right) { 153 RotateRight(n.parent); 154 n = n.right; 155 } 156 Insert5(n); 157 } 158 159 void Insert5(GRBTreeNode n) { 160 GRBTreeNode g = Grandparent(n); 161 n.parent.color = BLACK; 162 g.color = RED; 163 if (n == n.parent.left && n.parent == g.left) 164 RotateRight(g); 165 else 166 RotateLeft(g); 167 } 168 169 boolean Insert(int newKey) { 170 GRBTreeNode insNode = new GRBTreeNode(newKey, RED, null, null); 171 GRBTreeNode n; 172 173 if (root == null) 174 root = insNode; 175 else { 176 n = root; 177 while (true) { 178 if (newKey == n.key) { 179 return false; 180 } 181 182 if (newKey < n.key) { 183 if (n.left == null) { 184 n.left = insNode; 185 break; 186 } else 187 n = n.left; 188 } else { 189 if (n.right == null) { 190 n.right = insNode; 191 break; 192 } else 193 n = n.right; 194 } 195 } 196 insNode.parent = n; 197 } 198 199 Insert1(insNode); 200 201 return true; 202 } 203 204 public GRBTreeNode Maximum(GRBTreeNode n) { 205 if (n == null) 206 return null; 207 208 while (n.right != null) 209 n = n.right; 210 return n; 211 } 212 213 public GRBTreeNode Minimum(GRBTreeNode n) { 214 if (n == null) 215 return null; 216 217 while (n.left != null) 218 n = n.left; 219 return n; 220 } 221 222 void Delete1(GRBTreeNode n) { 223 if (n.parent != null) 224 Delete2(n); 225 } 226 227 void Delete2(GRBTreeNode n) { 228 GRBTreeNode s = Sibling(n); 229 230 if (ColorOf(s) == RED) { 231 n.parent.color = RED; 232 s.color = BLACK; 233 234 if (n == n.parent.left) { 235 RotateLeft(n.parent); 236 } else { 237 RotateRight(n.parent); 238 } 239 } 240 Delete3(n); 241 } 242 243 void Delete3(GRBTreeNode n) { 244 GRBTreeNode s = Sibling(n); 245 246 if (ColorOf(n.parent) == BLACK && ColorOf(s) == BLACK && 247 ColorOf(s.left) == BLACK && ColorOf(s.right) == BLACK) { 248 s.color = RED; 249 Delete1(n.parent); 250 } else 251 Delete4(n); 252 } 253 254 void Delete4(GRBTreeNode n) { 255 GRBTreeNode s = Sibling(n); 256 257 if (ColorOf(n.parent) == RED && ColorOf(s) == BLACK && 258 ColorOf(s.left) == BLACK && ColorOf(s.right) == BLACK) { 259 s.color = RED; 260 n.parent.color = BLACK; 261 } else 262 Delete5(n); 263 } 264 265 void Delete5(GRBTreeNode n) { 266 GRBTreeNode s = Sibling(n); 267 268 if (n == n.parent.left && 269 ColorOf(s) == BLACK && ColorOf(s.left) == RED && ColorOf(s.right) == BLACK) { 270 s.color = RED; 271 s.left.color = BLACK; 272 RotateRight(s); 273 } else if (n == n.parent.right && 274 ColorOf(s) == BLACK && ColorOf(s.right) == RED && ColorOf(s.left) == BLACK) { 275 s.color = RED; 276 s.right.color = BLACK; 277 RotateLeft(s); 278 } 279 Delete6(n); 280 } 281 282 void Delete6(GRBTreeNode n) { 283 GRBTreeNode s = Sibling(n); 284 285 s.color = ColorOf(n.parent); 286 n.parent.color = BLACK; 287 if (n == n.parent.left) { 288 s.right.color = BLACK; 289 RotateLeft(n.parent); 290 } else { 291 s.left.color = BLACK; 292 RotateRight(n.parent); 293 } 294 } 295 296 void Delete(int keyToDel) { 297 GRBTreeNode c; 298 GRBTreeNode n = Find(keyToDel); 299 if (n == null) 300 return; 301 302 if (n.left != null && n.right != null) { 303 GRBTreeNode pred = Maximum(n.left); 304 n.key = pred.key; 305 n = pred; 306 } 307 308 c = n.right == null ? n.left : n.right; 309 if (ColorOf(n) == BLACK) { 310 n.color = ColorOf(c); 311 Delete1(n); 312 } 313 Replace(n, c); 314 if (n.parent == null && c != null) 315 c.color = BLACK; 316 317 n.left = null; 318 n.right = null; 319 } 320 321 /* 前序遍历的结果放到一个数组里面*/ 322 private void preOrder(GRBTreeNode n, int width, int height) { 323 324 if (n != null) { 325 treeValue[iCount] = n.key; 326 iCount++; 327 preOrder(n.left, width, height); 328 preOrder(n.right, width, height); 329 } 330 331 } 332 333 public int[] preOrder(int width, int height) { 334 treeValue = new int[width * height]; 335 iCount = 0; 336 preOrder(root, width, height); 337 return treeValue; 338 } 339 340 public int getiCount() { 341 return iCount; 342 } 343 }
标签:black minimum 推荐 理解 maximum width class www. color
原文地址:http://www.cnblogs.com/gxclmx/p/7500970.html