diff --git a/avldup/avldup.go b/avldup/avldup.go index bf22d0b..f0552fe 100644 --- a/avldup/avldup.go +++ b/avldup/avldup.go @@ -287,39 +287,41 @@ func (tree *Tree) GetNode(value interface{}) (*Node, bool) { return nil, false } -func (tree *Tree) Put(value interface{}) { - tree.size++ - node := &Node{value: value} - if tree.size == 1 { - tree.root = node +func (tree *Tree) Put(key interface{}) { + + if tree.size == 0 { + tree.root = &Node{value: key} + tree.size++ return } for cur, c := tree.root, 0; ; { - c = tree.Compare(value, cur.value) - if c == 0 { - cur.value = value - return - } else if c == -1 { + c = tree.Compare(key, cur.value) + if c == -1 { if cur.children[0] == nil { - cur.children[0] = node - node.parent = cur + tree.size++ + cur.children[0] = &Node{value: key} + cur.children[0].parent = cur if cur.height == 0 { tree.fixPutHeight(cur) } return } cur = cur.children[0] - } else { + } else if c == 1 { if cur.children[1] == nil { - cur.children[1] = node - node.parent = cur + tree.size++ + cur.children[1] = &Node{value: key} + cur.children[1].parent = cur if cur.height == 0 { tree.fixPutHeight(cur) } return } cur = cur.children[1] + } else { + cur.value = key + return } } } diff --git a/avlkeydup/avlkeydup.go b/avlkeydup/avlkeydup.go index c113f56..fff617c 100644 --- a/avlkeydup/avlkeydup.go +++ b/avlkeydup/avlkeydup.go @@ -28,8 +28,7 @@ type Tree struct { root *Node size int Compare compare.Compare - - iter *Iterator + iter *Iterator } func New(Compare compare.Compare) *Tree { @@ -103,7 +102,8 @@ func (tree *Tree) Remove(key interface{}) (interface{}, bool) { cparent := cur.parent // 修改为interface 交换 - n.key, n.value, cur.key, cur.value = cur.key, cur.value, n.key, n.value + n.value, cur.value = cur.value, n.value + n.key, cur.key = cur.key, n.key // 考虑到刚好替换的节点是 被替换节点的孩子节点的时候, 从自身修复高度 if cparent == n { @@ -125,7 +125,7 @@ func (tree *Tree) Values() []interface{} { mszie = tree.size } result := make([]interface{}, 0, mszie) - tree.Traversal(func(v interface{}) bool { + tree.Traversal(func(k, v interface{}) bool { result = append(result, v) return true }, LDR) @@ -222,7 +222,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) { for n := tree.root; n != nil; { last = n - c := tree.Compare(key, n.value) + c := tree.Compare(key, n.key) switch c { case -1: n = n.children[0] @@ -231,17 +231,6 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) { n = n.children[1] lastc = c case 0: - - tree.iter.SetNode(n) - iter := tree.iter - iter.Prev() - for iter.Prev() { - if tree.Compare(iter.cur.value, n.value) == 0 { - n = iter.cur - } else { - break - } - } result[1] = n n = nil default: @@ -291,17 +280,6 @@ func (tree *Tree) GetNode(key interface{}) (*Node, bool) { case 1: n = n.children[1] case 0: - - tree.iter.SetNode(n) - iter := tree.iter - iter.Prev() - for iter.Prev() { - if tree.Compare(iter.cur.key, n.key) == 0 { - n = iter.cur - } else { - break - } - } return n, true default: panic("Get Compare only is allowed in -1, 0, 1") @@ -311,37 +289,42 @@ func (tree *Tree) GetNode(key interface{}) (*Node, bool) { } func (tree *Tree) Put(key, value interface{}) { - tree.size++ - node := &Node{key: key, value: value} - if tree.size == 1 { - tree.root = node + + if tree.size == 0 { + tree.size++ + tree.root = &Node{key: key, value: value} return } - cur := tree.root - parent := cur.parent - child := -1 - - for { - - if cur == nil { - parent.children[child] = node - node.parent = parent - if node.parent.height == 0 { - tree.fixPutHeight(node.parent) + for cur, c := tree.root, 0; ; { + c = tree.Compare(key, cur.key) + if c == -1 { + if cur.children[0] == nil { + tree.size++ + cur.children[0] = &Node{key: key, value: value} + cur.children[0].parent = cur + if cur.height == 0 { + tree.fixPutHeight(cur) + } + return } - return - } - - parent = cur - c := tree.Compare(key, cur.key) - if c == 0 { + cur = cur.children[0] + } else if c == 1 { + if cur.children[1] == nil { + tree.size++ + cur.children[1] = &Node{key: key, value: value} + cur.children[1].parent = cur + if cur.height == 0 { + tree.fixPutHeight(cur) + } + return + } + cur = cur.children[1] + } else { cur.key = key cur.value = value return } - child = (c + 2) / 2 - cur = cur.children[child] } } @@ -356,19 +339,16 @@ const ( LDR // LRD 同理 LRD - // DRL 同理 DRL - // RDL 先从右边有序访问到左边 从大到小 RDL - // RLD 同理 RLD ) // Traversal 遍历的方法 默认是LDR 从小到大 Compare 为 l < r -func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...interface{}) { +func (tree *Tree) Traversal(every func(k, v interface{}) bool, traversalMethod ...interface{}) { if tree.root == nil { return } @@ -385,7 +365,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i if cur == nil { return true } - if !every(cur.value) { + if !every(cur.key, cur.value) { return false } if !traverasl(cur.children[0]) { @@ -406,7 +386,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i if !traverasl(cur.children[0]) { return false } - if !every(cur.value) { + if !every(cur.key, cur.value) { return false } if !traverasl(cur.children[1]) { @@ -427,7 +407,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i if !traverasl(cur.children[1]) { return false } - if !every(cur.value) { + if !every(cur.key, cur.value) { return false } return true @@ -439,7 +419,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i if cur == nil { return true } - if !every(cur.value) { + if !every(cur.key, cur.value) { return false } if !traverasl(cur.children[0]) { @@ -460,7 +440,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i if !traverasl(cur.children[1]) { return false } - if !every(cur.value) { + if !every(cur.key, cur.value) { return false } if !traverasl(cur.children[0]) { @@ -481,7 +461,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i if !traverasl(cur.children[0]) { return false } - if !every(cur.value) { + if !every(cur.key, cur.value) { return false } return true @@ -498,7 +478,8 @@ func (tree *Tree) lrrotate(cur *Node) { movparent := cur.children[l] mov := movparent.children[r] - mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移 + mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移 + mov.key, cur.key = cur.key, mov.key if mov.children[l] != nil { movparent.children[r] = mov.children[l] @@ -525,10 +506,6 @@ func (tree *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 @@ -542,7 +519,8 @@ func (tree *Tree) rlrotate(cur *Node) { movparent := cur.children[l] mov := movparent.children[r] - mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移 + mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移 + mov.key, cur.key = cur.key, mov.key if mov.children[l] != nil { movparent.children[r] = mov.children[l] @@ -567,53 +545,11 @@ func (tree *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 } -func (tree *Tree) rrotateex(cur *Node) { - - const l = 0 - const r = 1 - // 1 right 0 left - mov := cur.children[l] - if mov == nil { - return - } - mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移 - - // mov.children[l]不可能为nil - cur.children[l] = mov.children[l] - if mov.children[l] != nil { - mov.children[l].parent = cur - } - - // 解决mov节点孩子转移的问题 - if mov.children[r] != nil { - mov.children[l] = mov.children[r] - } else { - mov.children[l] = nil - } - - if cur.children[r] != nil { - mov.children[r] = cur.children[r] - mov.children[r].parent = mov - } else { - mov.children[r] = nil - } - - // 连接转移后的节点 由于mov只是与cur交换值,parent不变 - cur.children[r] = mov - - mov.height = getMaxChildrenHeight(mov) + 1 - cur.height = getMaxChildrenHeight(cur) + 1 -} - func (tree *Tree) rrotate(cur *Node) { const l = 0 @@ -621,7 +557,8 @@ func (tree *Tree) rrotate(cur *Node) { // 1 right 0 left mov := cur.children[l] - mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移 + mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移 + mov.key, cur.key = cur.key, mov.key // mov.children[l]不可能为nil mov.children[l].parent = cur @@ -644,48 +581,6 @@ func (tree *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 -} - -func (tree *Tree) lrotateex(cur *Node) { - - const l = 1 - const r = 0 - - mov := cur.children[l] - if mov == nil { - return - } - mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移 - - // 不可能为nil - cur.children[l] = mov.children[l] - if mov.children[l] != nil { - mov.children[l].parent = cur - } - - if mov.children[r] != nil { - mov.children[l] = mov.children[r] - } else { - mov.children[l] = nil - } - - if cur.children[r] != nil { - mov.children[r] = cur.children[r] - mov.children[r].parent = mov - } else { - mov.children[r] = nil - } - - cur.children[r] = mov - - // mov.size = getChildrenSumSize(mov) + 1 - // cur.size = getChildrenSumSize(cur) + 1 - mov.height = getMaxChildrenHeight(mov) + 1 cur.height = getMaxChildrenHeight(cur) + 1 } @@ -697,7 +592,8 @@ func (tree *Tree) lrotate(cur *Node) { mov := cur.children[l] - mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移 + mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移 + mov.key, cur.key = cur.key, mov.key // 不可能为nil mov.children[l].parent = cur @@ -718,9 +614,6 @@ func (tree *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 } @@ -851,7 +744,7 @@ func output(node *Node, prefix string, isTail bool, str *string) { *str += "┌── " } - *str += spew.Sprint(node.key) + "\n" + *str += spew.Sprint(node.value) + "\n" if node.children[0] != nil { newPrefix := prefix @@ -891,7 +784,7 @@ func outputfordebug(node *Node, prefix string, isTail bool, str *string) { parentv = spew.Sprint(node.parent.value) } suffix += parentv + "|" + spew.Sprint(node.height) + ")" - *str += spew.Sprint(node.key) + suffix + "\n" + *str += spew.Sprint(node.value) + suffix + "\n" if node.children[0] != nil { newPrefix := prefix diff --git a/avlkeydup/avlkeydup_test.go b/avlkeydup/avlkeydup_test.go index 544f889..4af3c98 100644 --- a/avlkeydup/avlkeydup_test.go +++ b/avlkeydup/avlkeydup_test.go @@ -12,8 +12,6 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/emirpasic/gods/trees/avltree" "github.com/emirpasic/gods/trees/redblacktree" - - "github.com/emirpasic/gods/utils" ) func loadTestData() []int { @@ -27,6 +25,62 @@ func loadTestData() []int { return l } +func TestIterator(t *testing.T) { + tree := New(compare.Int) + for _, v := range []int{1, 2, 7, 4, 5, 6, 7, 14, 15, 20, 30, 21, 3} { + // t.Error(v) + tree.Put(v, v) + + } + // ` AVLTree + // │ ┌── 30 + // │ │ └── 21 + // │ ┌── 20 + // │ │ └── 15 + // └── 14 + // │ ┌── 7 + // │ ┌── 7 + // │ │ └── 6 + // └── 5 + // │ ┌── 4 + // │ │ └── 3 + // └── 2 + // └── 1` + + iter := tree.Iterator() // root start point + + l := []int{14, 15, 20, 21, 30} + + for i := 0; iter.Next(); i++ { + if iter.Value().(int) != l[i] { + t.Error("iter Next error", iter.Value(), l[i]) + } + } + + iter.Next() + if iter.Value().(int) != 30 { + t.Error("Next == false", iter.Value(), iter.Next(), iter.Value()) + } + + l = []int{21, 20, 15, 14, 7, 7, 6, 5, 4, 3, 2, 1} + for i := 0; iter.Prev(); i++ { // cur is 30 next is 21 + if iter.Value().(int) != l[i] { + t.Error(iter.Value()) + } + } + + if iter.Prev() != false { + t.Error("Prev is error, cur is tail, val = 1 Prev return false") + } + if iter.Value().(int) != 1 { // cur is 1 + t.Error("next == false", iter.Value(), iter.Prev(), iter.Value()) + } + + if iter.Next() != true && iter.Value().(int) != 2 { + t.Error("next to prev is error") + } +} + func TestGetRange(t *testing.T) { tree := New(compare.Int) for _, v := range []int{5, 6, 8, 10, 13, 17, 1, 2, 40, 30} { @@ -93,9 +147,9 @@ func TestGetAround(t *testing.T) { var Result string Result = spew.Sprint(tree.GetAround(14)) - if Result != "[7 14 15]" { + if Result != "[7 14 14]" { t.Error(tree.Values()) - t.Error("17 is root, tree.GetAround(14)) is error", Result) + t.Error("14 is root, tree.GetAround(14)) is error", Result) t.Error(tree.debugString()) } @@ -114,7 +168,7 @@ func TestGetAround(t *testing.T) { } Result = spew.Sprint(tree.GetAround(40)) - if Result != "[30 40 50]" { + if Result != "[30 40 40]" { t.Error(tree.Values()) t.Error("tree.GetAround(40)) is error", Result) t.Error(tree.debugString()) @@ -159,6 +213,16 @@ 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} + + // tree := New(compare.Int) + // for i := 0; i < 10; i++ { + // tree.Put(randomdata.Number(0, 100)) + // } + // t.Error(tree.debugString()) + // t.Error(tree.debugString(), tree.TraversalBreadth(), "\n", "-----------") } @@ -230,13 +294,13 @@ func TestGet(t *testing.T) { func TestRemoveAll(t *testing.T) { ALL: - for c := 0; c < 5000; c++ { + for c := 0; c < 50000; c++ { tree := New(compare.Int) gods := avltree.NewWithIntComparator() var l []int m := make(map[int]int) - for i := 0; len(l) < 100; i++ { + for i := 0; len(l) < 50; i++ { v := randomdata.Number(0, 100000) if _, ok := m[v]; !ok { m[v] = v @@ -246,7 +310,7 @@ ALL: } } - for i := 0; i < 100; i++ { + for i := 0; i < 50; i++ { tree.Remove(l[i]) gods.Remove(l[i]) s1 := spew.Sprint(tree.Values()) @@ -304,7 +368,7 @@ ALL: } func BenchmarkIterator(b *testing.B) { - tree := New(utils.IntComparator) + tree := New(compare.Int) l := loadTestData() @@ -332,20 +396,19 @@ func BenchmarkIterator(b *testing.B) { } func BenchmarkRemove(b *testing.B) { - tree := New(utils.IntComparator) + tree := New(compare.Int) l := loadTestData() + b.N = len(l) for _, v := range l { tree.Put(v, v) } - ll := tree.Values() - b.N = len(ll) b.ResetTimer() b.StartTimer() - for i := 0; i < len(ll); i++ { + for i := 0; i < len(l); i++ { tree.Remove(l[i]) } } @@ -360,12 +423,10 @@ func BenchmarkGodsRemove(b *testing.B) { tree.Put(v, v) } - ll := tree.Values() - b.N = len(ll) b.ResetTimer() b.StartTimer() - for i := 0; i < len(ll); i++ { + for i := 0; i < len(l); i++ { tree.Remove(l[i]) } } @@ -380,13 +441,10 @@ func BenchmarkGodsRBRemove(b *testing.B) { tree.Put(v, v) } - ll := tree.Values() - b.N = len(ll) - b.ResetTimer() b.StartTimer() - for i := 0; i < len(ll); i++ { + for i := 0; i < len(l); i++ { tree.Remove(l[i]) } } @@ -397,11 +455,20 @@ func BenchmarkGet(b *testing.B) { l := loadTestData() b.N = len(l) + for i := 0; i < b.N; i++ { + tree.Put(l[i], l[i]) + } b.ResetTimer() b.StartTimer() - for i := 0; i < b.N; i++ { - tree.Get(l[i]) + + execCount := 10 + b.N = len(l) * execCount + + for i := 0; i < execCount; i++ { + for _, v := range l { + tree.Get(v) + } } } @@ -410,11 +477,20 @@ func BenchmarkGodsRBGet(b *testing.B) { l := loadTestData() b.N = len(l) + for i := 0; i < b.N; i++ { + tree.Put(l[i], l[i]) + } b.ResetTimer() b.StartTimer() - for i := 0; i < b.N; i++ { - tree.Get(l[i]) + + execCount := 10 + b.N = len(l) * execCount + + for i := 0; i < execCount; i++ { + for _, v := range l { + tree.Get(v) + } } } @@ -423,11 +499,20 @@ func BenchmarkGodsAvlGet(b *testing.B) { l := loadTestData() b.N = len(l) + for i := 0; i < b.N; i++ { + tree.Put(l[i], l[i]) + } b.ResetTimer() b.StartTimer() - for i := 0; i < b.N; i++ { - tree.Get(l[i]) + + execCount := 10 + b.N = len(l) * execCount + + for i := 0; i < execCount; i++ { + for _, v := range l { + tree.Get(v) + } } } @@ -438,7 +523,7 @@ func BenchmarkPut(b *testing.B) { b.ResetTimer() b.StartTimer() - execCount := 5 + execCount := 10 b.N = len(l) * execCount for i := 0; i < execCount; i++ { tree := New(compare.Int)