研究到 多少左右才适合

This commit is contained in:
eson 2019-03-18 03:22:01 +08:00
parent 07cacef0b5
commit 8f58b3d9f2
2 changed files with 99 additions and 18 deletions

View File

@ -1,6 +1,8 @@
package avl
import (
"log"
"github.com/davecgh/go-spew/spew"
"github.com/emirpasic/gods/utils"
@ -10,9 +12,25 @@ type Node struct {
children [2]*Node
parent *Node
height int
size int
value interface{}
}
func getChildrenSumSize(cur *Node) int {
return getSize(cur.children[0]) + getSize(cur.children[1])
}
func getChildrenSize(cur *Node) (int, int) {
return getSize(cur.children[0]), getSize(cur.children[1])
}
func getSize(cur *Node) int {
if cur == nil {
return 0
}
return cur.size
}
func (n *Node) String() string {
if n == nil {
return "nil"
@ -33,6 +51,9 @@ type Tree struct {
}
func New(comparator utils.Comparator) *Tree {
if m == nil {
m = make(map[int]int)
}
return &Tree{comparator: comparator}
}
@ -212,7 +233,7 @@ func (avl *Tree) GetNode(value interface{}) (*Node, bool) {
func (avl *Tree) Put(value interface{}) {
avl.size++
node := &Node{value: value}
node := &Node{value: value, size: 1}
if avl.size == 1 {
avl.root = node
return
@ -233,6 +254,7 @@ func (avl *Tree) Put(value interface{}) {
return
}
cur.size++
parent = cur
c := avl.comparator(value, cur.value)
child = (c + 2) / 2
@ -328,6 +350,10 @@ func (avl *Tree) lrrotate(cur *Node) {
cur.children[r] = mov
mov.parent = cur
movparent.size = getChildrenSumSize(movparent) + 1
mov.size = getChildrenSumSize(mov) + 1
cur.size = getChildrenSumSize(cur) + 1
mov.height = getMaxChildrenHeight(mov) + 1
movparent.height = getMaxChildrenHeight(movparent) + 1
cur.height = getMaxChildrenHeight(cur) + 1
@ -366,6 +392,10 @@ func (avl *Tree) rlrotate(cur *Node) {
cur.children[r] = mov
mov.parent = cur
movparent.size = getChildrenSumSize(movparent) + 1
mov.size = getChildrenSumSize(mov) + 1
cur.size = getChildrenSumSize(cur) + 1
mov.height = getMaxChildrenHeight(mov) + 1
movparent.height = getMaxChildrenHeight(movparent) + 1
cur.height = getMaxChildrenHeight(cur) + 1
@ -439,6 +469,9 @@ func (avl *Tree) rrotate(cur *Node) {
// 连接转移后的节点 由于mov只是与cur交换值,parent不变
cur.children[r] = mov
mov.size = getChildrenSumSize(mov) + 1
cur.size = getChildrenSumSize(cur) + 1
mov.height = getMaxChildrenHeight(mov) + 1
cur.height = getMaxChildrenHeight(cur) + 1
}
@ -475,6 +508,9 @@ func (avl *Tree) lrotateex(cur *Node) {
cur.children[r] = mov
mov.size = getChildrenSumSize(mov) + 1
cur.size = getChildrenSumSize(cur) + 1
mov.height = getMaxChildrenHeight(mov) + 1
cur.height = getMaxChildrenHeight(cur) + 1
}
@ -507,6 +543,9 @@ func (avl *Tree) lrotate(cur *Node) {
cur.children[r] = mov
mov.size = getChildrenSumSize(mov) + 1
cur.size = getChildrenSumSize(cur) + 1
mov.height = getMaxChildrenHeight(mov) + 1
cur.height = getMaxChildrenHeight(cur) + 1
}
@ -588,6 +627,8 @@ func (avl *Tree) fixRemoveHeight(cur *Node) {
}
var m map[int]int
func (avl *Tree) fixPutHeight(cur *Node) {
for {
@ -599,6 +640,25 @@ func (avl *Tree) fixPutHeight(cur *Node) {
diff := lefth - rigthh
if diff < -1 {
avl.count++
switch s := cur.size; s {
default:
ls, rs := getChildrenSize(cur)
if ls > rs {
ls, rs = rs, ls
}
if ls > 7 {
break
}
hash := ls + rs<<16
if _, ok := m[hash]; !ok {
m[hash] = 1
str := "size = " + spew.Sprint(cur.size, "l =", ls, rs) + "\n"
outputfordebug(cur, "", true, &str)
log.Print(str)
}
}
r := cur.children[1] // 根据左旋转的右边节点的子节点 左右高度选择旋转的方式
if getHeight(r.children[0]) > getHeight(r.children[1]) {
avl.lrrotate(cur)
@ -607,6 +667,25 @@ func (avl *Tree) fixPutHeight(cur *Node) {
}
} else if diff > 1 {
avl.count++
switch s := cur.size; s {
default:
ls, rs := getChildrenSize(cur)
if ls > rs {
ls, rs = rs, ls
}
if ls > 7 {
break
}
hash := ls + rs<<16
if _, ok := m[hash]; !ok {
m[hash] = 1
str := "size = " + spew.Sprint(cur.size, "l =", ls, rs) + "\n"
outputfordebug(cur, "", true, &str)
log.Print(str)
}
}
l := cur.children[0]
if getHeight(l.children[1]) > getHeight(l.children[0]) {
avl.rlrotate(cur)
@ -681,13 +760,13 @@ func outputfordebug(node *Node, prefix string, isTail bool, str *string) {
}
suffix := "("
parentv := ""
if node.parent == nil {
parentv = "nil"
} else {
parentv = spew.Sprint(node.parent.value)
}
suffix += parentv + "|" + spew.Sprint(node.height) + ")"
// parentv := ""
// if node.parent == nil {
// parentv = "nil"
// } else {
// parentv = spew.Sprint(node.parent.value)
// }
suffix += spew.Sprint(node.size) + ")"
*str += spew.Sprint(node.value) + suffix + "\n"
if node.children[0] != nil {

View File

@ -134,22 +134,24 @@ func TestGetAround(t *testing.T) {
// for test error case
func TestPutStable(t *testing.T) {
f, _ := os.OpenFile("./test.log", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
log.SetOutput(f)
// 0-1 3 | 2-3 7-8 | 4-7 12-16 | 8-15 20-32 | 16-31 33-58 l := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 18, 19, 20, 21, 22, 30, 41, 41, 41}
for i := 0; i < 100000000; i++ {
var l []int
for len(l) < 1000 {
l = append(l, randomdata.Number(0, 100))
}
l := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 18, 19, 20, 21, 22, 30, 41, 41, 41}
for range l {
avl := New(utils.IntComparator)
for _, v := range l {
avl.Put(v)
}
for i := 0; i < 10; i++ {
r := randomdata.Number(0, len(l))
t.Error(avl.String(), avl.TraversalBreadth())
n, _ := avl.GetNode(l[r])
avl.lrotateex(n)
}
t.Error(avl.String(), avl.TraversalBreadth(), "\n", "-----------")
}
// t.Error(avl.debugString(), avl.TraversalBreadth(), "\n", "-----------")
}
func TestPutComparatorRandom(t *testing.T) {