2015-03-04 18:38:35 +00:00
|
|
|
/*
|
2015-03-06 16:10:34 +00:00
|
|
|
Copyright (c) 2015, Emir Pasic
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2015-03-04 18:38:35 +00:00
|
|
|
*/
|
|
|
|
|
2015-03-04 18:46:09 +00:00
|
|
|
// Implementation of Red-black tree.
|
|
|
|
// Used by TreeSet and TreeMap.
|
2015-03-04 20:13:12 +00:00
|
|
|
// Structure is not thread safe.
|
2015-03-04 18:38:35 +00:00
|
|
|
// References: http://en.wikipedia.org/wiki/Red%E2%80%93black_tree
|
2015-03-05 05:55:53 +00:00
|
|
|
|
2015-03-04 18:38:35 +00:00
|
|
|
package redblacktree
|
|
|
|
|
|
|
|
import (
|
2015-03-04 20:13:12 +00:00
|
|
|
"fmt"
|
2015-03-05 13:37:28 +00:00
|
|
|
"github.com/emirpasic/gods/stacks/linkedliststack"
|
2015-03-04 18:38:35 +00:00
|
|
|
"github.com/emirpasic/gods/utils"
|
|
|
|
)
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
type color bool
|
2015-03-04 18:38:35 +00:00
|
|
|
|
|
|
|
const (
|
2015-03-05 04:27:36 +00:00
|
|
|
black, red color = true, false
|
2015-03-04 18:38:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Tree struct {
|
2015-03-05 04:27:36 +00:00
|
|
|
root *node
|
2015-03-05 04:20:10 +00:00
|
|
|
size int
|
2015-03-04 18:38:35 +00:00
|
|
|
comparator utils.Comparator
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
type node struct {
|
2015-03-04 18:38:35 +00:00
|
|
|
key interface{}
|
|
|
|
value interface{}
|
2015-03-05 04:27:36 +00:00
|
|
|
color color
|
|
|
|
left *node
|
|
|
|
right *node
|
|
|
|
parent *node
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
|
2015-03-04 19:20:48 +00:00
|
|
|
// Instantiates a red-black tree with the custom comparator.
|
2015-03-04 18:38:35 +00:00
|
|
|
func NewWith(comparator utils.Comparator) *Tree {
|
|
|
|
return &Tree{comparator: comparator}
|
|
|
|
}
|
|
|
|
|
2015-03-04 19:20:48 +00:00
|
|
|
// Instantiates a red-black tree with the IntComparator, i.e. keys are of type int.
|
2015-03-04 18:38:35 +00:00
|
|
|
func NewWithIntComparator() *Tree {
|
|
|
|
return &Tree{comparator: utils.IntComparator}
|
|
|
|
}
|
|
|
|
|
2015-03-04 19:20:48 +00:00
|
|
|
// Instantiates a red-black tree with the StringComparator, i.e. keys are of type string.
|
2015-03-04 18:38:35 +00:00
|
|
|
func NewWithStringComparator() *Tree {
|
|
|
|
return &Tree{comparator: utils.StringComparator}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inserts node into the tree.
|
|
|
|
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
|
|
|
func (tree *Tree) Put(key interface{}, value interface{}) {
|
2015-03-05 04:27:36 +00:00
|
|
|
insertedNode := &node{key: key, value: value, color: red}
|
2015-03-04 18:38:35 +00:00
|
|
|
if tree.root == nil {
|
|
|
|
tree.root = insertedNode
|
|
|
|
} else {
|
|
|
|
node := tree.root
|
|
|
|
loop := true
|
|
|
|
for loop {
|
|
|
|
compare := tree.comparator(key, node.key)
|
|
|
|
switch {
|
|
|
|
case compare == 0:
|
|
|
|
node.value = value
|
|
|
|
return
|
|
|
|
case compare < 0:
|
|
|
|
if node.left == nil {
|
|
|
|
node.left = insertedNode
|
|
|
|
loop = false
|
|
|
|
} else {
|
|
|
|
node = node.left
|
|
|
|
}
|
|
|
|
case compare > 0:
|
|
|
|
if node.right == nil {
|
|
|
|
node.right = insertedNode
|
2015-03-05 00:18:24 +00:00
|
|
|
loop = false
|
2015-03-04 18:38:35 +00:00
|
|
|
} else {
|
|
|
|
node = node.right
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
insertedNode.parent = node
|
|
|
|
}
|
|
|
|
tree.insertCase1(insertedNode)
|
2015-03-05 04:20:10 +00:00
|
|
|
tree.size += 1
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Searches the node in the tree by key and returns its value or nil if key is not found in tree.
|
|
|
|
// Second return parameter is true if key was found, otherwise false.
|
2015-03-04 19:20:48 +00:00
|
|
|
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
2015-03-05 05:55:53 +00:00
|
|
|
func (tree *Tree) Get(key interface{}) (value interface{}, found bool) {
|
2015-03-04 18:38:35 +00:00
|
|
|
node := tree.lookup(key)
|
|
|
|
if node != nil {
|
|
|
|
return node.value, true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2015-03-04 19:20:48 +00:00
|
|
|
// Remove the node from the tree by key.
|
|
|
|
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
2015-03-04 18:46:09 +00:00
|
|
|
func (tree *Tree) Remove(key interface{}) {
|
2015-03-05 04:27:36 +00:00
|
|
|
var child *node
|
2015-03-04 19:20:48 +00:00
|
|
|
node := tree.lookup(key)
|
|
|
|
if node == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if node.left != nil && node.right != nil {
|
|
|
|
pred := node.left.maximumNode()
|
|
|
|
node.key = pred.key
|
|
|
|
node.value = pred.value
|
|
|
|
node = pred
|
|
|
|
}
|
2015-03-04 20:13:12 +00:00
|
|
|
if node.left == nil || node.right == nil {
|
|
|
|
if node.right == nil {
|
|
|
|
child = node.left
|
|
|
|
} else {
|
|
|
|
child = node.right
|
|
|
|
}
|
2015-03-05 04:27:36 +00:00
|
|
|
if node.color == black {
|
|
|
|
node.color = nodeColor(child)
|
2015-03-04 20:13:12 +00:00
|
|
|
tree.deleteCase1(node)
|
|
|
|
}
|
|
|
|
tree.replaceNode(node, child)
|
|
|
|
if node.parent == nil && child != nil {
|
2015-03-05 04:27:36 +00:00
|
|
|
child.color = black
|
2015-03-04 20:13:12 +00:00
|
|
|
}
|
2015-03-04 19:20:48 +00:00
|
|
|
}
|
2015-03-05 04:20:10 +00:00
|
|
|
tree.size -= 1
|
2015-03-04 18:46:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if tree does not contain any nodes
|
2015-03-05 05:55:53 +00:00
|
|
|
func (tree *Tree) Empty() bool {
|
|
|
|
return tree.size == 0
|
2015-03-04 18:46:09 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 04:20:10 +00:00
|
|
|
// Returns number of nodes in the tree.
|
|
|
|
func (tree *Tree) Size() int {
|
|
|
|
return tree.size
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:37:28 +00:00
|
|
|
// Returns all keys in-order
|
|
|
|
func (tree *Tree) Keys() []interface{} {
|
|
|
|
keys := make([]interface{}, tree.size)
|
|
|
|
for i, node := range tree.inOrder() {
|
|
|
|
keys[i] = node.key
|
|
|
|
}
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
2015-03-05 14:05:12 +00:00
|
|
|
// Returns all values in-order based on the key.
|
2015-03-05 13:37:28 +00:00
|
|
|
func (tree *Tree) Values() []interface{} {
|
|
|
|
values := make([]interface{}, tree.size)
|
|
|
|
for i, node := range tree.inOrder() {
|
|
|
|
values[i] = node.value
|
|
|
|
}
|
|
|
|
return values
|
|
|
|
}
|
|
|
|
|
2015-03-05 14:05:12 +00:00
|
|
|
// Removes all nodes from the tree.
|
|
|
|
func (tree *Tree) Clear() {
|
|
|
|
tree.root = nil
|
|
|
|
tree.size = 0
|
|
|
|
}
|
|
|
|
|
2015-03-04 20:13:12 +00:00
|
|
|
func (tree *Tree) String() string {
|
2015-03-05 03:15:29 +00:00
|
|
|
str := "RedBlackTree\n"
|
2015-03-05 05:55:53 +00:00
|
|
|
if !tree.Empty() {
|
2015-03-05 03:15:29 +00:00
|
|
|
output(tree.root, "", true, &str)
|
2015-03-04 20:13:12 +00:00
|
|
|
}
|
2015-03-05 03:15:29 +00:00
|
|
|
return str
|
2015-03-04 20:13:12 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (node *node) String() string {
|
2015-03-05 03:15:29 +00:00
|
|
|
return fmt.Sprintf("%v", node.key)
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:37:28 +00:00
|
|
|
// Returns all nodes in order
|
|
|
|
func (tree *Tree) inOrder() []*node {
|
|
|
|
nodes := make([]*node, tree.size)
|
|
|
|
if tree.size > 0 {
|
|
|
|
current := tree.root
|
|
|
|
stack := linkedliststack.New()
|
|
|
|
done := false
|
|
|
|
count := 0
|
|
|
|
for !done {
|
|
|
|
if current != nil {
|
|
|
|
stack.Push(current)
|
|
|
|
current = current.left
|
|
|
|
} else {
|
|
|
|
if !stack.Empty() {
|
|
|
|
currentPop, _ := stack.Pop()
|
|
|
|
current = currentPop.(*node)
|
|
|
|
nodes[count] = current
|
|
|
|
count += 1
|
|
|
|
current = current.right
|
|
|
|
} else {
|
|
|
|
done = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func output(node *node, prefix string, isTail bool, str *string) {
|
2015-03-05 03:15:29 +00:00
|
|
|
if node.right != nil {
|
|
|
|
newPrefix := prefix
|
|
|
|
if isTail {
|
|
|
|
newPrefix += "│ "
|
|
|
|
} else {
|
|
|
|
newPrefix += " "
|
|
|
|
}
|
|
|
|
output(node.right, newPrefix, false, str)
|
|
|
|
}
|
|
|
|
*str += prefix
|
|
|
|
if isTail {
|
|
|
|
*str += "└── "
|
|
|
|
} else {
|
|
|
|
*str += "┌── "
|
|
|
|
}
|
|
|
|
*str += node.String() + "\n"
|
|
|
|
if node.left != nil {
|
|
|
|
newPrefix := prefix
|
|
|
|
if isTail {
|
|
|
|
newPrefix += " "
|
|
|
|
} else {
|
|
|
|
newPrefix += "│ "
|
|
|
|
}
|
|
|
|
output(node.left, newPrefix, true, str)
|
|
|
|
}
|
2015-03-04 20:13:12 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (tree *Tree) lookup(key interface{}) *node {
|
2015-03-04 18:38:35 +00:00
|
|
|
node := tree.root
|
|
|
|
for node != nil {
|
|
|
|
compare := tree.comparator(key, node.key)
|
|
|
|
switch {
|
|
|
|
case compare == 0:
|
|
|
|
return node
|
|
|
|
case compare < 0:
|
|
|
|
node = node.left
|
|
|
|
case compare > 0:
|
|
|
|
node = node.right
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (node *node) grandparent() *node {
|
2015-03-04 18:38:35 +00:00
|
|
|
if node != nil && node.parent != nil {
|
|
|
|
return node.parent.parent
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (node *node) uncle() *node {
|
2015-03-04 19:38:57 +00:00
|
|
|
if node == nil || node.parent == nil || node.parent.parent == nil {
|
2015-03-04 18:38:35 +00:00
|
|
|
return nil
|
2015-03-04 19:38:57 +00:00
|
|
|
}
|
|
|
|
return node.parent.sibling()
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (node *node) sibling() *node {
|
2015-03-04 19:38:57 +00:00
|
|
|
if node == nil || node.parent == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if node == node.parent.left {
|
|
|
|
return node.parent.right
|
|
|
|
} else {
|
|
|
|
return node.parent.left
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (tree *Tree) rotateLeft(node *node) {
|
2015-03-04 18:38:35 +00:00
|
|
|
right := node.right
|
|
|
|
tree.replaceNode(node, right)
|
|
|
|
node.right = right.left
|
|
|
|
if right.left != nil {
|
|
|
|
right.left.parent = node
|
|
|
|
}
|
|
|
|
right.left = node
|
|
|
|
node.parent = right
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (tree *Tree) rotateRight(node *node) {
|
2015-03-04 18:38:35 +00:00
|
|
|
left := node.left
|
|
|
|
tree.replaceNode(node, left)
|
|
|
|
node.left = left.right
|
|
|
|
if left.right != nil {
|
|
|
|
left.right.parent = node
|
|
|
|
}
|
|
|
|
left.right = node
|
|
|
|
node.parent = left
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (tree *Tree) replaceNode(old *node, new *node) {
|
2015-03-04 18:38:35 +00:00
|
|
|
if old.parent == nil {
|
|
|
|
tree.root = new
|
|
|
|
} else {
|
|
|
|
if old == old.parent.left {
|
|
|
|
old.parent.left = new
|
|
|
|
} else {
|
|
|
|
old.parent.right = new
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if new != nil {
|
|
|
|
new.parent = old.parent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (tree *Tree) insertCase1(node *node) {
|
2015-03-04 18:38:35 +00:00
|
|
|
if node.parent == nil {
|
2015-03-05 04:27:36 +00:00
|
|
|
node.color = black
|
2015-03-04 18:38:35 +00:00
|
|
|
} else {
|
|
|
|
tree.insertCase2(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (tree *Tree) insertCase2(node *node) {
|
|
|
|
if nodeColor(node.parent) == black {
|
2015-03-05 03:15:29 +00:00
|
|
|
return
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
tree.insertCase3(node)
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (tree *Tree) insertCase3(node *node) {
|
2015-03-04 19:38:57 +00:00
|
|
|
uncle := node.uncle()
|
2015-03-05 04:27:36 +00:00
|
|
|
if nodeColor(uncle) == red {
|
|
|
|
node.parent.color = black
|
|
|
|
uncle.color = black
|
|
|
|
node.grandparent().color = red
|
2015-03-04 18:38:35 +00:00
|
|
|
tree.insertCase1(node.grandparent())
|
|
|
|
} else {
|
|
|
|
tree.insertCase4(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (tree *Tree) insertCase4(node *node) {
|
2015-03-05 03:15:29 +00:00
|
|
|
grandparent := node.grandparent()
|
|
|
|
if node == node.parent.right && node.parent == grandparent.left {
|
2015-03-04 18:38:35 +00:00
|
|
|
tree.rotateLeft(node.parent)
|
|
|
|
node = node.left
|
2015-03-05 03:15:29 +00:00
|
|
|
} else if node == node.parent.left && node.parent == grandparent.right {
|
2015-03-04 18:38:35 +00:00
|
|
|
tree.rotateRight(node.parent)
|
|
|
|
node = node.right
|
|
|
|
}
|
|
|
|
tree.insertCase5(node)
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (tree *Tree) insertCase5(node *node) {
|
|
|
|
node.parent.color = black
|
2015-03-05 03:15:29 +00:00
|
|
|
grandparent := node.grandparent()
|
2015-03-05 04:27:36 +00:00
|
|
|
grandparent.color = red
|
2015-03-05 03:15:29 +00:00
|
|
|
if node == node.parent.left && node.parent == grandparent.left {
|
|
|
|
tree.rotateRight(grandparent)
|
|
|
|
} else if node == node.parent.right && node.parent == grandparent.right {
|
|
|
|
tree.rotateLeft(grandparent)
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-04 19:20:48 +00:00
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (node *node) maximumNode() *node {
|
2015-03-04 19:38:57 +00:00
|
|
|
if node == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-03-04 19:20:48 +00:00
|
|
|
for node.right != nil {
|
|
|
|
node = node.right
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (tree *Tree) deleteCase1(node *node) {
|
2015-03-04 19:20:48 +00:00
|
|
|
if node.parent == nil {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
tree.deleteCase2(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (tree *Tree) deleteCase2(node *node) {
|
2015-03-05 03:15:29 +00:00
|
|
|
sibling := node.sibling()
|
2015-03-05 04:27:36 +00:00
|
|
|
if nodeColor(sibling) == red {
|
|
|
|
node.parent.color = red
|
|
|
|
sibling.color = black
|
2015-03-04 19:20:48 +00:00
|
|
|
if node == node.parent.left {
|
|
|
|
tree.rotateLeft(node.parent)
|
|
|
|
} else {
|
|
|
|
tree.rotateRight(node.parent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tree.deleteCase3(node)
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (tree *Tree) deleteCase3(node *node) {
|
2015-03-05 03:15:29 +00:00
|
|
|
sibling := node.sibling()
|
2015-03-05 04:27:36 +00:00
|
|
|
if nodeColor(node.parent) == black &&
|
|
|
|
nodeColor(sibling) == black &&
|
|
|
|
nodeColor(sibling.left) == black &&
|
|
|
|
nodeColor(sibling.right) == black {
|
|
|
|
sibling.color = red
|
2015-03-04 19:20:48 +00:00
|
|
|
tree.deleteCase1(node.parent)
|
|
|
|
} else {
|
|
|
|
tree.deleteCase4(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (tree *Tree) deleteCase4(node *node) {
|
2015-03-05 03:15:29 +00:00
|
|
|
sibling := node.sibling()
|
2015-03-05 04:27:36 +00:00
|
|
|
if nodeColor(node.parent) == red &&
|
|
|
|
nodeColor(sibling) == black &&
|
|
|
|
nodeColor(sibling.left) == black &&
|
|
|
|
nodeColor(sibling.right) == black {
|
|
|
|
sibling.color = red
|
|
|
|
node.parent.color = black
|
2015-03-04 19:20:48 +00:00
|
|
|
} else {
|
|
|
|
tree.deleteCase5(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (tree *Tree) deleteCase5(node *node) {
|
2015-03-05 03:15:29 +00:00
|
|
|
sibling := node.sibling()
|
2015-03-04 19:20:48 +00:00
|
|
|
if node == node.parent.left &&
|
2015-03-05 04:27:36 +00:00
|
|
|
nodeColor(sibling) == black &&
|
|
|
|
nodeColor(sibling.left) == red &&
|
|
|
|
nodeColor(sibling.right) == black {
|
|
|
|
sibling.color = red
|
|
|
|
sibling.left.color = black
|
2015-03-05 03:15:29 +00:00
|
|
|
tree.rotateRight(sibling)
|
2015-03-04 19:20:48 +00:00
|
|
|
} else if node == node.parent.right &&
|
2015-03-05 04:27:36 +00:00
|
|
|
nodeColor(sibling) == black &&
|
|
|
|
nodeColor(sibling.right) == red &&
|
|
|
|
nodeColor(sibling.left) == black {
|
|
|
|
sibling.color = red
|
|
|
|
sibling.right.color = black
|
2015-03-05 03:15:29 +00:00
|
|
|
tree.rotateLeft(sibling)
|
2015-03-04 19:20:48 +00:00
|
|
|
}
|
|
|
|
tree.deleteCase6(node)
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func (tree *Tree) deleteCase6(node *node) {
|
2015-03-05 03:15:29 +00:00
|
|
|
sibling := node.sibling()
|
2015-03-05 04:27:36 +00:00
|
|
|
sibling.color = nodeColor(node.parent)
|
|
|
|
node.parent.color = black
|
|
|
|
if node == node.parent.left && nodeColor(sibling.right) == red {
|
|
|
|
sibling.right.color = black
|
2015-03-04 19:20:48 +00:00
|
|
|
tree.rotateLeft(node.parent)
|
2015-03-05 04:27:36 +00:00
|
|
|
} else if nodeColor(sibling.left) == red {
|
|
|
|
sibling.left.color = black
|
2015-03-04 19:20:48 +00:00
|
|
|
tree.rotateRight(node.parent)
|
|
|
|
}
|
|
|
|
}
|
2015-03-05 03:15:29 +00:00
|
|
|
|
2015-03-05 04:27:36 +00:00
|
|
|
func nodeColor(node *node) color {
|
2015-03-05 03:15:29 +00:00
|
|
|
if node == nil {
|
2015-03-05 04:27:36 +00:00
|
|
|
return black
|
2015-03-05 03:15:29 +00:00
|
|
|
}
|
|
|
|
return node.color
|
|
|
|
}
|