稳定后的一个版本

This commit is contained in:
huangsimin
2019-03-18 17:54:08 +08:00
parent 8f58b3d9f2
commit d7677074e1
4 changed files with 71 additions and 133 deletions

View File

@@ -1,8 +1,6 @@
package avlindex
import (
"log"
"github.com/davecgh/go-spew/spew"
"github.com/emirpasic/gods/utils"
@@ -166,26 +164,18 @@ func (avl *Tree) Put(value interface{}) {
parent.children[child] = node
node.parent = parent
fixed := node.parent.parent
for {
switch fsize := getSize(fixed); fsize {
case 3, 5:
log.Println(fsize)
lefts, rigths := getChildrenSize(fixed)
avl.fixPutHeight(fixed, lefts, rigths)
fixed = fixed.parent
default:
return
}
fixed := parent.parent
fsize := getSize(fixed)
if fsize == 3 {
lefts, rigths := getChildrenSize(fixed)
avl.fixPutHeight(fixed, lefts, rigths)
}
return
}
if cur.size > 7 {
if cur.size > 9 {
ls, rs := cur.children[0].size, cur.children[1].size
if rs >= ls*2 || ls >= rs*2 {
// ll, lr := getChildrenSize(cur.children[0])
// rl, rr := getChildrenSize(cur.children[1])
avl.fixPutHeight(cur, ls, rs)
}
}
@@ -196,7 +186,6 @@ func (avl *Tree) Put(value interface{}) {
child = (c + 2) / 2
cur = cur.children[child]
}
}
func (avl *Tree) debugString() string {
@@ -439,22 +428,14 @@ func (avl *Tree) fixRemoveHeight(cur *Node) {
}
func abs(n int) int {
y := n >> 31
return (n ^ y) - y
}
// func abs(n int) int {
// y := n >> 31
// return (n ^ y) - y
// }
func (avl *Tree) fixPutHeight(cur *Node, lefts, rigths int) {
avl.count++
if lefts < rigths {
r := cur.children[1]
rlsize, rrsize := getChildrenSize(r)
if rlsize > rrsize {
avl.lrrotate(cur)
} else {
avl.lrotate(cur)
}
} else {
if lefts > rigths {
l := cur.children[0]
llsize, lrsize := getChildrenSize(l)
if lrsize > llsize {
@@ -462,6 +443,14 @@ func (avl *Tree) fixPutHeight(cur *Node, lefts, rigths int) {
} else {
avl.rrotate(cur)
}
} else {
r := cur.children[1]
rlsize, rrsize := getChildrenSize(r)
if rlsize > rrsize {
avl.lrrotate(cur)
} else {
avl.lrotate(cur)
}
}
}

View File

@@ -14,8 +14,8 @@ import (
"github.com/emirpasic/gods/utils"
)
const CompartorSize = 10000000
const NumberMax = 500000
const CompartorSize = 1000
const NumberMax = 50000000
func TestSave(t *testing.T) {
@@ -397,27 +397,28 @@ func BenchmarkGet(b *testing.B) {
// }
func BenchmarkPut(b *testing.B) {
avl := New(utils.IntComparator)
l := loadTestData()
b.ResetTimer()
b.StartTimer()
b.N = len(l)
for _, v := range l {
avl.Put(v)
execCount := 50
b.N = len(l) * execCount
for i := 0; i < execCount; i++ {
avl := New(utils.IntComparator)
for _, v := range l {
avl.Put(v)
}
}
b.Log(avl.count)
}
func TestPutStable(t *testing.T) {
l := []int{14, 18, 20, 21, 22, 23, 19}
// var l []int
// for i := 0; len(l) < 7; i++ {
// l = append(l, randomdata.Number(0, 65))
// }
// l := []int{14, 18, 20, 21, 22, 23, 19}
var l []int
for i := 0; len(l) < 100; i++ {
l = append(l, randomdata.Number(0, 65))
}
avl := New(utils.IntComparator)
for _, v := range l {
@@ -425,6 +426,8 @@ func TestPutStable(t *testing.T) {
t.Error(avl.debugString(), v)
}
t.Error(avl.count)
// t.Error(len(l), avl.debugString(), "\n", "-----------") // 3 6(4)
}