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-07 18:23:43 +00:00
|
|
|
"github.com/emirpasic/gods/trees"
|
2015-03-04 18:38:35 +00:00
|
|
|
"github.com/emirpasic/gods/utils"
|
|
|
|
)
|
|
|
|
|
2015-03-07 18:23:43 +00:00
|
|
|
func assertInterfaceImplementation() {
|
|
|
|
var _ trees.Interface = (*Tree)(nil)
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-03-23 04:40:01 +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
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
type Node struct {
|
|
|
|
Key interface{}
|
|
|
|
Value interface{}
|
2015-03-05 04:27:36 +00:00
|
|
|
color color
|
2016-03-23 04:40:01 +00:00
|
|
|
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{}) {
|
2016-03-23 04:40:01 +00:00
|
|
|
insertedNode := &Node{Key: key, Value: value, color: red}
|
|
|
|
if tree.Root == nil {
|
|
|
|
tree.Root = insertedNode
|
2015-03-04 18:38:35 +00:00
|
|
|
} else {
|
2016-03-23 04:40:01 +00:00
|
|
|
node := tree.Root
|
2015-03-04 18:38:35 +00:00
|
|
|
loop := true
|
|
|
|
for loop {
|
2016-03-23 04:40:01 +00:00
|
|
|
compare := tree.comparator(key, node.Key)
|
2015-03-04 18:38:35 +00:00
|
|
|
switch {
|
|
|
|
case compare == 0:
|
2016-03-23 04:40:01 +00:00
|
|
|
node.Value = value
|
2015-03-04 18:38:35 +00:00
|
|
|
return
|
|
|
|
case compare < 0:
|
2016-03-23 04:40:01 +00:00
|
|
|
if node.Left == nil {
|
|
|
|
node.Left = insertedNode
|
2015-03-04 18:38:35 +00:00
|
|
|
loop = false
|
|
|
|
} else {
|
2016-03-23 04:40:01 +00:00
|
|
|
node = node.Left
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
case compare > 0:
|
2016-03-23 04:40:01 +00:00
|
|
|
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 {
|
2016-03-23 04:40:01 +00:00
|
|
|
node = node.Right
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-23 04:40:01 +00:00
|
|
|
insertedNode.Parent = node
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
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.
|
2016-03-27 05:51:09 +00:00
|
|
|
// 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 {
|
2016-03-23 04:40:01 +00:00
|
|
|
return node.Value, true
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2016-03-27 05:51:09 +00:00
|
|
|
// Find ceiling node of the input key, return its key and value or nil if no ceiling is found.
|
|
|
|
// Third return parameter is true if ceiling was found, otherwise false.
|
|
|
|
// Ceiling node is defined as the smallest node that is larger than or equal to the given node.
|
|
|
|
// A ceiling node may not be found, either because the tree is empty, or because
|
|
|
|
// all nodes in the tree is smaller than the given node.
|
|
|
|
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
|
|
|
func (tree *Tree) Ceiling(key interface{}) (ceilingKey interface{}, value interface{}, found bool) {
|
|
|
|
ceiling := &Node{}
|
|
|
|
found = false
|
|
|
|
|
|
|
|
node := tree.Root
|
|
|
|
for node != nil {
|
|
|
|
compare := tree.comparator(key, node.Key)
|
|
|
|
switch {
|
|
|
|
case compare == 0:
|
|
|
|
return node.Key, node.Value, true
|
|
|
|
case compare < 0:
|
|
|
|
ceiling, found = node, true
|
|
|
|
node = node.Left
|
|
|
|
case compare > 0:
|
|
|
|
node = node.Right
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
return ceiling.Key, ceiling.Value, true
|
|
|
|
}
|
|
|
|
return nil, nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find floor node of the input key, return its key and value or nil if no ceiling is found.
|
|
|
|
// Third return parameter is true if floor was found, otherwise false.
|
|
|
|
// Floor node is defined as the largest node that is smaller than or equal to the given node.
|
|
|
|
// A floor node may not be found, either because the tree is empty, or because
|
|
|
|
// all nodes in the tree is larger than the given node.
|
|
|
|
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
|
|
|
func (tree *Tree) Floor(key interface{}) (floorKey interface{}, value interface{}, found bool) {
|
|
|
|
floor := &Node{}
|
|
|
|
found = false
|
|
|
|
|
|
|
|
node := tree.Root
|
|
|
|
for node != nil {
|
|
|
|
compare := tree.comparator(key, node.Key)
|
|
|
|
switch {
|
|
|
|
case compare == 0:
|
|
|
|
return node.Key, node.Value, true
|
|
|
|
case compare < 0:
|
|
|
|
node = node.Left
|
|
|
|
case compare > 0:
|
|
|
|
floor, found = node, true
|
|
|
|
node = node.Right
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
return floor.Key, floor.Value, true
|
|
|
|
}
|
|
|
|
return nil, 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{}) {
|
2016-03-23 04:40:01 +00:00
|
|
|
var child *Node
|
2015-03-04 19:20:48 +00:00
|
|
|
node := tree.lookup(key)
|
|
|
|
if node == nil {
|
|
|
|
return
|
|
|
|
}
|
2016-03-23 04:40:01 +00:00
|
|
|
if node.Left != nil && node.Right != nil {
|
|
|
|
pred := node.Left.maximumNode()
|
|
|
|
node.Key = pred.Key
|
|
|
|
node.Value = pred.Value
|
2015-03-04 19:20:48 +00:00
|
|
|
node = pred
|
|
|
|
}
|
2016-03-23 04:40:01 +00:00
|
|
|
if node.Left == nil || node.Right == nil {
|
|
|
|
if node.Right == nil {
|
|
|
|
child = node.Left
|
2015-03-04 20:13:12 +00:00
|
|
|
} else {
|
2016-03-23 04:40:01 +00:00
|
|
|
child = node.Right
|
2015-03-04 20:13:12 +00:00
|
|
|
}
|
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)
|
2016-03-23 04:40:01 +00:00
|
|
|
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() {
|
2016-03-23 04:40:01 +00:00
|
|
|
keys[i] = node.Key
|
2015-03-05 13:37:28 +00:00
|
|
|
}
|
|
|
|
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() {
|
2016-03-23 04:40:01 +00:00
|
|
|
values[i] = node.Value
|
2015-03-05 13:37:28 +00:00
|
|
|
}
|
|
|
|
return values
|
|
|
|
}
|
|
|
|
|
2015-03-05 14:05:12 +00:00
|
|
|
// Removes all nodes from the tree.
|
|
|
|
func (tree *Tree) Clear() {
|
2016-03-23 04:40:01 +00:00
|
|
|
tree.Root = nil
|
2015-03-05 14:05:12 +00:00
|
|
|
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() {
|
2016-03-23 04:40:01 +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
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
func (node *Node) String() string {
|
|
|
|
return fmt.Sprintf("%v", node.Key)
|
2015-03-05 03:15:29 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 13:37:28 +00:00
|
|
|
// Returns all nodes in order
|
2016-03-23 04:40:01 +00:00
|
|
|
func (tree *Tree) inOrder() []*Node {
|
|
|
|
nodes := make([]*Node, tree.size)
|
2015-03-05 13:37:28 +00:00
|
|
|
if tree.size > 0 {
|
2016-03-23 04:40:01 +00:00
|
|
|
current := tree.Root
|
2015-03-05 13:37:28 +00:00
|
|
|
stack := linkedliststack.New()
|
|
|
|
done := false
|
|
|
|
count := 0
|
|
|
|
for !done {
|
|
|
|
if current != nil {
|
|
|
|
stack.Push(current)
|
2016-03-23 04:40:01 +00:00
|
|
|
current = current.Left
|
2015-03-05 13:37:28 +00:00
|
|
|
} else {
|
|
|
|
if !stack.Empty() {
|
|
|
|
currentPop, _ := stack.Pop()
|
2016-03-23 04:40:01 +00:00
|
|
|
current = currentPop.(*Node)
|
2015-03-05 13:37:28 +00:00
|
|
|
nodes[count] = current
|
|
|
|
count += 1
|
2016-03-23 04:40:01 +00:00
|
|
|
current = current.Right
|
2015-03-05 13:37:28 +00:00
|
|
|
} else {
|
|
|
|
done = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
func output(node *Node, prefix string, isTail bool, str *string) {
|
|
|
|
if node.Right != nil {
|
2015-03-05 03:15:29 +00:00
|
|
|
newPrefix := prefix
|
|
|
|
if isTail {
|
|
|
|
newPrefix += "│ "
|
|
|
|
} else {
|
|
|
|
newPrefix += " "
|
|
|
|
}
|
2016-03-23 04:40:01 +00:00
|
|
|
output(node.Right, newPrefix, false, str)
|
2015-03-05 03:15:29 +00:00
|
|
|
}
|
|
|
|
*str += prefix
|
|
|
|
if isTail {
|
|
|
|
*str += "└── "
|
|
|
|
} else {
|
|
|
|
*str += "┌── "
|
|
|
|
}
|
|
|
|
*str += node.String() + "\n"
|
2016-03-23 04:40:01 +00:00
|
|
|
if node.Left != nil {
|
2015-03-05 03:15:29 +00:00
|
|
|
newPrefix := prefix
|
|
|
|
if isTail {
|
|
|
|
newPrefix += " "
|
|
|
|
} else {
|
|
|
|
newPrefix += "│ "
|
|
|
|
}
|
2016-03-23 04:40:01 +00:00
|
|
|
output(node.Left, newPrefix, true, str)
|
2015-03-05 03:15:29 +00:00
|
|
|
}
|
2015-03-04 20:13:12 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
func (tree *Tree) lookup(key interface{}) *Node {
|
|
|
|
node := tree.Root
|
2015-03-04 18:38:35 +00:00
|
|
|
for node != nil {
|
2016-03-23 04:40:01 +00:00
|
|
|
compare := tree.comparator(key, node.Key)
|
2015-03-04 18:38:35 +00:00
|
|
|
switch {
|
|
|
|
case compare == 0:
|
|
|
|
return node
|
|
|
|
case compare < 0:
|
2016-03-23 04:40:01 +00:00
|
|
|
node = node.Left
|
2015-03-04 18:38:35 +00:00
|
|
|
case compare > 0:
|
2016-03-23 04:40:01 +00:00
|
|
|
node = node.Right
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
func (node *Node) grandparent() *Node {
|
|
|
|
if node != nil && node.Parent != nil {
|
|
|
|
return node.Parent.Parent
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
func (node *Node) uncle() *Node {
|
|
|
|
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
|
|
|
}
|
2016-03-23 04:40:01 +00:00
|
|
|
return node.Parent.sibling()
|
2015-03-04 19:38:57 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
func (node *Node) sibling() *Node {
|
|
|
|
if node == nil || node.Parent == nil {
|
2015-03-04 19:38:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-03-23 04:40:01 +00:00
|
|
|
if node == node.Parent.Left {
|
|
|
|
return node.Parent.Right
|
2015-03-04 19:38:57 +00:00
|
|
|
} else {
|
2016-03-23 04:40:01 +00:00
|
|
|
return node.Parent.Left
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
func (tree *Tree) rotateLeft(node *Node) {
|
|
|
|
right := node.Right
|
2015-03-04 18:38:35 +00:00
|
|
|
tree.replaceNode(node, right)
|
2016-03-23 04:40:01 +00:00
|
|
|
node.Right = right.Left
|
|
|
|
if right.Left != nil {
|
|
|
|
right.Left.Parent = node
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
2016-03-23 04:40:01 +00:00
|
|
|
right.Left = node
|
|
|
|
node.Parent = right
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
func (tree *Tree) rotateRight(node *Node) {
|
|
|
|
left := node.Left
|
2015-03-04 18:38:35 +00:00
|
|
|
tree.replaceNode(node, left)
|
2016-03-23 04:40:01 +00:00
|
|
|
node.Left = left.Right
|
|
|
|
if left.Right != nil {
|
|
|
|
left.Right.Parent = node
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
2016-03-23 04:40:01 +00:00
|
|
|
left.Right = node
|
|
|
|
node.Parent = left
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
func (tree *Tree) replaceNode(old *Node, new *Node) {
|
|
|
|
if old.Parent == nil {
|
|
|
|
tree.Root = new
|
2015-03-04 18:38:35 +00:00
|
|
|
} else {
|
2016-03-23 04:40:01 +00:00
|
|
|
if old == old.Parent.Left {
|
|
|
|
old.Parent.Left = new
|
2015-03-04 18:38:35 +00:00
|
|
|
} else {
|
2016-03-23 04:40:01 +00:00
|
|
|
old.Parent.Right = new
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if new != nil {
|
2016-03-23 04:40:01 +00:00
|
|
|
new.Parent = old.Parent
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
func (tree *Tree) insertCase1(node *Node) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +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)
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +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 {
|
2016-03-23 04:40:01 +00:00
|
|
|
node.Parent.color = black
|
2015-03-05 04:27:36 +00:00
|
|
|
uncle.color = black
|
|
|
|
node.grandparent().color = red
|
2015-03-04 18:38:35 +00:00
|
|
|
tree.insertCase1(node.grandparent())
|
|
|
|
} else {
|
|
|
|
tree.insertCase4(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
func (tree *Tree) insertCase4(node *Node) {
|
2015-03-05 03:15:29 +00:00
|
|
|
grandparent := node.grandparent()
|
2016-03-23 04:40:01 +00:00
|
|
|
if node == node.Parent.Right && node.Parent == grandparent.Left {
|
|
|
|
tree.rotateLeft(node.Parent)
|
|
|
|
node = node.Left
|
|
|
|
} else if node == node.Parent.Left && node.Parent == grandparent.Right {
|
|
|
|
tree.rotateRight(node.Parent)
|
|
|
|
node = node.Right
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
tree.insertCase5(node)
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +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
|
2016-03-23 04:40:01 +00:00
|
|
|
if node == node.Parent.Left && node.Parent == grandparent.Left {
|
2015-03-05 03:15:29 +00:00
|
|
|
tree.rotateRight(grandparent)
|
2016-03-23 04:40:01 +00:00
|
|
|
} else if node == node.Parent.Right && node.Parent == grandparent.Right {
|
2015-03-05 03:15:29 +00:00
|
|
|
tree.rotateLeft(grandparent)
|
2015-03-04 18:38:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-04 19:20:48 +00:00
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
func (node *Node) maximumNode() *Node {
|
2015-03-04 19:38:57 +00:00
|
|
|
if node == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-23 04:40:01 +00:00
|
|
|
for node.Right != nil {
|
|
|
|
node = node.Right
|
2015-03-04 19:20:48 +00:00
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
func (tree *Tree) deleteCase1(node *Node) {
|
|
|
|
if node.Parent == nil {
|
2015-03-04 19:20:48 +00:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
tree.deleteCase2(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +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 {
|
2016-03-23 04:40:01 +00:00
|
|
|
node.Parent.color = red
|
2015-03-05 04:27:36 +00:00
|
|
|
sibling.color = black
|
2016-03-23 04:40:01 +00:00
|
|
|
if node == node.Parent.Left {
|
|
|
|
tree.rotateLeft(node.Parent)
|
2015-03-04 19:20:48 +00:00
|
|
|
} else {
|
2016-03-23 04:40:01 +00:00
|
|
|
tree.rotateRight(node.Parent)
|
2015-03-04 19:20:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tree.deleteCase3(node)
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
func (tree *Tree) deleteCase3(node *Node) {
|
2015-03-05 03:15:29 +00:00
|
|
|
sibling := node.sibling()
|
2016-03-23 04:40:01 +00:00
|
|
|
if nodeColor(node.Parent) == black &&
|
2015-03-05 04:27:36 +00:00
|
|
|
nodeColor(sibling) == black &&
|
2016-03-23 04:40:01 +00:00
|
|
|
nodeColor(sibling.Left) == black &&
|
|
|
|
nodeColor(sibling.Right) == black {
|
2015-03-05 04:27:36 +00:00
|
|
|
sibling.color = red
|
2016-03-23 04:40:01 +00:00
|
|
|
tree.deleteCase1(node.Parent)
|
2015-03-04 19:20:48 +00:00
|
|
|
} else {
|
|
|
|
tree.deleteCase4(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
func (tree *Tree) deleteCase4(node *Node) {
|
2015-03-05 03:15:29 +00:00
|
|
|
sibling := node.sibling()
|
2016-03-23 04:40:01 +00:00
|
|
|
if nodeColor(node.Parent) == red &&
|
2015-03-05 04:27:36 +00:00
|
|
|
nodeColor(sibling) == black &&
|
2016-03-23 04:40:01 +00:00
|
|
|
nodeColor(sibling.Left) == black &&
|
|
|
|
nodeColor(sibling.Right) == black {
|
2015-03-05 04:27:36 +00:00
|
|
|
sibling.color = red
|
2016-03-23 04:40:01 +00:00
|
|
|
node.Parent.color = black
|
2015-03-04 19:20:48 +00:00
|
|
|
} else {
|
|
|
|
tree.deleteCase5(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
func (tree *Tree) deleteCase5(node *Node) {
|
2015-03-05 03:15:29 +00:00
|
|
|
sibling := node.sibling()
|
2016-03-23 04:40:01 +00:00
|
|
|
if node == node.Parent.Left &&
|
2015-03-05 04:27:36 +00:00
|
|
|
nodeColor(sibling) == black &&
|
2016-03-23 04:40:01 +00:00
|
|
|
nodeColor(sibling.Left) == red &&
|
|
|
|
nodeColor(sibling.Right) == black {
|
2015-03-05 04:27:36 +00:00
|
|
|
sibling.color = red
|
2016-03-23 04:40:01 +00:00
|
|
|
sibling.Left.color = black
|
2015-03-05 03:15:29 +00:00
|
|
|
tree.rotateRight(sibling)
|
2016-03-23 04:40:01 +00:00
|
|
|
} else if node == node.Parent.Right &&
|
2015-03-05 04:27:36 +00:00
|
|
|
nodeColor(sibling) == black &&
|
2016-03-23 04:40:01 +00:00
|
|
|
nodeColor(sibling.Right) == red &&
|
|
|
|
nodeColor(sibling.Left) == black {
|
2015-03-05 04:27:36 +00:00
|
|
|
sibling.color = red
|
2016-03-23 04:40:01 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:40:01 +00:00
|
|
|
func (tree *Tree) deleteCase6(node *Node) {
|
2015-03-05 03:15:29 +00:00
|
|
|
sibling := node.sibling()
|
2016-03-23 04:40:01 +00:00
|
|
|
sibling.color = nodeColor(node.Parent)
|
|
|
|
node.Parent.color = black
|
|
|
|
if node == node.Parent.Left && nodeColor(sibling.Right) == red {
|
|
|
|
sibling.Right.color = black
|
|
|
|
tree.rotateLeft(node.Parent)
|
|
|
|
} else if nodeColor(sibling.Left) == red {
|
|
|
|
sibling.Left.color = black
|
|
|
|
tree.rotateRight(node.Parent)
|
2015-03-04 19:20:48 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-05 03:15:29 +00:00
|
|
|
|
2016-03-23 04:40:01 +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
|
|
|
|
}
|