@ -49,18 +49,18 @@ const (
)
)
type Tree struct {
type Tree struct {
root * n ode
Root * N ode
size int
size int
comparator utils . Comparator
comparator utils . Comparator
}
}
type n ode struct {
type N ode struct {
k ey interface { }
K ey interface { }
v alue interface { }
V alue interface { }
color color
color color
left * n ode
Left * N ode
right * n ode
Right * N ode
parent * n ode
Parent * N ode
}
}
// Instantiates a red-black tree with the custom comparator.
// Instantiates a red-black tree with the custom comparator.
@ -81,35 +81,35 @@ func NewWithStringComparator() *Tree {
// Inserts node into the tree.
// Inserts node into the tree.
// Key should adhere to the comparator's type assertion, otherwise method panics.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func ( tree * Tree ) Put ( key interface { } , value interface { } ) {
func ( tree * Tree ) Put ( key interface { } , value interface { } ) {
insertedNode := & node{ key : key , v alue: value , color : red }
insertedNode := & Node{ Key : key , V alue: value , color : red }
if tree . r oot == nil {
if tree . R oot == nil {
tree . r oot = insertedNode
tree . R oot = insertedNode
} else {
} else {
node := tree . r oot
node := tree . R oot
loop := true
loop := true
for loop {
for loop {
compare := tree . comparator ( key , node . k ey)
compare := tree . comparator ( key , node . K ey)
switch {
switch {
case compare == 0 :
case compare == 0 :
node . v alue = value
node . V alue = value
return
return
case compare < 0 :
case compare < 0 :
if node . l eft == nil {
if node . L eft == nil {
node . l eft = insertedNode
node . L eft = insertedNode
loop = false
loop = false
} else {
} else {
node = node . l eft
node = node . L eft
}
}
case compare > 0 :
case compare > 0 :
if node . r ight == nil {
if node . R ight == nil {
node . r ight = insertedNode
node . R ight = insertedNode
loop = false
loop = false
} else {
} else {
node = node . r ight
node = node . R ight
}
}
}
}
}
}
insertedNode . p arent = node
insertedNode . P arent = node
}
}
tree . insertCase1 ( insertedNode )
tree . insertCase1 ( insertedNode )
tree . size += 1
tree . size += 1
@ -121,7 +121,7 @@ func (tree *Tree) Put(key interface{}, value interface{}) {
func ( tree * Tree ) Get ( key interface { } ) ( value interface { } , found bool ) {
func ( tree * Tree ) Get ( key interface { } ) ( value interface { } , found bool ) {
node := tree . lookup ( key )
node := tree . lookup ( key )
if node != nil {
if node != nil {
return node . v alue, true
return node . V alue, true
}
}
return nil , false
return nil , false
}
}
@ -129,29 +129,29 @@ func (tree *Tree) Get(key interface{}) (value interface{}, found bool) {
// Remove the node from the tree by key.
// Remove the node from the tree by key.
// Key should adhere to the comparator's type assertion, otherwise method panics.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func ( tree * Tree ) Remove ( key interface { } ) {
func ( tree * Tree ) Remove ( key interface { } ) {
var child * n ode
var child * N ode
node := tree . lookup ( key )
node := tree . lookup ( key )
if node == nil {
if node == nil {
return
return
}
}
if node . left != nil && node . r ight != nil {
if node . Left != nil && node . R ight != nil {
pred := node . l eft. maximumNode ( )
pred := node . L eft. maximumNode ( )
node . key = pred . k ey
node . Key = pred . K ey
node . value = pred . v alue
node . Value = pred . V alue
node = pred
node = pred
}
}
if node . left == nil || node . r ight == nil {
if node . Left == nil || node . R ight == nil {
if node . r ight == nil {
if node . R ight == nil {
child = node . l eft
child = node . L eft
} else {
} else {
child = node . r ight
child = node . R ight
}
}
if node . color == black {
if node . color == black {
node . color = nodeColor ( child )
node . color = nodeColor ( child )
tree . deleteCase1 ( node )
tree . deleteCase1 ( node )
}
}
tree . replaceNode ( node , child )
tree . replaceNode ( node , child )
if node . p arent == nil && child != nil {
if node . P arent == nil && child != nil {
child . color = black
child . color = black
}
}
}
}
@ -172,7 +172,7 @@ func (tree *Tree) Size() int {
func ( tree * Tree ) Keys ( ) [ ] interface { } {
func ( tree * Tree ) Keys ( ) [ ] interface { } {
keys := make ( [ ] interface { } , tree . size )
keys := make ( [ ] interface { } , tree . size )
for i , node := range tree . inOrder ( ) {
for i , node := range tree . inOrder ( ) {
keys [ i ] = node . k ey
keys [ i ] = node . K ey
}
}
return keys
return keys
}
}
@ -181,48 +181,48 @@ func (tree *Tree) Keys() []interface{} {
func ( tree * Tree ) Values ( ) [ ] interface { } {
func ( tree * Tree ) Values ( ) [ ] interface { } {
values := make ( [ ] interface { } , tree . size )
values := make ( [ ] interface { } , tree . size )
for i , node := range tree . inOrder ( ) {
for i , node := range tree . inOrder ( ) {
values [ i ] = node . v alue
values [ i ] = node . V alue
}
}
return values
return values
}
}
// Removes all nodes from the tree.
// Removes all nodes from the tree.
func ( tree * Tree ) Clear ( ) {
func ( tree * Tree ) Clear ( ) {
tree . r oot = nil
tree . R oot = nil
tree . size = 0
tree . size = 0
}
}
func ( tree * Tree ) String ( ) string {
func ( tree * Tree ) String ( ) string {
str := "RedBlackTree\n"
str := "RedBlackTree\n"
if ! tree . Empty ( ) {
if ! tree . Empty ( ) {
output ( tree . r oot, "" , true , & str )
output ( tree . R oot, "" , true , & str )
}
}
return str
return str
}
}
func ( node * n ode) String ( ) string {
func ( node * N ode) String ( ) string {
return fmt . Sprintf ( "%v" , node . k ey)
return fmt . Sprintf ( "%v" , node . K ey)
}
}
// Returns all nodes in order
// Returns all nodes in order
func ( tree * Tree ) inOrder ( ) [ ] * n ode {
func ( tree * Tree ) inOrder ( ) [ ] * N ode {
nodes := make ( [ ] * n ode, tree . size )
nodes := make ( [ ] * N ode, tree . size )
if tree . size > 0 {
if tree . size > 0 {
current := tree . r oot
current := tree . R oot
stack := linkedliststack . New ( )
stack := linkedliststack . New ( )
done := false
done := false
count := 0
count := 0
for ! done {
for ! done {
if current != nil {
if current != nil {
stack . Push ( current )
stack . Push ( current )
current = current . l eft
current = current . L eft
} else {
} else {
if ! stack . Empty ( ) {
if ! stack . Empty ( ) {
currentPop , _ := stack . Pop ( )
currentPop , _ := stack . Pop ( )
current = currentPop . ( * n ode)
current = currentPop . ( * N ode)
nodes [ count ] = current
nodes [ count ] = current
count += 1
count += 1
current = current . r ight
current = current . R ight
} else {
} else {
done = true
done = true
}
}
@ -232,15 +232,15 @@ func (tree *Tree) inOrder() []*node {
return nodes
return nodes
}
}
func output ( node * n ode, prefix string , isTail bool , str * string ) {
func output ( node * N ode, prefix string , isTail bool , str * string ) {
if node . r ight != nil {
if node . R ight != nil {
newPrefix := prefix
newPrefix := prefix
if isTail {
if isTail {
newPrefix += "│ "
newPrefix += "│ "
} else {
} else {
newPrefix += " "
newPrefix += " "
}
}
output ( node . r ight, newPrefix , false , str )
output ( node . R ight, newPrefix , false , str )
}
}
* str += prefix
* str += prefix
if isTail {
if isTail {
@ -249,114 +249,114 @@ func output(node *node, prefix string, isTail bool, str *string) {
* str += "┌── "
* str += "┌── "
}
}
* str += node . String ( ) + "\n"
* str += node . String ( ) + "\n"
if node . l eft != nil {
if node . L eft != nil {
newPrefix := prefix
newPrefix := prefix
if isTail {
if isTail {
newPrefix += " "
newPrefix += " "
} else {
} else {
newPrefix += "│ "
newPrefix += "│ "
}
}
output ( node . l eft, newPrefix , true , str )
output ( node . L eft, newPrefix , true , str )
}
}
}
}
func ( tree * Tree ) lookup ( key interface { } ) * n ode {
func ( tree * Tree ) lookup ( key interface { } ) * N ode {
node := tree . r oot
node := tree . R oot
for node != nil {
for node != nil {
compare := tree . comparator ( key , node . k ey)
compare := tree . comparator ( key , node . K ey)
switch {
switch {
case compare == 0 :
case compare == 0 :
return node
return node
case compare < 0 :
case compare < 0 :
node = node . l eft
node = node . L eft
case compare > 0 :
case compare > 0 :
node = node . r ight
node = node . R ight
}
}
}
}
return nil
return nil
}
}
func ( node * node) grandparent ( ) * n ode {
func ( node * Node) grandparent ( ) * N ode {
if node != nil && node . p arent != nil {
if node != nil && node . P arent != nil {
return node . parent. p arent
return node . Parent. P arent
}
}
return nil
return nil
}
}
func ( node * node) uncle ( ) * n ode {
func ( node * Node) uncle ( ) * N ode {
if node == nil || node . parent == nil || node . parent . p arent == nil {
if node == nil || node . Parent == nil || node . Parent . P arent == nil {
return nil
return nil
}
}
return node . p arent. sibling ( )
return node . P arent. sibling ( )
}
}
func ( node * node) sibling ( ) * n ode {
func ( node * Node) sibling ( ) * N ode {
if node == nil || node . p arent == nil {
if node == nil || node . P arent == nil {
return nil
return nil
}
}
if node == node . parent. l eft {
if node == node . Parent. L eft {
return node . parent. r ight
return node . Parent. R ight
} else {
} else {
return node . parent. l eft
return node . Parent. L eft
}
}
}
}
func ( tree * Tree ) rotateLeft ( node * n ode) {
func ( tree * Tree ) rotateLeft ( node * N ode) {
right := node . r ight
right := node . R ight
tree . replaceNode ( node , right )
tree . replaceNode ( node , right )
node . right = right . l eft
node . Right = right . L eft
if right . l eft != nil {
if right . L eft != nil {
right . left. p arent = node
right . Left. P arent = node
}
}
right . l eft = node
right . L eft = node
node . p arent = right
node . P arent = right
}
}
func ( tree * Tree ) rotateRight ( node * n ode) {
func ( tree * Tree ) rotateRight ( node * N ode) {
left := node . l eft
left := node . L eft
tree . replaceNode ( node , left )
tree . replaceNode ( node , left )
node . left = left . r ight
node . Left = left . R ight
if left . r ight != nil {
if left . R ight != nil {
left . right. p arent = node
left . Right. P arent = node
}
}
left . r ight = node
left . R ight = node
node . p arent = left
node . P arent = left
}
}
func ( tree * Tree ) replaceNode ( old * node, new * n ode) {
func ( tree * Tree ) replaceNode ( old * Node, new * N ode) {
if old . p arent == nil {
if old . P arent == nil {
tree . r oot = new
tree . R oot = new
} else {
} else {
if old == old . parent. l eft {
if old == old . Parent. L eft {
old . parent. l eft = new
old . Parent. L eft = new
} else {
} else {
old . parent. r ight = new
old . Parent. R ight = new
}
}
}
}
if new != nil {
if new != nil {
new . parent = old . p arent
new . Parent = old . P arent
}
}
}
}
func ( tree * Tree ) insertCase1 ( node * n ode) {
func ( tree * Tree ) insertCase1 ( node * N ode) {
if node . p arent == nil {
if node . P arent == nil {
node . color = black
node . color = black
} else {
} else {
tree . insertCase2 ( node )
tree . insertCase2 ( node )
}
}
}
}
func ( tree * Tree ) insertCase2 ( node * n ode) {
func ( tree * Tree ) insertCase2 ( node * N ode) {
if nodeColor ( node . p arent) == black {
if nodeColor ( node . P arent) == black {
return
return
}
}
tree . insertCase3 ( node )
tree . insertCase3 ( node )
}
}
func ( tree * Tree ) insertCase3 ( node * n ode) {
func ( tree * Tree ) insertCase3 ( node * N ode) {
uncle := node . uncle ( )
uncle := node . uncle ( )
if nodeColor ( uncle ) == red {
if nodeColor ( uncle ) == red {
node . p arent. color = black
node . P arent. color = black
uncle . color = black
uncle . color = black
node . grandparent ( ) . color = red
node . grandparent ( ) . color = red
tree . insertCase1 ( node . grandparent ( ) )
tree . insertCase1 ( node . grandparent ( ) )
@ -365,121 +365,121 @@ func (tree *Tree) insertCase3(node *node) {
}
}
}
}
func ( tree * Tree ) insertCase4 ( node * n ode) {
func ( tree * Tree ) insertCase4 ( node * N ode) {
grandparent := node . grandparent ( )
grandparent := node . grandparent ( )
if node == node . parent. right && node . parent == grandparent . l eft {
if node == node . Parent. Right && node . Parent == grandparent . L eft {
tree . rotateLeft ( node . p arent)
tree . rotateLeft ( node . P arent)
node = node . l eft
node = node . L eft
} else if node == node . parent. left && node . parent == grandparent . r ight {
} else if node == node . Parent. Left && node . Parent == grandparent . R ight {
tree . rotateRight ( node . p arent)
tree . rotateRight ( node . P arent)
node = node . r ight
node = node . R ight
}
}
tree . insertCase5 ( node )
tree . insertCase5 ( node )
}
}
func ( tree * Tree ) insertCase5 ( node * n ode) {
func ( tree * Tree ) insertCase5 ( node * N ode) {
node . p arent. color = black
node . P arent. color = black
grandparent := node . grandparent ( )
grandparent := node . grandparent ( )
grandparent . color = red
grandparent . color = red
if node == node . parent. left && node . parent == grandparent . l eft {
if node == node . Parent. Left && node . Parent == grandparent . L eft {
tree . rotateRight ( grandparent )
tree . rotateRight ( grandparent )
} else if node == node . parent. right && node . parent == grandparent . r ight {
} else if node == node . Parent. Right && node . Parent == grandparent . R ight {
tree . rotateLeft ( grandparent )
tree . rotateLeft ( grandparent )
}
}
}
}
func ( node * node) maximumNode ( ) * n ode {
func ( node * Node) maximumNode ( ) * N ode {
if node == nil {
if node == nil {
return nil
return nil
}
}
for node . r ight != nil {
for node . R ight != nil {
node = node . r ight
node = node . R ight
}
}
return node
return node
}
}
func ( tree * Tree ) deleteCase1 ( node * n ode) {
func ( tree * Tree ) deleteCase1 ( node * N ode) {
if node . p arent == nil {
if node . P arent == nil {
return
return
} else {
} else {
tree . deleteCase2 ( node )
tree . deleteCase2 ( node )
}
}
}
}
func ( tree * Tree ) deleteCase2 ( node * n ode) {
func ( tree * Tree ) deleteCase2 ( node * N ode) {
sibling := node . sibling ( )
sibling := node . sibling ( )
if nodeColor ( sibling ) == red {
if nodeColor ( sibling ) == red {
node . p arent. color = red
node . P arent. color = red
sibling . color = black
sibling . color = black
if node == node . parent. l eft {
if node == node . Parent. L eft {
tree . rotateLeft ( node . p arent)
tree . rotateLeft ( node . P arent)
} else {
} else {
tree . rotateRight ( node . p arent)
tree . rotateRight ( node . P arent)
}
}
}
}
tree . deleteCase3 ( node )
tree . deleteCase3 ( node )
}
}
func ( tree * Tree ) deleteCase3 ( node * n ode) {
func ( tree * Tree ) deleteCase3 ( node * N ode) {
sibling := node . sibling ( )
sibling := node . sibling ( )
if nodeColor ( node . p arent) == black &&
if nodeColor ( node . P arent) == black &&
nodeColor ( sibling ) == black &&
nodeColor ( sibling ) == black &&
nodeColor ( sibling . l eft) == black &&
nodeColor ( sibling . L eft) == black &&
nodeColor ( sibling . r ight) == black {
nodeColor ( sibling . R ight) == black {
sibling . color = red
sibling . color = red
tree . deleteCase1 ( node . p arent)
tree . deleteCase1 ( node . P arent)
} else {
} else {
tree . deleteCase4 ( node )
tree . deleteCase4 ( node )
}
}
}
}
func ( tree * Tree ) deleteCase4 ( node * n ode) {
func ( tree * Tree ) deleteCase4 ( node * N ode) {
sibling := node . sibling ( )
sibling := node . sibling ( )
if nodeColor ( node . p arent) == red &&
if nodeColor ( node . P arent) == red &&
nodeColor ( sibling ) == black &&
nodeColor ( sibling ) == black &&
nodeColor ( sibling . l eft) == black &&
nodeColor ( sibling . L eft) == black &&
nodeColor ( sibling . r ight) == black {
nodeColor ( sibling . R ight) == black {
sibling . color = red
sibling . color = red
node . p arent. color = black
node . P arent. color = black
} else {
} else {
tree . deleteCase5 ( node )
tree . deleteCase5 ( node )
}
}
}
}
func ( tree * Tree ) deleteCase5 ( node * n ode) {
func ( tree * Tree ) deleteCase5 ( node * N ode) {
sibling := node . sibling ( )
sibling := node . sibling ( )
if node == node . parent. l eft &&
if node == node . Parent. L eft &&
nodeColor ( sibling ) == black &&
nodeColor ( sibling ) == black &&
nodeColor ( sibling . l eft) == red &&
nodeColor ( sibling . L eft) == red &&
nodeColor ( sibling . r ight) == black {
nodeColor ( sibling . R ight) == black {
sibling . color = red
sibling . color = red
sibling . l eft. color = black
sibling . L eft. color = black
tree . rotateRight ( sibling )
tree . rotateRight ( sibling )
} else if node == node . parent. r ight &&
} else if node == node . Parent. R ight &&
nodeColor ( sibling ) == black &&
nodeColor ( sibling ) == black &&
nodeColor ( sibling . r ight) == red &&
nodeColor ( sibling . R ight) == red &&
nodeColor ( sibling . l eft) == black {
nodeColor ( sibling . L eft) == black {
sibling . color = red
sibling . color = red
sibling . r ight. color = black
sibling . R ight. color = black
tree . rotateLeft ( sibling )
tree . rotateLeft ( sibling )
}
}
tree . deleteCase6 ( node )
tree . deleteCase6 ( node )
}
}
func ( tree * Tree ) deleteCase6 ( node * n ode) {
func ( tree * Tree ) deleteCase6 ( node * N ode) {
sibling := node . sibling ( )
sibling := node . sibling ( )
sibling . color = nodeColor ( node . p arent)
sibling . color = nodeColor ( node . P arent)
node . p arent. color = black
node . P arent. color = black
if node == node . parent. left && nodeColor ( sibling . r ight) == red {
if node == node . Parent. Left && nodeColor ( sibling . R ight) == red {
sibling . r ight. color = black
sibling . R ight. color = black
tree . rotateLeft ( node . p arent)
tree . rotateLeft ( node . P arent)
} else if nodeColor ( sibling . l eft) == red {
} else if nodeColor ( sibling . L eft) == red {
sibling . l eft. color = black
sibling . L eft. color = black
tree . rotateRight ( node . p arent)
tree . rotateRight ( node . P arent)
}
}
}
}
func nodeColor ( node * n ode) color {
func nodeColor ( node * N ode) color {
if node == nil {
if node == nil {
return black
return black
}
}