github.com/zhongdalu/gf@v1.0.0/g/container/gtree/gtree_redblacktree.go (about) 1 // Copyright 2019 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 package gtree 8 9 import ( 10 "encoding/json" 11 "fmt" 12 13 "github.com/zhongdalu/gf/g/container/gvar" 14 "github.com/zhongdalu/gf/g/internal/rwmutex" 15 ) 16 17 type color bool 18 19 const ( 20 black, red color = true, false 21 ) 22 23 // RedBlackTree holds elements of the red-black tree. 24 type RedBlackTree struct { 25 mu *rwmutex.RWMutex 26 root *RedBlackTreeNode 27 size int 28 comparator func(v1, v2 interface{}) int 29 } 30 31 // RedBlackTreeNode is a single element within the tree. 32 type RedBlackTreeNode struct { 33 Key interface{} 34 Value interface{} 35 color color 36 left *RedBlackTreeNode 37 right *RedBlackTreeNode 38 parent *RedBlackTreeNode 39 } 40 41 // NewRedBlackTree instantiates a red-black tree with the custom key comparator. 42 // The parameter <unsafe> used to specify whether using tree in un-concurrent-safety, 43 // which is false in default. 44 func NewRedBlackTree(comparator func(v1, v2 interface{}) int, unsafe ...bool) *RedBlackTree { 45 return &RedBlackTree{ 46 mu: rwmutex.New(unsafe...), 47 comparator: comparator, 48 } 49 } 50 51 // NewRedBlackTreeFrom instantiates a red-black tree with the custom key comparator and <data> map. 52 // The parameter <unsafe> used to specify whether using tree in un-concurrent-safety, 53 // which is false in default. 54 func NewRedBlackTreeFrom(comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, unsafe ...bool) *RedBlackTree { 55 tree := NewRedBlackTree(comparator, unsafe...) 56 for k, v := range data { 57 tree.doSet(k, v) 58 } 59 return tree 60 } 61 62 // Clone returns a new tree with a copy of current tree. 63 func (tree *RedBlackTree) Clone(unsafe ...bool) *RedBlackTree { 64 newTree := NewRedBlackTree(tree.comparator, !tree.mu.IsSafe()) 65 newTree.Sets(tree.Map()) 66 return newTree 67 } 68 69 // Set inserts key-value item into the tree. 70 func (tree *RedBlackTree) Set(key interface{}, value interface{}) { 71 tree.mu.Lock() 72 defer tree.mu.Unlock() 73 tree.doSet(key, value) 74 } 75 76 // Sets batch sets key-values to the tree. 77 func (tree *RedBlackTree) Sets(data map[interface{}]interface{}) { 78 tree.mu.Lock() 79 defer tree.mu.Unlock() 80 for k, v := range data { 81 tree.doSet(k, v) 82 } 83 } 84 85 // doSet inserts key-value item into the tree without mutex. 86 func (tree *RedBlackTree) doSet(key interface{}, value interface{}) { 87 insertedNode := (*RedBlackTreeNode)(nil) 88 if tree.root == nil { 89 // Assert key is of comparator's type for initial tree 90 tree.comparator(key, key) 91 tree.root = &RedBlackTreeNode{Key: key, Value: value, color: red} 92 insertedNode = tree.root 93 } else { 94 node := tree.root 95 loop := true 96 for loop { 97 compare := tree.comparator(key, node.Key) 98 switch { 99 case compare == 0: 100 //node.Key = key 101 node.Value = value 102 return 103 case compare < 0: 104 if node.left == nil { 105 node.left = &RedBlackTreeNode{Key: key, Value: value, color: red} 106 insertedNode = node.left 107 loop = false 108 } else { 109 node = node.left 110 } 111 case compare > 0: 112 if node.right == nil { 113 node.right = &RedBlackTreeNode{Key: key, Value: value, color: red} 114 insertedNode = node.right 115 loop = false 116 } else { 117 node = node.right 118 } 119 } 120 } 121 insertedNode.parent = node 122 } 123 tree.insertCase1(insertedNode) 124 tree.size++ 125 } 126 127 // Get searches the node in the tree by <key> and returns its value or nil if key is not found in tree. 128 func (tree *RedBlackTree) Get(key interface{}) (value interface{}) { 129 value, _ = tree.Search(key) 130 return 131 } 132 133 // doSetWithLockCheck checks whether value of the key exists with mutex.Lock, 134 // if not exists, set value to the map with given <key>, 135 // or else just return the existing value. 136 // 137 // When setting value, if <value> is type of <func() interface {}>, 138 // it will be executed with mutex.Lock of the hash map, 139 // and its return value will be set to the map with <key>. 140 // 141 // It returns value with given <key>. 142 func (tree *RedBlackTree) doSetWithLockCheck(key interface{}, value interface{}) interface{} { 143 tree.mu.Lock() 144 defer tree.mu.Unlock() 145 if node := tree.doSearch(key); node != nil { 146 return node.Value 147 } 148 if f, ok := value.(func() interface{}); ok { 149 value = f() 150 } 151 tree.doSet(key, value) 152 return value 153 } 154 155 // GetOrSet returns the value by key, 156 // or set value with given <value> if not exist and returns this value. 157 func (tree *RedBlackTree) GetOrSet(key interface{}, value interface{}) interface{} { 158 if v, ok := tree.Search(key); !ok { 159 return tree.doSetWithLockCheck(key, value) 160 } else { 161 return v 162 } 163 } 164 165 // GetOrSetFunc returns the value by key, 166 // or sets value with return value of callback function <f> if not exist 167 // and returns this value. 168 func (tree *RedBlackTree) GetOrSetFunc(key interface{}, f func() interface{}) interface{} { 169 if v, ok := tree.Search(key); !ok { 170 return tree.doSetWithLockCheck(key, f()) 171 } else { 172 return v 173 } 174 } 175 176 // GetOrSetFuncLock returns the value by key, 177 // or sets value with return value of callback function <f> if not exist 178 // and returns this value. 179 // 180 // GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function <f> 181 // with mutex.Lock of the hash map. 182 func (tree *RedBlackTree) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{} { 183 if v, ok := tree.Search(key); !ok { 184 return tree.doSetWithLockCheck(key, f) 185 } else { 186 return v 187 } 188 } 189 190 // GetVar returns a gvar.Var with the value by given <key>. 191 // The returned gvar.Var is un-concurrent safe. 192 func (tree *RedBlackTree) GetVar(key interface{}) *gvar.Var { 193 return gvar.New(tree.Get(key), true) 194 } 195 196 // GetVarOrSet returns a gvar.Var with result from GetVarOrSet. 197 // The returned gvar.Var is un-concurrent safe. 198 func (tree *RedBlackTree) GetVarOrSet(key interface{}, value interface{}) *gvar.Var { 199 return gvar.New(tree.GetOrSet(key, value), true) 200 } 201 202 // GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc. 203 // The returned gvar.Var is un-concurrent safe. 204 func (tree *RedBlackTree) GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var { 205 return gvar.New(tree.GetOrSetFunc(key, f), true) 206 } 207 208 // GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock. 209 // The returned gvar.Var is un-concurrent safe. 210 func (tree *RedBlackTree) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var { 211 return gvar.New(tree.GetOrSetFuncLock(key, f), true) 212 } 213 214 // SetIfNotExist sets <value> to the map if the <key> does not exist, then return true. 215 // It returns false if <key> exists, and <value> would be ignored. 216 func (tree *RedBlackTree) SetIfNotExist(key interface{}, value interface{}) bool { 217 if !tree.Contains(key) { 218 tree.doSetWithLockCheck(key, value) 219 return true 220 } 221 return false 222 } 223 224 // SetIfNotExistFunc sets value with return value of callback function <f>, then return true. 225 // It returns false if <key> exists, and <value> would be ignored. 226 func (tree *RedBlackTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool { 227 if !tree.Contains(key) { 228 tree.doSetWithLockCheck(key, f()) 229 return true 230 } 231 return false 232 } 233 234 // SetIfNotExistFuncLock sets value with return value of callback function <f>, then return true. 235 // It returns false if <key> exists, and <value> would be ignored. 236 // 237 // SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that 238 // it executes function <f> with mutex.Lock of the hash map. 239 func (tree *RedBlackTree) SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool { 240 if !tree.Contains(key) { 241 tree.doSetWithLockCheck(key, f) 242 return true 243 } 244 return false 245 } 246 247 // Contains checks whether <key> exists in the tree. 248 func (tree *RedBlackTree) Contains(key interface{}) bool { 249 _, ok := tree.Search(key) 250 return ok 251 } 252 253 // doRemove removes the node from the tree by <key> without mutex. 254 func (tree *RedBlackTree) doRemove(key interface{}) (value interface{}) { 255 child := (*RedBlackTreeNode)(nil) 256 node := tree.doSearch(key) 257 if node == nil { 258 return 259 } 260 value = node.Value 261 if node.left != nil && node.right != nil { 262 p := node.left.maximumNode() 263 node.Key = p.Key 264 node.Value = p.Value 265 node = p 266 } 267 if node.left == nil || node.right == nil { 268 if node.right == nil { 269 child = node.left 270 } else { 271 child = node.right 272 } 273 if node.color == black { 274 node.color = tree.nodeColor(child) 275 tree.deleteCase1(node) 276 } 277 tree.replaceNode(node, child) 278 if node.parent == nil && child != nil { 279 child.color = black 280 } 281 } 282 tree.size-- 283 return 284 } 285 286 // Remove removes the node from the tree by <key>. 287 func (tree *RedBlackTree) Remove(key interface{}) (value interface{}) { 288 tree.mu.Lock() 289 defer tree.mu.Unlock() 290 return tree.doRemove(key) 291 } 292 293 // Removes batch deletes values of the tree by <keys>. 294 func (tree *RedBlackTree) Removes(keys []interface{}) { 295 tree.mu.Lock() 296 defer tree.mu.Unlock() 297 for _, key := range keys { 298 tree.doRemove(key) 299 } 300 } 301 302 // IsEmpty returns true if tree does not contain any nodes. 303 func (tree *RedBlackTree) IsEmpty() bool { 304 return tree.Size() == 0 305 } 306 307 // Size returns number of nodes in the tree. 308 func (tree *RedBlackTree) Size() int { 309 tree.mu.RLock() 310 defer tree.mu.RUnlock() 311 return tree.size 312 } 313 314 // Keys returns all keys in asc order. 315 func (tree *RedBlackTree) Keys() []interface{} { 316 keys := make([]interface{}, tree.Size()) 317 index := 0 318 tree.IteratorAsc(func(key, value interface{}) bool { 319 keys[index] = key 320 index++ 321 return true 322 }) 323 return keys 324 } 325 326 // Values returns all values in asc order based on the key. 327 func (tree *RedBlackTree) Values() []interface{} { 328 values := make([]interface{}, tree.Size()) 329 index := 0 330 tree.IteratorAsc(func(key, value interface{}) bool { 331 values[index] = value 332 index++ 333 return true 334 }) 335 return values 336 } 337 338 // Map returns all key-value items as map. 339 func (tree *RedBlackTree) Map() map[interface{}]interface{} { 340 m := make(map[interface{}]interface{}, tree.Size()) 341 tree.IteratorAsc(func(key, value interface{}) bool { 342 m[key] = value 343 return true 344 }) 345 return m 346 } 347 348 // Left returns the left-most (min) node or nil if tree is empty. 349 func (tree *RedBlackTree) Left() *RedBlackTreeNode { 350 tree.mu.RLock() 351 defer tree.mu.RUnlock() 352 node := tree.leftNode() 353 if tree.mu.IsSafe() { 354 return &RedBlackTreeNode{ 355 Key: node.Key, 356 Value: node.Value, 357 } 358 } 359 return node 360 } 361 362 // Right returns the right-most (max) node or nil if tree is empty. 363 func (tree *RedBlackTree) Right() *RedBlackTreeNode { 364 tree.mu.RLock() 365 defer tree.mu.RUnlock() 366 node := tree.rightNode() 367 if tree.mu.IsSafe() { 368 return &RedBlackTreeNode{ 369 Key: node.Key, 370 Value: node.Value, 371 } 372 } 373 return node 374 } 375 376 // leftNode returns the left-most (min) node or nil if tree is empty. 377 func (tree *RedBlackTree) leftNode() *RedBlackTreeNode { 378 p := (*RedBlackTreeNode)(nil) 379 n := tree.root 380 for n != nil { 381 p = n 382 n = n.left 383 } 384 return p 385 } 386 387 // rightNode returns the right-most (max) node or nil if tree is empty. 388 func (tree *RedBlackTree) rightNode() *RedBlackTreeNode { 389 p := (*RedBlackTreeNode)(nil) 390 n := tree.root 391 for n != nil { 392 p = n 393 n = n.right 394 } 395 return p 396 } 397 398 // Floor Finds floor node of the input key, return the floor node or nil if no floor node is found. 399 // Second return parameter is true if floor was found, otherwise false. 400 // 401 // Floor node is defined as the largest node that its key is smaller than or equal to the given <key>. 402 // A floor node may not be found, either because the tree is empty, or because 403 // all nodes in the tree are larger than the given node. 404 func (tree *RedBlackTree) Floor(key interface{}) (floor *RedBlackTreeNode, found bool) { 405 tree.mu.RLock() 406 defer tree.mu.RUnlock() 407 n := tree.root 408 for n != nil { 409 compare := tree.comparator(key, n.Key) 410 switch { 411 case compare == 0: 412 return n, true 413 case compare < 0: 414 n = n.left 415 case compare > 0: 416 floor, found = n, true 417 n = n.right 418 } 419 } 420 if found { 421 return 422 } 423 return nil, false 424 } 425 426 // Ceiling finds ceiling node of the input key, return the ceiling node or nil if no ceiling node is found. 427 // Second return parameter is true if ceiling was found, otherwise false. 428 // 429 // Ceiling node is defined as the smallest node that its key is larger than or equal to the given <key>. 430 // A ceiling node may not be found, either because the tree is empty, or because 431 // all nodes in the tree are smaller than the given node. 432 func (tree *RedBlackTree) Ceiling(key interface{}) (ceiling *RedBlackTreeNode, found bool) { 433 tree.mu.RLock() 434 defer tree.mu.RUnlock() 435 n := tree.root 436 for n != nil { 437 compare := tree.comparator(key, n.Key) 438 switch { 439 case compare == 0: 440 return n, true 441 case compare > 0: 442 n = n.right 443 case compare < 0: 444 ceiling, found = n, true 445 n = n.left 446 } 447 } 448 if found { 449 return 450 } 451 return nil, false 452 } 453 454 // Iterator is alias of IteratorAsc. 455 func (tree *RedBlackTree) Iterator(f func(key, value interface{}) bool) { 456 tree.IteratorAsc(f) 457 } 458 459 // IteratorAsc iterates the tree in ascending order with given callback function <f>. 460 // If <f> returns true, then it continues iterating; or false to stop. 461 func (tree *RedBlackTree) IteratorAsc(f func(key, value interface{}) bool) { 462 tree.mu.RLock() 463 defer tree.mu.RUnlock() 464 node := tree.leftNode() 465 if node == nil { 466 return 467 } 468 loop: 469 if node == nil { 470 return 471 } 472 if !f(node.Key, node.Value) { 473 return 474 } 475 if node.right != nil { 476 node = node.right 477 for node.left != nil { 478 node = node.left 479 } 480 goto loop 481 } 482 if node.parent != nil { 483 old := node 484 for node.parent != nil { 485 node = node.parent 486 if tree.comparator(old.Key, node.Key) <= 0 { 487 goto loop 488 } 489 } 490 } 491 } 492 493 // IteratorDesc iterates the tree in descending order with given callback function <f>. 494 // If <f> returns true, then it continues iterating; or false to stop. 495 func (tree *RedBlackTree) IteratorDesc(f func(key, value interface{}) bool) { 496 tree.mu.RLock() 497 defer tree.mu.RUnlock() 498 node := tree.rightNode() 499 if node == nil { 500 return 501 } 502 loop: 503 if node == nil { 504 return 505 } 506 if !f(node.Key, node.Value) { 507 return 508 } 509 if node.left != nil { 510 node = node.left 511 for node.right != nil { 512 node = node.right 513 } 514 goto loop 515 } 516 if node.parent != nil { 517 old := node 518 for node.parent != nil { 519 node = node.parent 520 if tree.comparator(old.Key, node.Key) >= 0 { 521 goto loop 522 } 523 } 524 } 525 } 526 527 // Clear removes all nodes from the tree. 528 func (tree *RedBlackTree) Clear() { 529 tree.mu.Lock() 530 defer tree.mu.Unlock() 531 tree.root = nil 532 tree.size = 0 533 } 534 535 // String returns a string representation of container. 536 func (tree *RedBlackTree) String() string { 537 tree.mu.RLock() 538 defer tree.mu.RUnlock() 539 str := "RedBlackTree\n" 540 if tree.size != 0 { 541 tree.output(tree.root, "", true, &str) 542 } 543 return str 544 } 545 546 // Print prints the tree to stdout. 547 func (tree *RedBlackTree) Print() { 548 fmt.Println(tree.String()) 549 } 550 551 // Search searches the tree with given <key>. 552 // Second return parameter <found> is true if key was found, otherwise false. 553 func (tree *RedBlackTree) Search(key interface{}) (value interface{}, found bool) { 554 tree.mu.RLock() 555 defer tree.mu.RUnlock() 556 node := tree.doSearch(key) 557 if node != nil { 558 return node.Value, true 559 } 560 return nil, false 561 } 562 563 // Flip exchanges key-value of the tree to value-key. 564 // Note that you should guarantee the value is the same type as key, 565 // or else the comparator would panic. 566 // 567 // If the type of value is different with key, you pass the new <comparator>. 568 func (tree *RedBlackTree) Flip(comparator ...func(v1, v2 interface{}) int) { 569 t := (*RedBlackTree)(nil) 570 if len(comparator) > 0 { 571 t = NewRedBlackTree(comparator[0], !tree.mu.IsSafe()) 572 } else { 573 t = NewRedBlackTree(tree.comparator, !tree.mu.IsSafe()) 574 } 575 tree.IteratorAsc(func(key, value interface{}) bool { 576 t.doSet(value, key) 577 return true 578 }) 579 tree.mu.Lock() 580 tree.root = t.root 581 tree.size = t.size 582 tree.mu.Unlock() 583 } 584 585 func (tree *RedBlackTree) output(node *RedBlackTreeNode, prefix string, isTail bool, str *string) { 586 if node.right != nil { 587 newPrefix := prefix 588 if isTail { 589 newPrefix += "│ " 590 } else { 591 newPrefix += " " 592 } 593 tree.output(node.right, newPrefix, false, str) 594 } 595 *str += prefix 596 if isTail { 597 *str += "└── " 598 } else { 599 *str += "┌── " 600 } 601 *str += fmt.Sprintf("%v\n", node.Key) 602 if node.left != nil { 603 newPrefix := prefix 604 if isTail { 605 newPrefix += " " 606 } else { 607 newPrefix += "│ " 608 } 609 tree.output(node.left, newPrefix, true, str) 610 } 611 } 612 613 // doSearch searches the tree with given <key> without mutex. 614 // It returns the node if found or otherwise nil. 615 func (tree *RedBlackTree) doSearch(key interface{}) *RedBlackTreeNode { 616 node := tree.root 617 for node != nil { 618 compare := tree.comparator(key, node.Key) 619 switch { 620 case compare == 0: 621 return node 622 case compare < 0: 623 node = node.left 624 case compare > 0: 625 node = node.right 626 } 627 } 628 return nil 629 } 630 631 func (node *RedBlackTreeNode) grandparent() *RedBlackTreeNode { 632 if node != nil && node.parent != nil { 633 return node.parent.parent 634 } 635 return nil 636 } 637 638 func (node *RedBlackTreeNode) uncle() *RedBlackTreeNode { 639 if node == nil || node.parent == nil || node.parent.parent == nil { 640 return nil 641 } 642 return node.parent.sibling() 643 } 644 645 func (node *RedBlackTreeNode) sibling() *RedBlackTreeNode { 646 if node == nil || node.parent == nil { 647 return nil 648 } 649 if node == node.parent.left { 650 return node.parent.right 651 } 652 return node.parent.left 653 } 654 655 func (tree *RedBlackTree) rotateLeft(node *RedBlackTreeNode) { 656 right := node.right 657 tree.replaceNode(node, right) 658 node.right = right.left 659 if right.left != nil { 660 right.left.parent = node 661 } 662 right.left = node 663 node.parent = right 664 } 665 666 func (tree *RedBlackTree) rotateRight(node *RedBlackTreeNode) { 667 left := node.left 668 tree.replaceNode(node, left) 669 node.left = left.right 670 if left.right != nil { 671 left.right.parent = node 672 } 673 left.right = node 674 node.parent = left 675 } 676 677 func (tree *RedBlackTree) replaceNode(old *RedBlackTreeNode, new *RedBlackTreeNode) { 678 if old.parent == nil { 679 tree.root = new 680 } else { 681 if old == old.parent.left { 682 old.parent.left = new 683 } else { 684 old.parent.right = new 685 } 686 } 687 if new != nil { 688 new.parent = old.parent 689 } 690 } 691 692 func (tree *RedBlackTree) insertCase1(node *RedBlackTreeNode) { 693 if node.parent == nil { 694 node.color = black 695 } else { 696 tree.insertCase2(node) 697 } 698 } 699 700 func (tree *RedBlackTree) insertCase2(node *RedBlackTreeNode) { 701 if tree.nodeColor(node.parent) == black { 702 return 703 } 704 tree.insertCase3(node) 705 } 706 707 func (tree *RedBlackTree) insertCase3(node *RedBlackTreeNode) { 708 uncle := node.uncle() 709 if tree.nodeColor(uncle) == red { 710 node.parent.color = black 711 uncle.color = black 712 node.grandparent().color = red 713 tree.insertCase1(node.grandparent()) 714 } else { 715 tree.insertCase4(node) 716 } 717 } 718 719 func (tree *RedBlackTree) insertCase4(node *RedBlackTreeNode) { 720 grandparent := node.grandparent() 721 if node == node.parent.right && node.parent == grandparent.left { 722 tree.rotateLeft(node.parent) 723 node = node.left 724 } else if node == node.parent.left && node.parent == grandparent.right { 725 tree.rotateRight(node.parent) 726 node = node.right 727 } 728 tree.insertCase5(node) 729 } 730 731 func (tree *RedBlackTree) insertCase5(node *RedBlackTreeNode) { 732 node.parent.color = black 733 grandparent := node.grandparent() 734 grandparent.color = red 735 if node == node.parent.left && node.parent == grandparent.left { 736 tree.rotateRight(grandparent) 737 } else if node == node.parent.right && node.parent == grandparent.right { 738 tree.rotateLeft(grandparent) 739 } 740 } 741 742 func (node *RedBlackTreeNode) maximumNode() *RedBlackTreeNode { 743 if node == nil { 744 return nil 745 } 746 for node.right != nil { 747 return node.right 748 } 749 return node 750 } 751 752 func (tree *RedBlackTree) deleteCase1(node *RedBlackTreeNode) { 753 if node.parent == nil { 754 return 755 } 756 tree.deleteCase2(node) 757 } 758 759 func (tree *RedBlackTree) deleteCase2(node *RedBlackTreeNode) { 760 sibling := node.sibling() 761 if tree.nodeColor(sibling) == red { 762 node.parent.color = red 763 sibling.color = black 764 if node == node.parent.left { 765 tree.rotateLeft(node.parent) 766 } else { 767 tree.rotateRight(node.parent) 768 } 769 } 770 tree.deleteCase3(node) 771 } 772 773 func (tree *RedBlackTree) deleteCase3(node *RedBlackTreeNode) { 774 sibling := node.sibling() 775 if tree.nodeColor(node.parent) == black && 776 tree.nodeColor(sibling) == black && 777 tree.nodeColor(sibling.left) == black && 778 tree.nodeColor(sibling.right) == black { 779 sibling.color = red 780 tree.deleteCase1(node.parent) 781 } else { 782 tree.deleteCase4(node) 783 } 784 } 785 786 func (tree *RedBlackTree) deleteCase4(node *RedBlackTreeNode) { 787 sibling := node.sibling() 788 if tree.nodeColor(node.parent) == red && 789 tree.nodeColor(sibling) == black && 790 tree.nodeColor(sibling.left) == black && 791 tree.nodeColor(sibling.right) == black { 792 sibling.color = red 793 node.parent.color = black 794 } else { 795 tree.deleteCase5(node) 796 } 797 } 798 799 func (tree *RedBlackTree) deleteCase5(node *RedBlackTreeNode) { 800 sibling := node.sibling() 801 if node == node.parent.left && 802 tree.nodeColor(sibling) == black && 803 tree.nodeColor(sibling.left) == red && 804 tree.nodeColor(sibling.right) == black { 805 sibling.color = red 806 sibling.left.color = black 807 tree.rotateRight(sibling) 808 } else if node == node.parent.right && 809 tree.nodeColor(sibling) == black && 810 tree.nodeColor(sibling.right) == red && 811 tree.nodeColor(sibling.left) == black { 812 sibling.color = red 813 sibling.right.color = black 814 tree.rotateLeft(sibling) 815 } 816 tree.deleteCase6(node) 817 } 818 819 func (tree *RedBlackTree) deleteCase6(node *RedBlackTreeNode) { 820 sibling := node.sibling() 821 sibling.color = tree.nodeColor(node.parent) 822 node.parent.color = black 823 if node == node.parent.left && tree.nodeColor(sibling.right) == red { 824 sibling.right.color = black 825 tree.rotateLeft(node.parent) 826 } else if tree.nodeColor(sibling.left) == red { 827 sibling.left.color = black 828 tree.rotateRight(node.parent) 829 } 830 } 831 832 func (tree *RedBlackTree) nodeColor(node *RedBlackTreeNode) color { 833 if node == nil { 834 return black 835 } 836 return node.color 837 } 838 839 // MarshalJSON implements the interface MarshalJSON for json.Marshal. 840 func (tree *RedBlackTree) MarshalJSON() ([]byte, error) { 841 return json.Marshal(tree.Map()) 842 }