完善了旋转

This commit is contained in:
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
import (
"log"
"474420502.top/eson/structure/compare"
"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) {
const l = 1
const r = 0
// 1 right 0 left
ln := cur.children[l]
lrn := ln.children[r]
movparent := cur.children[l]
mov := movparent.children[r]
if cur.parent != nil {
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
mov.parent = cur
lrn.children[r] = cur
lrn.children[r].parent = lrn
cur.children[l] = movparent
movparent.children[r] = nil
cur.children[l] = nil
ln.children[r] = nil
cur.children[r] = mov
mov.parent = cur
// cur.size = 3
// cur.children[r].size = 1
cur.children[l].size = 1
lrn.size = 3
ln.size = 1
cur.size = 1
}
func (tree *vbTree) lrrotate(cur *tNode) *tNode {
@@ -632,7 +639,9 @@ func (tree *vbTree) lrrotate(cur *tNode) *tNode {
lrrn := lrn.children[r]
ln.children[r] = lrln
ln.children[r].parent = ln
if lrln != nil {
ln.children[r].parent = ln
}
if cur.parent != nil {
if cur.parent.children[l] == cur {
@@ -652,7 +661,9 @@ func (tree *vbTree) lrrotate(cur *tNode) *tNode {
lrn.children[r].parent = lrn
cur.children[l] = lrrn
cur.children[l].parent = cur
if lrrn != nil {
cur.children[l].parent = cur
}
ln.size = getChildrenSumSize(ln) + 1
cur.size = getChildrenSumSize(cur) + 1
@@ -664,24 +675,33 @@ func (tree *vbTree) lrrotate(cur *tNode) *tNode {
func (tree *vbTree) rlrotate3(cur *tNode) {
const l = 0
const r = 1
// 1 right 0 left
ln := cur.children[l]
lrn := ln.children[r]
movparent := cur.children[l]
mov := movparent.children[r]
if cur.parent != nil {
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
mov.parent = cur
lrn.children[r] = cur
lrn.children[r].parent = lrn
cur.children[l] = movparent
movparent.children[r] = nil
cur.children[l] = nil
ln.children[r] = nil
cur.children[r] = mov
mov.parent = cur
// cur.size = 3
// cur.children[r].size = 1
cur.children[l].size = 1
lrn.size = 3
ln.size = 1
cur.size = 1
}
func (tree *vbTree) rlrotate(cur *tNode) *tNode {
@@ -696,7 +716,9 @@ func (tree *vbTree) rlrotate(cur *tNode) *tNode {
lrrn := lrn.children[r]
ln.children[r] = lrln
ln.children[r].parent = ln
if lrln != nil {
ln.children[r].parent = ln
}
if cur.parent != nil {
if cur.parent.children[l] == cur {
@@ -716,7 +738,9 @@ func (tree *vbTree) rlrotate(cur *tNode) *tNode {
lrn.children[r].parent = lrn
cur.children[l] = lrrn
cur.children[l].parent = cur
if lrrn != nil {
cur.children[l].parent = cur
}
ln.size = getChildrenSumSize(ln) + 1
cur.size = getChildrenSumSize(cur) + 1
@@ -729,19 +753,26 @@ func (tree *vbTree) rrotate3(cur *tNode) {
const l = 0
const r = 1
// 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
mov.size = 1
ln.children[r] = cur
cur.parent = ln
cur.children[l] = mov.children[l]
cur.children[l].parent = cur
cur.children[l] = nil
mov.children[l] = nil
mov.size = 1
ln.size = 3
cur.size = 1
}
func (tree *vbTree) rrotate(cur *tNode) *tNode {
@@ -781,19 +812,26 @@ func (tree *vbTree) lrotate3(cur *tNode) {
const l = 1
const r = 0
// 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
mov.size = 1
ln.children[r] = cur
cur.parent = ln
cur.children[l] = mov.children[l]
cur.children[l].parent = cur
cur.children[l] = nil
mov.children[l] = nil
mov.size = 1
ln.size = 3
cur.size = 1
}
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]
llsize, lrsize := getChildrenSize(l)
if lrsize > llsize {
log.Println("rlrotate")
return tree.rlrotate(cur)
} else {
log.Println("rrotate")
return tree.rrotate(cur)
}
} else {
r := cur.children[1]
rlsize, rrsize := getChildrenSize(r)
if rlsize > rrsize {
log.Println("lrrotate")
return tree.lrrotate(cur)
} else {
log.Println("lrotate")
return tree.lrotate(cur)
}
}

View File

@@ -189,15 +189,25 @@ func TestGetAround(t *testing.T) {
// // 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) {
for n := 0; n < 300000; n++ {
for n := 0; n < 50000; n++ {
tree := newVBT(compare.Int)
godsavl := avltree.NewWithIntComparator()
content := ""
m := make(map[int]int)
for i := 0; len(m) < 10; i++ {
for i := 0; len(m) < 50; i++ {
v := randomdata.Number(0, 65535)
if _, ok := m[v]; !ok {
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) {
tree := newVBT(compare.Int)