完善了旋转

This commit is contained in:
eson 2019-03-29 00:50:55 +08:00
parent 45ef1aca6c
commit 750ed93b74
4 changed files with 103 additions and 71 deletions

View File

@ -1,8 +1,6 @@
package pqueue package pqueue
import ( import (
"log"
"474420502.top/eson/structure/compare" "474420502.top/eson/structure/compare"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
) )
@ -600,24 +598,33 @@ func (tree *vbTree) Traversal(every func(v interface{}) bool, traversalMethod ..
func (tree *vbTree) lrrotate3(cur *tNode) { func (tree *vbTree) lrrotate3(cur *tNode) {
const l = 1 const l = 1
const r = 0 const r = 0
// 1 right 0 left
ln := cur.children[l]
lrn := ln.children[r]
movparent := cur.children[l] if cur.parent != nil {
mov := movparent.children[r] if cur.parent.children[l] == cur {
cur.parent.children[l] = lrn
} else {
cur.parent.children[r] = lrn
}
} else {
tree.root = lrn
}
lrn.parent = cur.parent
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移 lrn.children[l] = ln
lrn.children[l].parent = lrn
cur.children[r] = mov lrn.children[r] = cur
mov.parent = cur lrn.children[r].parent = lrn
cur.children[l] = movparent cur.children[l] = nil
movparent.children[r] = nil ln.children[r] = nil
cur.children[r] = mov lrn.size = 3
mov.parent = cur ln.size = 1
cur.size = 1
// cur.size = 3
// cur.children[r].size = 1
cur.children[l].size = 1
} }
func (tree *vbTree) lrrotate(cur *tNode) *tNode { func (tree *vbTree) lrrotate(cur *tNode) *tNode {
@ -632,7 +639,9 @@ func (tree *vbTree) lrrotate(cur *tNode) *tNode {
lrrn := lrn.children[r] lrrn := lrn.children[r]
ln.children[r] = lrln ln.children[r] = lrln
if lrln != nil {
ln.children[r].parent = ln ln.children[r].parent = ln
}
if cur.parent != nil { if cur.parent != nil {
if cur.parent.children[l] == cur { if cur.parent.children[l] == cur {
@ -652,7 +661,9 @@ func (tree *vbTree) lrrotate(cur *tNode) *tNode {
lrn.children[r].parent = lrn lrn.children[r].parent = lrn
cur.children[l] = lrrn cur.children[l] = lrrn
if lrrn != nil {
cur.children[l].parent = cur cur.children[l].parent = cur
}
ln.size = getChildrenSumSize(ln) + 1 ln.size = getChildrenSumSize(ln) + 1
cur.size = getChildrenSumSize(cur) + 1 cur.size = getChildrenSumSize(cur) + 1
@ -664,24 +675,33 @@ func (tree *vbTree) lrrotate(cur *tNode) *tNode {
func (tree *vbTree) rlrotate3(cur *tNode) { func (tree *vbTree) rlrotate3(cur *tNode) {
const l = 0 const l = 0
const r = 1 const r = 1
// 1 right 0 left
ln := cur.children[l]
lrn := ln.children[r]
movparent := cur.children[l] if cur.parent != nil {
mov := movparent.children[r] if cur.parent.children[l] == cur {
cur.parent.children[l] = lrn
} else {
cur.parent.children[r] = lrn
}
} else {
tree.root = lrn
}
lrn.parent = cur.parent
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移 lrn.children[l] = ln
lrn.children[l].parent = lrn
cur.children[r] = mov lrn.children[r] = cur
mov.parent = cur lrn.children[r].parent = lrn
cur.children[l] = movparent cur.children[l] = nil
movparent.children[r] = nil ln.children[r] = nil
cur.children[r] = mov lrn.size = 3
mov.parent = cur ln.size = 1
cur.size = 1
// cur.size = 3
// cur.children[r].size = 1
cur.children[l].size = 1
} }
func (tree *vbTree) rlrotate(cur *tNode) *tNode { func (tree *vbTree) rlrotate(cur *tNode) *tNode {
@ -696,7 +716,9 @@ func (tree *vbTree) rlrotate(cur *tNode) *tNode {
lrrn := lrn.children[r] lrrn := lrn.children[r]
ln.children[r] = lrln ln.children[r] = lrln
if lrln != nil {
ln.children[r].parent = ln ln.children[r].parent = ln
}
if cur.parent != nil { if cur.parent != nil {
if cur.parent.children[l] == cur { if cur.parent.children[l] == cur {
@ -716,7 +738,9 @@ func (tree *vbTree) rlrotate(cur *tNode) *tNode {
lrn.children[r].parent = lrn lrn.children[r].parent = lrn
cur.children[l] = lrrn cur.children[l] = lrrn
if lrrn != nil {
cur.children[l].parent = cur cur.children[l].parent = cur
}
ln.size = getChildrenSumSize(ln) + 1 ln.size = getChildrenSumSize(ln) + 1
cur.size = getChildrenSumSize(cur) + 1 cur.size = getChildrenSumSize(cur) + 1
@ -729,19 +753,26 @@ func (tree *vbTree) rrotate3(cur *tNode) {
const l = 0 const l = 0
const r = 1 const r = 1
// 1 right 0 left // 1 right 0 left
mov := cur.children[l] ln := cur.children[l]
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移 if cur.parent != nil {
if cur.parent.children[l] == cur {
cur.parent.children[l] = ln
} else {
cur.parent.children[r] = ln
}
} else {
tree.root = ln
}
ln.parent = cur.parent
cur.children[r] = mov ln.children[r] = cur
mov.size = 1 cur.parent = ln
cur.children[l] = mov.children[l] cur.children[l] = nil
cur.children[l].parent = cur
mov.children[l] = nil ln.size = 3
cur.size = 1
mov.size = 1
} }
func (tree *vbTree) rrotate(cur *tNode) *tNode { func (tree *vbTree) rrotate(cur *tNode) *tNode {
@ -781,19 +812,26 @@ func (tree *vbTree) lrotate3(cur *tNode) {
const l = 1 const l = 1
const r = 0 const r = 0
// 1 right 0 left // 1 right 0 left
mov := cur.children[l] ln := cur.children[l]
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移 if cur.parent != nil {
if cur.parent.children[l] == cur {
cur.parent.children[l] = ln
} else {
cur.parent.children[r] = ln
}
} else {
tree.root = ln
}
ln.parent = cur.parent
cur.children[r] = mov ln.children[r] = cur
mov.size = 1 cur.parent = ln
cur.children[l] = mov.children[l] cur.children[l] = nil
cur.children[l].parent = cur
mov.children[l] = nil ln.size = 3
cur.size = 1
mov.size = 1
} }
func (tree *vbTree) lrotate(cur *tNode) *tNode { func (tree *vbTree) lrotate(cur *tNode) *tNode {
@ -883,20 +921,16 @@ func (tree *vbTree) fixSize(cur *tNode, lefts, rigths int) *tNode {
l := cur.children[0] l := cur.children[0]
llsize, lrsize := getChildrenSize(l) llsize, lrsize := getChildrenSize(l)
if lrsize > llsize { if lrsize > llsize {
log.Println("rlrotate")
return tree.rlrotate(cur) return tree.rlrotate(cur)
} else { } else {
log.Println("rrotate")
return tree.rrotate(cur) return tree.rrotate(cur)
} }
} else { } else {
r := cur.children[1] r := cur.children[1]
rlsize, rrsize := getChildrenSize(r) rlsize, rrsize := getChildrenSize(r)
if rlsize > rrsize { if rlsize > rrsize {
log.Println("lrrotate")
return tree.lrrotate(cur) return tree.lrrotate(cur)
} else { } else {
log.Println("lrotate")
return tree.lrotate(cur) return tree.lrotate(cur)
} }
} }

View File

@ -189,15 +189,25 @@ func TestGetAround(t *testing.T) {
// // for test error case // // for test error case
func TestPutStable(t *testing.T) {
tree := newVBT(compare.Int)
for i := 0; i < 20; i++ {
v := randomdata.Number(0, 100)
tree.Put(v)
t.Error(i, tree.debugString(), v)
}
}
func TestPutComparatorRandom(t *testing.T) { func TestPutComparatorRandom(t *testing.T) {
for n := 0; n < 300000; n++ { for n := 0; n < 50000; n++ {
tree := newVBT(compare.Int) tree := newVBT(compare.Int)
godsavl := avltree.NewWithIntComparator() godsavl := avltree.NewWithIntComparator()
content := "" content := ""
m := make(map[int]int) m := make(map[int]int)
for i := 0; len(m) < 10; i++ { for i := 0; len(m) < 50; i++ {
v := randomdata.Number(0, 65535) v := randomdata.Number(0, 65535)
if _, ok := m[v]; !ok { if _, ok := m[v]; !ok {
m[v] = v m[v] = v
@ -622,16 +632,6 @@ func BenchmarkPut(b *testing.B) {
} }
} }
func TestPutStable(t *testing.T) {
tree := newVBT(compare.Int)
for i := 0; i < 10; i++ {
v := randomdata.Number(0, 100)
tree.Put(v)
t.Error(tree.debugString(), v)
}
}
func BenchmarkIndex(b *testing.B) { func BenchmarkIndex(b *testing.B) {
tree := newVBT(compare.Int) tree := newVBT(compare.Int)

View File

@ -191,13 +191,13 @@ func TestGetAround(t *testing.T) {
func TestPutComparatorRandom(t *testing.T) { func TestPutComparatorRandom(t *testing.T) {
for n := 0; n < 300000; n++ { for n := 0; n < 100000; n++ {
tree := New(compare.Int) tree := New(compare.Int)
godsavl := avltree.NewWithIntComparator() godsavl := avltree.NewWithIntComparator()
content := "" content := ""
m := make(map[int]int) m := make(map[int]int)
for i := 0; len(m) < 10; i++ { for i := 0; len(m) < 20; i++ {
v := randomdata.Number(0, 65535) v := randomdata.Number(0, 65535)
if _, ok := m[v]; !ok { if _, ok := m[v]; !ok {
m[v] = v m[v] = v

View File

@ -20,7 +20,7 @@ import (
const CompareSize = 1000000 const CompareSize = 1000000
const NumberMax = 50000000 const NumberMax = 50000000
func Save(t *testing.T) { func TestSave(t *testing.T) {
f, err := os.OpenFile("../l.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) f, err := os.OpenFile("../l.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil { if err != nil {
@ -447,10 +447,8 @@ func BenchmarkIndexRange(b *testing.B) {
func BenchmarkSkipListSet(b *testing.B) { func BenchmarkSkipListSet(b *testing.B) {
l := loadTestData() l := loadTestData()
execCount := 1 execCount := 1
b.N = len(l) * execCount b.N = len(l) * execCount
for i := 0; i < execCount; i++ { for i := 0; i < execCount; i++ {
sl := skiplist.New(skiplist.Int) sl := skiplist.New(skiplist.Int)
for _, v := range l { for _, v := range l {