diff --git a/priority_queue/vbt.go b/priority_queue/vbt.go index 393f90b..cb4e7de 100644 --- a/priority_queue/vbt.go +++ b/priority_queue/vbt.go @@ -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) } } diff --git a/priority_queue/vbt_test.go b/priority_queue/vbt_test.go index 6501702..821fb47 100644 --- a/priority_queue/vbt_test.go +++ b/priority_queue/vbt_test.go @@ -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) diff --git a/vbt/vbt_test.go b/vbt/vbt_test.go index 783542d..5cb6068 100644 --- a/vbt/vbt_test.go +++ b/vbt/vbt_test.go @@ -191,13 +191,13 @@ func TestGetAround(t *testing.T) { func TestPutComparatorRandom(t *testing.T) { - for n := 0; n < 300000; n++ { + for n := 0; n < 100000; n++ { tree := New(compare.Int) godsavl := avltree.NewWithIntComparator() content := "" m := make(map[int]int) - for i := 0; len(m) < 10; i++ { + for i := 0; len(m) < 20; i++ { v := randomdata.Number(0, 65535) if _, ok := m[v]; !ok { m[v] = v diff --git a/vbtkey/vbtkey_test.go b/vbtkey/vbtkey_test.go index 1beda66..c771b52 100644 --- a/vbtkey/vbtkey_test.go +++ b/vbtkey/vbtkey_test.go @@ -20,7 +20,7 @@ import ( const CompareSize = 1000000 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) if err != nil { @@ -447,10 +447,8 @@ func BenchmarkIndexRange(b *testing.B) { func BenchmarkSkipListSet(b *testing.B) { l := loadTestData() - execCount := 1 b.N = len(l) * execCount - for i := 0; i < execCount; i++ { sl := skiplist.New(skiplist.Int) for _, v := range l {