TODO: GetRange fix bug;
This commit is contained in:
parent
3d1be547ca
commit
2f472ffded
|
@ -612,7 +612,7 @@ func BenchmarkPut(b *testing.B) {
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
execCount := 1000
|
execCount := 50
|
||||||
b.N = len(l) * execCount
|
b.N = len(l) * execCount
|
||||||
for i := 0; i < execCount; i++ {
|
for i := 0; i < execCount; i++ {
|
||||||
tree := New(compare.Int)
|
tree := New(compare.Int)
|
||||||
|
|
|
@ -422,38 +422,49 @@ func (tree *Tree) Put(key, value interface{}) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cur := tree.root
|
for cur := tree.root; ; {
|
||||||
parent := cur.parent
|
|
||||||
child := -1
|
|
||||||
|
|
||||||
for {
|
|
||||||
|
|
||||||
if cur == nil {
|
|
||||||
parent.children[child] = node
|
|
||||||
node.parent = parent
|
|
||||||
|
|
||||||
fixed := parent.parent
|
|
||||||
fsize := getSize(fixed)
|
|
||||||
if fsize == 3 {
|
|
||||||
lefts, rigths := getChildrenSize(fixed)
|
|
||||||
tree.fix3Size(fixed, lefts, rigths)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if cur.size > 8 {
|
if cur.size > 8 {
|
||||||
ls, rs := cur.children[0].size, cur.children[1].size
|
|
||||||
factor := cur.size / 10 // or factor = 1
|
factor := cur.size / 10 // or factor = 1
|
||||||
if rs >= ls*2+factor || ls >= rs*2+factor {
|
if cur.children[1].size >= cur.children[0].size*2+factor || cur.children[0].size >= cur.children[1].size*2+factor {
|
||||||
tree.fixSize(cur, ls, rs)
|
tree.fixSize(cur)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cur.size++
|
cur.size++
|
||||||
parent = cur
|
|
||||||
c := tree.Compare(key, cur.key)
|
c := tree.Compare(key, cur.key)
|
||||||
child = (c + 2) / 2
|
if c < 0 {
|
||||||
cur = cur.children[child]
|
if cur.children[0] == nil {
|
||||||
|
cur.children[0] = node
|
||||||
|
node.parent = cur
|
||||||
|
|
||||||
|
if cur.parent != nil && cur.parent.size == 3 {
|
||||||
|
if cur.parent.children[0] == nil {
|
||||||
|
tree.lrrotate3(cur.parent)
|
||||||
|
} else {
|
||||||
|
tree.rrotate3(cur.parent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cur = cur.children[0]
|
||||||
|
} else {
|
||||||
|
if cur.children[1] == nil {
|
||||||
|
cur.children[1] = node
|
||||||
|
node.parent = cur
|
||||||
|
|
||||||
|
if cur.parent != nil && cur.parent.size == 3 {
|
||||||
|
if cur.parent.children[1] == nil {
|
||||||
|
tree.rlrotate3(cur.parent)
|
||||||
|
} else {
|
||||||
|
tree.lrotate3(cur.parent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cur = cur.children[1]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -756,6 +767,7 @@ func (tree *Tree) rrotate(cur *Node) {
|
||||||
|
|
||||||
// mov.children[l]不可能为nil
|
// mov.children[l]不可能为nil
|
||||||
mov.children[l].parent = cur
|
mov.children[l].parent = cur
|
||||||
|
|
||||||
cur.children[l] = mov.children[l]
|
cur.children[l] = mov.children[l]
|
||||||
|
|
||||||
// 解决mov节点孩子转移的问题
|
// 解决mov节点孩子转移的问题
|
||||||
|
@ -809,6 +821,7 @@ func (tree *Tree) lrotate(cur *Node) {
|
||||||
|
|
||||||
// mov.children[l]不可能为nil
|
// mov.children[l]不可能为nil
|
||||||
mov.children[l].parent = cur
|
mov.children[l].parent = cur
|
||||||
|
|
||||||
cur.children[l] = mov.children[l]
|
cur.children[l] = mov.children[l]
|
||||||
|
|
||||||
// 解决mov节点孩子转移的问题
|
// 解决mov节点孩子转移的问题
|
||||||
|
@ -851,10 +864,9 @@ func (tree *Tree) fixSizeWithRemove(cur *Node) {
|
||||||
for cur != nil {
|
for cur != nil {
|
||||||
cur.size--
|
cur.size--
|
||||||
if cur.size > 8 {
|
if cur.size > 8 {
|
||||||
ls, rs := getChildrenSize(cur)
|
|
||||||
factor := cur.size / 10 // or factor = 1
|
factor := cur.size / 10 // or factor = 1
|
||||||
if rs >= ls*2+factor || ls >= rs*2+factor {
|
if cur.children[1].size >= cur.children[0].size*2+factor || cur.children[0].size >= cur.children[1].size*2+factor {
|
||||||
tree.fixSize(cur, ls, rs)
|
tree.fixSize(cur)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cur = cur.parent
|
cur = cur.parent
|
||||||
|
@ -881,7 +893,7 @@ func (tree *Tree) fix3Size(cur *Node, lefts, rigths int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tree *Tree) fixSize(cur *Node, lefts, rigths int) {
|
func (tree *Tree) fix6Size(cur *Node, lefts, rigths int) {
|
||||||
if lefts > rigths {
|
if lefts > rigths {
|
||||||
l := cur.children[0]
|
l := cur.children[0]
|
||||||
llsize, lrsize := getChildrenSize(l)
|
llsize, lrsize := getChildrenSize(l)
|
||||||
|
@ -901,6 +913,24 @@ func (tree *Tree) fixSize(cur *Node, lefts, rigths int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tree *Tree) fixSize(cur *Node) {
|
||||||
|
if cur.children[0].size > cur.children[1].size {
|
||||||
|
llsize, lrsize := getChildrenSize(cur.children[0])
|
||||||
|
if lrsize > llsize {
|
||||||
|
tree.rlrotate(cur)
|
||||||
|
} else {
|
||||||
|
tree.rrotate(cur)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
rlsize, rrsize := getChildrenSize(cur.children[1])
|
||||||
|
if rlsize > rrsize {
|
||||||
|
tree.lrrotate(cur)
|
||||||
|
} else {
|
||||||
|
tree.lrotate(cur)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func output(node *Node, prefix string, isTail bool, str *string) {
|
func output(node *Node, prefix string, isTail bool, str *string) {
|
||||||
|
|
||||||
if node.children[1] != nil {
|
if node.children[1] != nil {
|
||||||
|
@ -956,10 +986,10 @@ func outputfordebug(node *Node, prefix string, isTail bool, str *string) {
|
||||||
if node.parent == nil {
|
if node.parent == nil {
|
||||||
parentv = "nil"
|
parentv = "nil"
|
||||||
} else {
|
} else {
|
||||||
parentv = spew.Sprint(node.key) + ":" + spew.Sprint(node.parent.value)
|
parentv = spew.Sprint(node.parent.key)
|
||||||
}
|
}
|
||||||
suffix += parentv + "|" + spew.Sprint(node.size) + ")"
|
suffix += parentv + "|" + spew.Sprint(node.size) + ")"
|
||||||
*str += spew.Sprint(node.key) + ":" + spew.Sprint(node.value) + suffix + "\n"
|
*str += spew.Sprint(node.key) + suffix + "\n"
|
||||||
|
|
||||||
if node.children[0] != nil {
|
if node.children[0] != nil {
|
||||||
newPrefix := prefix
|
newPrefix := prefix
|
||||||
|
|
|
@ -625,16 +625,18 @@ func TestPutStable(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkGodsRBPut(b *testing.B) {
|
func BenchmarkGodsRBPut(b *testing.B) {
|
||||||
tree := redblacktree.NewWithIntComparator()
|
|
||||||
|
|
||||||
l := loadTestData()
|
l := loadTestData()
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
b.N = len(l)
|
execCount := 50
|
||||||
for _, v := range l {
|
b.N = len(l) * execCount
|
||||||
tree.Put(v, v)
|
for i := 0; i < execCount; i++ {
|
||||||
|
tree := redblacktree.NewWithIntComparator()
|
||||||
|
for _, v := range l {
|
||||||
|
tree.Put(v, v)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user