- AVL tree, remove dynamic func initialization within a func, simply extract those put/remove func on its own

pull/51/head
Emir Pasic 9 years ago
parent d6611c11d3
commit 9f8722300a

@ -5,6 +5,8 @@
// Package avltree implements an AVL balanced binary tree. // Package avltree implements an AVL balanced binary tree.
// //
// Structure is not thread safe. // Structure is not thread safe.
//
// References: https://en.wikipedia.org/wiki/AVL_tree
package avltree package avltree
import ( import (
@ -144,80 +146,78 @@ func (t *Tree) Ceiling(key interface{}) (floor *Node, found bool) {
// Put inserts node into the tree. // Put 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 (t *Tree) Put(key interface{}, value interface{}) { func (t *Tree) Put(key interface{}, value interface{}) {
var put func(*Node, **Node) bool t.put(key, value, nil, &t.Root)
put = func(p *Node, qp **Node) bool { }
q := *qp
if q == nil {
t.size++
*qp = &Node{Key: key, Value: value, Parent: p}
return true
}
c := t.Comparator(key, q.Key) func (t *Tree) put(key interface{}, value interface{}, p *Node, qp **Node) bool {
if c == 0 { q := *qp
q.Key = key if q == nil {
q.Value = value t.size++
return false *qp = &Node{Key: key, Value: value, Parent: p}
} return true
}
if c < 0 { c := t.Comparator(key, q.Key)
c = -1 if c == 0 {
} else { q.Key = key
c = 1 q.Value = value
}
a := (c + 1) / 2
var fix bool
fix = put(q, &q.Children[a])
if fix {
return putFix(int8(c), qp)
}
return false return false
} }
put(nil, &t.Root) if c < 0 {
c = -1
} else {
c = 1
}
a := (c + 1) / 2
var fix bool
fix = t.put(key, value, q, &q.Children[a])
if fix {
return putFix(int8(c), qp)
}
return false
} }
// Remove remove the node from the tree by key. // Remove 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 (t *Tree) Remove(key interface{}) { func (t *Tree) Remove(key interface{}) {
var remove func(**Node) bool t.remove(key, &t.Root)
remove = func(qp **Node) bool { }
q := *qp
if q == nil {
return false
}
c := t.Comparator(key, q.Key) func (t *Tree) remove(key interface{}, qp **Node) bool {
if c == 0 { q := *qp
t.size-- if q == nil {
if q.Children[1] == nil { return false
if q.Children[0] != nil { }
q.Children[0].Parent = q.Parent
}
*qp = q.Children[0]
return true
}
fix := removeMin(&q.Children[1], &q.Key, &q.Value)
if fix {
return removeFix(-1, qp)
}
return false
}
if c < 0 { c := t.Comparator(key, q.Key)
c = -1 if c == 0 {
} else { t.size--
c = 1 if q.Children[1] == nil {
if q.Children[0] != nil {
q.Children[0].Parent = q.Parent
}
*qp = q.Children[0]
return true
} }
a := (c + 1) / 2 fix := removeMin(&q.Children[1], &q.Key, &q.Value)
fix := remove(&q.Children[a])
if fix { if fix {
return removeFix(int8(-c), qp) return removeFix(-1, qp)
} }
return false return false
} }
remove(&t.Root) if c < 0 {
c = -1
} else {
c = 1
}
a := (c + 1) / 2
fix := t.remove(key, &q.Children[a])
if fix {
return removeFix(int8(-c), qp)
}
return false
} }
func removeMin(qp **Node, minKey *interface{}, minVal *interface{}) bool { func removeMin(qp **Node, minKey *interface{}, minVal *interface{}) bool {

Loading…
Cancel
Save