diff --git a/avl/avl.go b/avl/avl.go index 6c80bc2..a2a7d70 100644 --- a/avl/avl.go +++ b/avl/avl.go @@ -29,6 +29,7 @@ type Tree struct { root *Node size int comparator utils.Comparator + count int } func New(comparator utils.Comparator) *Tree { @@ -555,6 +556,7 @@ func (avl *Tree) fixRemoveHeight(cur *Node) { // 计算高度的差值 绝对值大于2的时候需要旋转 diff := lefth - rigthh if diff < -1 { + avl.count++ r := cur.children[1] // 根据左旋转的右边节点的子节点 左右高度选择旋转的方式 if getHeight(r.children[0]) > getHeight(r.children[1]) { avl.lrrotate(cur) @@ -562,6 +564,7 @@ func (avl *Tree) fixRemoveHeight(cur *Node) { avl.lrotate(cur) } } else if diff > 1 { + avl.count++ l := cur.children[0] if getHeight(l.children[1]) > getHeight(l.children[0]) { avl.rlrotate(cur) @@ -595,6 +598,7 @@ func (avl *Tree) fixPutHeight(cur *Node) { // 计算高度的差值 绝对值大于2的时候需要旋转 diff := lefth - rigthh if diff < -1 { + avl.count++ r := cur.children[1] // 根据左旋转的右边节点的子节点 左右高度选择旋转的方式 if getHeight(r.children[0]) > getHeight(r.children[1]) { avl.lrrotate(cur) @@ -602,6 +606,7 @@ func (avl *Tree) fixPutHeight(cur *Node) { avl.lrotate(cur) } } else if diff > 1 { + avl.count++ l := cur.children[0] if getHeight(l.children[1]) > getHeight(l.children[0]) { avl.rlrotate(cur) diff --git a/avl/avl_test.go b/avl/avl_test.go index 3d76c9e..684b736 100644 --- a/avl/avl_test.go +++ b/avl/avl_test.go @@ -441,7 +441,7 @@ func BenchmarkPut(b *testing.B) { for _, v := range l { avl.Put(v) } - + b.Log(avl.count) } func BenchmarkGodsRBPut(b *testing.B) { diff --git a/avlindex/avlindex.go b/avlindex/avlindex.go index 0d3c319..b86ca3d 100644 --- a/avlindex/avlindex.go +++ b/avlindex/avlindex.go @@ -1,6 +1,8 @@ package avlindex import ( + "log" + "github.com/davecgh/go-spew/spew" "github.com/emirpasic/gods/utils" @@ -28,6 +30,7 @@ func (n *Node) String() string { type Tree struct { root *Node comparator utils.Comparator + count int } func New(comparator utils.Comparator) *Tree { @@ -162,23 +165,28 @@ func (avl *Tree) Put(value interface{}) { if cur == nil { parent.children[child] = node node.parent = parent - if getSize(node.parent.parent) == 3 { - lefts, rigths := getChildrenSize(node.parent.parent) - avl.fixPutHeight(node.parent.parent, lefts, rigths) + + 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 + } } - return } - if cur.size > 4 { - ls, rs := getChildrenSize(cur) - if rs > ls { - if rs >= ls*2 { - avl.fixPutHeight(cur, ls, rs) - } - } else { - if ls >= rs*2 { - avl.fixPutHeight(cur, ls, rs) - } + if cur.size > 7 { + 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) } } @@ -325,14 +333,6 @@ func (avl *Tree) rlrotate(cur *Node) { movparent.size = getChildrenSumSize(movparent) + 1 mov.size = getChildrenSumSize(mov) + 1 cur.size = getChildrenSumSize(cur) + 1 - - // cur.size = 3 - // cur.children[0].size = 1 - // cur.children[1].size = 1 - - // mov.height = getMaxChildrenHeight(mov) + 1 - // movparent.height = getMaxChildrenHeight(movparent) + 1 - // cur.height = getMaxChildrenHeight(cur) + 1 } func (avl *Tree) rrotate(cur *Node) { @@ -418,7 +418,6 @@ func (avl *Tree) lrotate(cur *Node) { mov.size = getChildrenSumSize(mov) + 1 cur.size = getChildrenSumSize(cur) + 1 - } func getChildrenSumSize(cur *Node) int { @@ -446,8 +445,7 @@ func abs(n int) int { } func (avl *Tree) fixPutHeight(cur *Node, lefts, rigths int) { - - // lefts, rigths := getChildrenSize(cur) + avl.count++ if lefts < rigths { r := cur.children[1] rlsize, rrsize := getChildrenSize(r) @@ -456,7 +454,6 @@ func (avl *Tree) fixPutHeight(cur *Node, lefts, rigths int) { } else { avl.lrotate(cur) } - } else { l := cur.children[0] llsize, lrsize := getChildrenSize(l) @@ -466,7 +463,6 @@ func (avl *Tree) fixPutHeight(cur *Node, lefts, rigths int) { avl.rrotate(cur) } } - } func output(node *Node, prefix string, isTail bool, str *string) { diff --git a/avlindex/avlindex_test.go b/avlindex/avlindex_test.go index 58a1f92..35bab7a 100644 --- a/avlindex/avlindex_test.go +++ b/avlindex/avlindex_test.go @@ -15,7 +15,7 @@ import ( ) const CompartorSize = 10000000 -const NumberMax = 60000000 +const NumberMax = 500000 func TestSave(t *testing.T) { @@ -27,13 +27,19 @@ func TestSave(t *testing.T) { //fmt.Println(userBytes) var l []int - m := make(map[int]int) + + // for i := 0; len(l) < 1000; i++ { + // v := randomdata.Number(0, 65535) + // l = append(l, v) + // } + + //m := make(map[int]int) for i := 0; len(l) < CompartorSize; i++ { v := randomdata.Number(0, NumberMax) - if _, ok := m[v]; !ok { - m[v] = v - l = append(l, v) - } + // if _, ok := m[v]; !ok { + // m[v] = v + l = append(l, v) + // } } var result bytes.Buffer @@ -402,26 +408,24 @@ func BenchmarkPut(b *testing.B) { for _, v := range l { avl.Put(v) } + b.Log(avl.count) } func TestPutStable(t *testing.T) { - // l := []int{14, 18, 19, 20, 5, 6, 7, 8, 9, 21, 22, 30, 41, 41, 41, 0, 1, 2, 3, 4, 10, 11, 12, 13} - var l []int - for i := 0; len(l) < 100; i++ { - l = append(l, randomdata.Number(0, 65535)) - } - - for i := 0; len(l) < 1000; i++ { - l = append(l, randomdata.Number(70, 100)) - } + 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)) + // } avl := New(utils.IntComparator) for _, v := range l { avl.Put(v) + t.Error(avl.debugString(), v) } - t.Error(len(l), avl.debugString(), avl.TraversalBreadth(), "\n", "-----------") + // t.Error(len(l), avl.debugString(), "\n", "-----------") // 3 6(4) } func BenchmarkGodsRBPut(b *testing.B) { diff --git a/priority_queue/priority_queue.go b/priority_queue/priority_queue.go index a490fd8..6f856ab 100644 --- a/priority_queue/priority_queue.go +++ b/priority_queue/priority_queue.go @@ -1,4 +1,7 @@ package pqueue +import "474420502.top/eson/structure/avl" + type PriorityQueue struct { + datas *avl.Tree }