import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
public class RedBlackTree<K extends Comparable<K>, V> {
private static final boolean RED = true;
private static final boolean BLACK = false;
private Node root;
private class Node {
private K key;
private V value;
private Node left, right;
private boolean color;
private int N;
public Node(K key, V value, boolean color, int N) {
this.key = key;
this.value = value;
this.color = color;
this.N = N;
}
}
public RedBlackTree() {
}
/**
* 空节点默认都是黑色节点
*
* @param x
* @return
*/
private boolean isRed(Node x) {
if (x == null)
return false;
return x.color == RED;
}
/**
* 返回以X为根的子树的节点数目
*
* @param x
* @return
*/
private int size(Node x) {
if (x == null)
return 0;
return x.N;
}
/**
* 返回整棵树的节点数目
*
* @return
*/
public int size() {
return size(root);
}
public boolean isEmpty() {
return root == null;
}
/**
* 根据key进行查找
*
* @param key
* @return
*/
public V get(K key) {
if (key == null) {
throw new NullPointerException("key不能为空");
}
return get(root, key);
}
private V get(Node x, K key) {
while (x != null) {
int cmp = key.compareTo(x.key);
if (cmp < 0) {
x = x.left;
} else if (cmp > 0) {
x = x.right;
} else {
return x.value;
}
}
return null;
}
/**
* 判断树中是否包含名字为key的键
*
* @param key
* @return
*/
public boolean contains(K key) {
return get(key) != null;
}
/**
* 右旋转, 将向左倾斜的红链接变为向右倾斜
*
* @param h
* @return
*/
private Node rotateRight(Node h) {
Node x = h.left;
h.left = x.right;
x.right = h;
x.color = h.color;
h.color = RED;
x.N = h.N;
h.N = size(h.left) + size(h.right) + 1;
return x;
}
/**
* 左旋转,与右旋转相反
*
* @param h
* @return
*/
private Node rotateLeft(Node h) {
Node x = h.right;
h.right = x.left;
x.left = h;
x.color = h.color;
h.color = RED;
x.N = h.N;
h.N = size(h.left) + size(h.right) + 1;
return x;
}
/**
* 颜色反转,注意:h必须与其两个子节点颜色相反
*
* @param h
*/
private void flipColors(Node h) {
h.color = !h.color;
h.left.color = !h.left.color;
h.right.color = !h.right.color;
}
/**
* 如果h节点是红色节点, 并且 h.right和h.right.left是黑色。将h.right或者其某个子节点变为红色
*
* @param h
* @return
*/
private Node moveRedRight(Node h) {
flipColors(h);
if (isRed(h.left.left)) {
h = rotateRight(h);
flipColors(h);
}
return h;
}
/**
* 如果h节点是红色节点,并且 h.left和h.left.left是黑色。将 h.left或者其某个子节点变为红色
*
* @param h
* @return
*/
private Node moveRedLeft(Node h) {
flipColors(h);
if (isRed(h.right.left)) {
h.right = rotateRight(h.right);
h = rotateLeft(h);
flipColors(h);
}
return h;
}
private Node balance(Node h) {
if (isRed(h.right)) {
h = rotateLeft(h);
}
if (isRed(h.left) && isRed(h.left.left)) {
h = rotateRight(h);
}
if (isRed(h.left) && isRed(h.right)) {
flipColors(h);
}
h.N = size(h.left) + size(h.right) + 1;
return h;
}
/**
* 插入键值对
*
* @param key
* @param value
*/
public void put(K key, V value) {
if (key == null) {
throw new NullPointerException("key不能为空");
}
if (value == null) {
delete(key);
return;
}
root = put(root, key, value);
root.color = BLACK;
assert check();
}
/**
* 在以h为根节点的子树中插入键值对
*
* @param h
* @param key
* @param value
* @return
*/
private Node put(Node h, K key, V value) {
if (h == null) {
return new Node(key, value, RED, 1);
}
int cmp = key.compareTo(h.key);
if (cmp < 0) {
h.left = put(h.left, key, value);
} else if (cmp > 0) {
h.right = put(h.right, key, value);
} else {
h.value = value;
}
if (isRed(h.right) && !isRed(h.left)) {
h = rotateLeft(h);
}
if (isRed(h.left) && isRed(h.left.left)) {
h = rotateRight(h);
}
if (isRed(h.left) && isRed(h.right)) {
flipColors(h);
}
h.N = size(h.left) + size(h.right) + 1;
return h;
}
public void deleteMin() {
if (isEmpty()) {
throw new NoSuchElementException("该红黑树为空, 删毛线!!!");
}
if (!isRed(root.left) && !isRed(root.right)) {
root.color = RED;
}
root = deleteMin(root);
if (!isEmpty()) {
root.color = BLACK;
}
assert check();
}
private Node deleteMin(Node h) {
if (h.left == null) {
return null;
}
if (!isRed(h.left) && !isRed(h.left.left)) {
h = moveRedLeft(h);
}
h.left = deleteMin(h.left);
return balance(h);
}
public void deleteMax() {
if (isEmpty()) {
throw new NoSuchElementException("该红黑树为空, 删毛线!!!");
}
if (!isRed(root.left) && !isRed(root.right)) {
root.color = RED;
}
root = deleteMax(root);
if (!isEmpty()) {
root.color = BLACK;
}
assert check();
}
private Node deleteMax(Node h) {
if (isRed(h.left)) {
h = rotateRight(h);
}
if (h.right == null) {
return null;
}
if (!isRed(h.right) && !isRed(h.right.left)) {
h = moveRedRight(h);
}
h.right = deleteMax(h.right);
return balance(h);
}
public void delete(K key) {
if (key == null) {
throw new NullPointerException("key不能为空");
}
if (!contains(key)) {
return;
}
if (!isRed(root.left) && !isRed(root.right)) {
root.color = RED;
}
root = delete(root, key);
if (!isEmpty()) {
root.color = BLACK;
}
assert check();
}
private Node delete(Node h, K key) {
if (key.compareTo(h.key) < 0) {
if (!isRed(h.left) && !isRed(h.left.left)) {
h = moveRedLeft(h);
}
h.left = delete(h.left, key);
} else {
if (isRed(h.left)) {
h = rotateRight(h);
}
if (key.compareTo(h.key) == 0 && (h.right == null)) {
return null;
}
if (!isRed(h.right) && !isRed(h.right.left)) {
h = moveRedRight(h);
}
if (key.compareTo(h.key) == 0) {
Node x = min(h.right);
h.key = x.key;
h.value = x.value;
h.right = deleteMin(h.right);
} else {
h.right = delete(h.right, key);
}
}
return balance(h);
}
/**
* 求红黑树的高度
*
* @return
*/
public int height() {
return height(root);
}
private int height(Node x) {
if (x == null) {
return -1;
}
return 1 + Math.max(height(x.left), height(x.right));
}
/**
* 返回最小键
*
* @return
*/
public K min() {
if (isEmpty()) {
throw new NoSuchElementException("该红黑树为空,没有任何节点");
}
return min(root).key;
}
private Node min(Node x) {
if (x.left == null) {
return x;
} else {
return min(x.left);
}
}
public K max() {
if (isEmpty()) {
throw new NoSuchElementException("该红黑树为空,没有任何节点");
}
return max(root).key;
}
/**
* 返回以x为根节点的子树中的最大的key
*
* @param x
* @return
*/
private Node max(Node x) {
if (x.right == null) {
return x;
} else {
return max(x.right);
}
}
public K floor(K key) {
if (key == null) {
throw new NullPointerException("key不能为空");
}
if (isEmpty()) {
throw new NoSuchElementException("该红黑树为空,没有任何节点");
}
Node x = floor(root, key);
if (x == null) {
return null;
} else {
return x.key;
}
}
/**
* 返回存在于以x为根节点的子树中的, 键的值小于或等于key的最大键
*
* @param x
* @param key
* @return
*/
private Node floor(Node x, K key) {
if (x == null) {
return null;
}
int cmp = key.compareTo(x.key);
if (cmp == 0) {
return x;
}
if (cmp < 0) {
return floor(x.left, key);
}
Node t = floor(x.right, key);
if (t != null) {
return t;
} else {
return x;
}
}
public K ceiling(K key) {
if (key == null) {
throw new NullPointerException("key不能为空");
}
if (isEmpty()) {
throw new NoSuchElementException("该红黑树为空,没有任何节点");
}
Node x = ceiling(root, key);
if (x == null) {
return null;
} else {
return x.key;
}
}
/**
* 返回以x为根节点的子树中, 大于或等于key的最小键
*
* @param x
* @param key
* @return
*/
private Node ceiling(Node x, K key) {
if (x == null) {
return null;
}
int cmp = key.compareTo(x.key);
if (cmp == 0) {
return x;
}
if (cmp > 0) {
return ceiling(x.right, key);
}
Node t = ceiling(x.left, key);
if (t != null) {
return t;
} else {
return x;
}
}
public K select(int k) {
if (k < 0 || k >= size()) {
throw new IllegalArgumentException();
}
Node x = select(root, k);
return x.key;
}
/**
* 返回以x为根节点的子树中, 大小排名(从小到大的排序,也即升序排序)为k的节点
*
* @param x
* @param k
* @return
*/
private Node select(Node x, int k) {
int t = size(x.left);
if (t > k) {
return select(x.left, k);
} else if (t < k) {
return select(x.right, k - t - 1);
} else {
return x;
}
}
public int rank(K key) {
if (key == null) {
throw new NullPointerException("key不能为空");
}
return rank(key, root);
}
private int rank(K key, Node x) {
if (x == null) {
return 0;
}
int cmp = key.compareTo(x.key);
if (cmp < 0) {
return rank(key, x.left);
} else if (cmp > 0) {
return 1 + size(x.left) + rank(key, x.right);
} else {
return size(x.left);
}
}
/**
* 迭代整棵红黑树中的所有键
*
* @return
*/
public Iterable<K> keys() {
if (isEmpty()) {
return new LinkedList<K>();
}
return keys(min(), max());
}
/**
* 迭代low~high范围内的键
*
* @param low
* @param high
* @return
*/
public Iterable<K> keys(K low, K high) {
if (low == null)
throw new NullPointerException("low不能为空");
if (high == null)
throw new NullPointerException("high不能为空");
Queue<K> queue = new LinkedList<K>();
keys(root, queue, low, high);
return queue;
}
/**
* 将以x为根节点的子树中的, 范围在low~high之间的所有键添加到队列中。
* 过程是:首先判断节点x对应的key在不在low~high范围内,在则添加到队列中
* ,不在则不添加。然后递归调用,判断其左节点和右节点在不在low~high指定的范围内。
*
* @param x
* @param queue
* @param low
* @param high
*/
private void keys(Node x, Queue<K> queue, K low, K high) {
if (x == null) {
return;
}
int cmplo = low.compareTo(x.key);
int cmphi = high.compareTo(x.key);
if (cmplo < 0) {
keys(x.left, queue, low, high);
}
if (cmplo <= 0 && cmphi >= 0) {
queue.add(x.key);
}
if (cmphi > 0) {
keys(x.right, queue, low, high);
}
}
private boolean check() {
if (!isBST()) {
System.out.println("不满足二叉树的定义");
}
if (!isSizeConsistent()) {
System.out.println("节点数目不一致");
}
if (!isRankConsistent()) {
System.out.println("排名不一致");
}
if (!is23()) {
System.out.println("不满足2-3树的定义");
}
if (!isBalanced()) {
System.out.println("不满足红黑树的完美黑色平衡");
}
return isBST() && isSizeConsistent() && isRankConsistent() && is23() && isBalanced();
}
/**
* 判断红黑树是否满足二叉树(二叉搜索树)的定义(左节点比当前节点小, 右节点比当前节点大)
*
* @return
*/
private boolean isBST() {
return isBST(root, null, null);
}
private boolean isBST(Node x, K min, K max) {
if (x == null) {
return true;
}
if (min != null && x.key.compareTo(min) <= 0) {
return false;
}
if (max != null && x.key.compareTo(max) >= 0) {
return false;
}
return isBST(x.left, min, x.key) && isBST(x.right, x.key, max);
}
/**
* 递归判断每一个节点的大小是不是等于: size(x.left) + 1 + size(x.right)
*
* @return
*/
private boolean isSizeConsistent() {
return isSizeConsistent(root);
}
private boolean isSizeConsistent(Node x) {
if (x == null) {
return true;
}
if (x.N != size(x.left) + size(x.right) + 1) {
return false;
}
return isSizeConsistent(x.left) && isSizeConsistent(x.right);
}
/**
* 通过select与rank方法验证二叉树(此处的二叉树就是指红黑树)
*
* @return
*/
private boolean isRankConsistent() {
for (int i = 0; i < size(); i++)
if (i != rank(select(i))) {
return false;
}
for (K key : keys()) {
if (key.compareTo(select(rank(key))) != 0) {
return false;
}
}
return true;
}
/**
* 判断是否满足二三树(或者红黑树)的定义。注意:红黑树是二三树的一种具体实现。二三树的定义大家自己查询咯
*
* @return
*/
private boolean is23() {
return is23(root);
}
private boolean is23(Node x) {
if (x == null) {
return true;
}
if (isRed(x.right)) {
return false;
}
if (x != root && isRed(x) && isRed(x.left)) {
return false;
}
return is23(x.left) && is23(x.right);
}
/**
* 判断从根节点到叶节点的任意一条路径上的黒链接的数量是否相等(也即判断23树是否满足完美黑色平衡)
*
* @return
*/
private boolean isBalanced() {
int black = 0;
Node x = root;
while (x != null) {
if (!isRed(x)) {
black++;
}
x = x.left;
}
return isBalanced(root, black);
}
/**
* @param x
* @param black
* @return
*/
private boolean isBalanced(Node x, int black) {
if (x == null) {
return black == 0;
}
if (!isRed(x)) {
black--;
}
return isBalanced(x.left, black) && isBalanced(x.right, black);
}
public void recursiveInorder() {
recursiveInorder(root);
}
/** 递归实现中序遍历 */
private void recursiveInorder(Node node) {
if (node != null) {
recursiveInorder(node.left);
visit(node);
recursiveInorder(node.right);
}
}
public void visit(Node node) {
StringBuffer buff = new StringBuffer();
if (node != null) {
buff.append(" " + node.key + "[");
String left = node.left != null ? node.left.key + "" : "null";
String right = node.right != null ? node.right.key + "" : "null";
String color = node.color == RED ? "红" : "黑";
buff.append(left).append(" : ").append(right).append(" - ").append(color).append("] ");
}
System.out.print(buff.toString());
}
public static void main(String... args) {
RedBlackTree<Integer, String> st = new RedBlackTree<Integer, String>();
st.put(1, "yi");
st.put(2, "er");
st.put(3, "san");
st.put(4, "si");
st.put(5, "wu");
st.put(6, "liu");
st.put(7, "qi");
st.put(8, "ba");
st.put(9, "jiu");
st.put(10, "shi");
st.put(11, "shiyi");
st.put(21, "eryi");
st.put(31, "sanyi");
System.out.println("插入值后的大小是" + st.size());
for (Integer s : st.keys()) {
System.out.print(st.get(s) + " ");
}
System.out.println();
st.recursiveInorder();
System.out.println();
st.delete(8);
st.delete(10);
st.delete(5);
st.delete(3);
st.delete(4);
System.out.println("删除后的大小是:" + st.size());
System.out.println();
st.recursiveInorder();
}
}
- 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
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
- 471
- 472
- 473
- 474
- 475
- 476
- 477
- 478
- 479
- 480
- 481
- 482
- 483
- 484
- 485
- 486
- 487
- 488
- 489
- 490
- 491
- 492
- 493
- 494
- 495
- 496
- 497
- 498
- 499
- 500
- 501
- 502
- 503
- 504
- 505
- 506
- 507
- 508
- 509
- 510
- 511
- 512
- 513
- 514
- 515
- 516
- 517
- 518
- 519
- 520
- 521
- 522
- 523
- 524
- 525
- 526
- 527
- 528
- 529
- 530
- 531
- 532
- 533
- 534
- 535
- 536
- 537
- 538
- 539
- 540
- 541
- 542
- 543
- 544
- 545
- 546
- 547
- 548
- 549
- 550
- 551
- 552
- 553
- 554
- 555
- 556
- 557
- 558
- 559
- 560
- 561
- 562
- 563
- 564
- 565
- 566
- 567
- 568
- 569
- 570
- 571
- 572
- 573
- 574
- 575
- 576
- 577
- 578
- 579
- 580
- 581
- 582
- 583
- 584
- 585
- 586
- 587
- 588
- 589
- 590
- 591
- 592
- 593
- 594
- 595
- 596
- 597
- 598
- 599
- 600
- 601
- 602
- 603
- 604
- 605
- 606
- 607
- 608
- 609
- 610
- 611
- 612
- 613
- 614
- 615
- 616
- 617
- 618
- 619
- 620
- 621
- 622
- 623
- 624
- 625
- 626
- 627
- 628
- 629
- 630
- 631
- 632
- 633
- 634
- 635
- 636
- 637
- 638
- 639
- 640
- 641
- 642
- 643
- 644
- 645
- 646
- 647
- 648
- 649
- 650
- 651
- 652
- 653
- 654
- 655
- 656
- 657
- 658
- 659
- 660
- 661
- 662
- 663
- 664
- 665
- 666
- 667
- 668
- 669
- 670
- 671
- 672
- 673
- 674
- 675
- 676
- 677
- 678
- 679
- 680
- 681
- 682
- 683
- 684
- 685
- 686
- 687
- 688
- 689
- 690
- 691
- 692
- 693
- 694
- 695
- 696
- 697
- 698
- 699
- 700
- 701
- 702
- 703
- 704
- 705
- 706
- 707
- 708
- 709
- 710
- 711
- 712
- 713
- 714
- 715
- 716
- 717
- 718
- 719
- 720
- 721
- 722
- 723
- 724
- 725
- 726
- 727
- 728
- 729
- 730
- 731
- 732
- 733
- 734
- 735
- 736
- 737
- 738
- 739
- 740
- 741
- 742
- 743
- 744
- 745
- 746
- 747
- 748
- 749
- 750
- 751
- 752
- 753
- 754
- 755
- 756
- 757
- 758
- 759
- 760
- 761
- 762
- 763
- 764
- 765
- 766
- 767
- 768
- 769
- 770
- 771
- 772
- 773
- 774
- 775
- 776
- 777
- 778
- 779
- 780
- 781
- 782
- 783
- 784
- 785
- 786
- 787
- 788
- 789
- 790
- 791
- 792
- 793
- 794
- 795
- 796
- 797
- 798
- 799
- 800
- 801
- 802
- 803
- 804
- 805
- 806
- 807
- 808
- 809
- 810
- 811
- 812
- 813
- 814
- 815
- 816
- 817
- 818
- 819
- 820
- 821
- 822
- 823
- 824
- 825
- 826
- 827
- 828
- 829
- 830
- 831
- 832
- 833
- 834
- 835
- 836
- 837
- 838
- 839
- 840
- 841
- 842
- 843
- 844
- 845
- 846
- 847
- 848
- 849
- 850
- 851
- 852
- 853
- 854
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-8cccb36679.css" rel="stylesheet">
</div>