fix avlkey some bug

This commit is contained in:
huangsimin 2019-04-09 17:09:18 +08:00
parent 1f3df9205f
commit 9c458a42b8
2 changed files with 102 additions and 168 deletions

View File

@ -28,8 +28,7 @@ type Tree struct {
root *Node root *Node
size int size int
Compare compare.Compare Compare compare.Compare
iter *Iterator
iter *Iterator
} }
func New(Compare compare.Compare) *Tree { func New(Compare compare.Compare) *Tree {
@ -103,7 +102,8 @@ func (tree *Tree) Remove(key interface{}) (interface{}, bool) {
cparent := cur.parent cparent := cur.parent
// 修改为interface 交换 // 修改为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 { if cparent == n {
@ -125,7 +125,7 @@ func (tree *Tree) Values() []interface{} {
mszie = tree.size mszie = tree.size
} }
result := make([]interface{}, 0, mszie) result := make([]interface{}, 0, mszie)
tree.Traversal(func(v interface{}) bool { tree.Traversal(func(k, v interface{}) bool {
result = append(result, v) result = append(result, v)
return true return true
}, LDR) }, LDR)
@ -222,7 +222,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
for n := tree.root; n != nil; { for n := tree.root; n != nil; {
last = n last = n
c := tree.Compare(key, n.value) c := tree.Compare(key, n.key)
switch c { switch c {
case -1: case -1:
n = n.children[0] n = n.children[0]
@ -236,7 +236,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
iter := tree.iter iter := tree.iter
iter.Prev() iter.Prev()
for iter.Prev() { for iter.Prev() {
if tree.Compare(iter.cur.value, n.value) == 0 { if tree.Compare(iter.cur.key, n.key) == 0 {
n = iter.cur n = iter.cur
} else { } else {
break break
@ -291,6 +291,7 @@ func (tree *Tree) GetNode(key interface{}) (*Node, bool) {
case 1: case 1:
n = n.children[1] n = n.children[1]
case 0: case 0:
tree.iter.SetNode(n) tree.iter.SetNode(n)
iter := tree.iter iter := tree.iter
iter.Prev() iter.Prev()
@ -317,25 +318,29 @@ func (tree *Tree) Put(key, value interface{}) {
return return
} }
cur := tree.root for cur, c := tree.root, 0; ; {
parent := cur.parent c = tree.Compare(key, cur.key)
child := -1 if c == -1 {
if cur.children[0] == nil {
for { cur.children[0] = node
cur.children[0].parent = cur
if cur == nil { if cur.height == 0 {
parent.children[child] = node tree.fixPutHeight(cur)
node.parent = parent }
if node.parent.height == 0 { return
tree.fixPutHeight(node.parent)
} }
return cur = cur.children[0]
} else {
if cur.children[1] == nil {
cur.children[1] = node
cur.children[1].parent = cur
if cur.height == 0 {
tree.fixPutHeight(cur)
}
return
}
cur = cur.children[1]
} }
parent = cur
c := tree.Compare(key, cur.key)
child = (c + 2) / 2
cur = cur.children[child]
} }
} }
@ -350,19 +355,16 @@ const (
LDR LDR
// LRD 同理 // LRD 同理
LRD LRD
// DRL 同理 // DRL 同理
DRL DRL
// RDL 先从右边有序访问到左边 从大到小 // RDL 先从右边有序访问到左边 从大到小
RDL RDL
// RLD 同理 // RLD 同理
RLD RLD
) )
// Traversal 遍历的方法 默认是LDR 从小到大 Compare 为 l < r // 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 { if tree.root == nil {
return return
} }
@ -379,7 +381,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i
if cur == nil { if cur == nil {
return true return true
} }
if !every(cur.value) { if !every(cur.key, cur.value) {
return false return false
} }
if !traverasl(cur.children[0]) { if !traverasl(cur.children[0]) {
@ -400,7 +402,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i
if !traverasl(cur.children[0]) { if !traverasl(cur.children[0]) {
return false return false
} }
if !every(cur.value) { if !every(cur.key, cur.value) {
return false return false
} }
if !traverasl(cur.children[1]) { if !traverasl(cur.children[1]) {
@ -421,7 +423,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i
if !traverasl(cur.children[1]) { if !traverasl(cur.children[1]) {
return false return false
} }
if !every(cur.value) { if !every(cur.key, cur.value) {
return false return false
} }
return true return true
@ -433,7 +435,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i
if cur == nil { if cur == nil {
return true return true
} }
if !every(cur.value) { if !every(cur.key, cur.value) {
return false return false
} }
if !traverasl(cur.children[0]) { if !traverasl(cur.children[0]) {
@ -454,7 +456,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i
if !traverasl(cur.children[1]) { if !traverasl(cur.children[1]) {
return false return false
} }
if !every(cur.value) { if !every(cur.key, cur.value) {
return false return false
} }
if !traverasl(cur.children[0]) { if !traverasl(cur.children[0]) {
@ -475,7 +477,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i
if !traverasl(cur.children[0]) { if !traverasl(cur.children[0]) {
return false return false
} }
if !every(cur.value) { if !every(cur.key, cur.value) {
return false return false
} }
return true return true
@ -492,7 +494,8 @@ func (tree *Tree) lrrotate(cur *Node) {
movparent := cur.children[l] movparent := cur.children[l]
mov := movparent.children[r] 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 { if mov.children[l] != nil {
movparent.children[r] = mov.children[l] movparent.children[r] = mov.children[l]
@ -519,10 +522,6 @@ func (tree *Tree) lrrotate(cur *Node) {
cur.children[r] = mov cur.children[r] = mov
mov.parent = cur mov.parent = cur
// movparent.size = getChildrenSumSize(movparent) + 1
// mov.size = getChildrenSumSize(mov) + 1
// cur.size = getChildrenSumSize(cur) + 1
mov.height = getMaxChildrenHeight(mov) + 1 mov.height = getMaxChildrenHeight(mov) + 1
movparent.height = getMaxChildrenHeight(movparent) + 1 movparent.height = getMaxChildrenHeight(movparent) + 1
cur.height = getMaxChildrenHeight(cur) + 1 cur.height = getMaxChildrenHeight(cur) + 1
@ -536,7 +535,8 @@ func (tree *Tree) rlrotate(cur *Node) {
movparent := cur.children[l] movparent := cur.children[l]
mov := movparent.children[r] 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 { if mov.children[l] != nil {
movparent.children[r] = mov.children[l] movparent.children[r] = mov.children[l]
@ -561,53 +561,11 @@ func (tree *Tree) rlrotate(cur *Node) {
cur.children[r] = mov cur.children[r] = mov
mov.parent = cur mov.parent = cur
// movparent.size = getChildrenSumSize(movparent) + 1
// mov.size = getChildrenSumSize(mov) + 1
// cur.size = getChildrenSumSize(cur) + 1
mov.height = getMaxChildrenHeight(mov) + 1 mov.height = getMaxChildrenHeight(mov) + 1
movparent.height = getMaxChildrenHeight(movparent) + 1 movparent.height = getMaxChildrenHeight(movparent) + 1
cur.height = getMaxChildrenHeight(cur) + 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) { func (tree *Tree) rrotate(cur *Node) {
const l = 0 const l = 0
@ -615,7 +573,8 @@ func (tree *Tree) rrotate(cur *Node) {
// 1 right 0 left // 1 right 0 left
mov := cur.children[l] 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]不可能为nil
mov.children[l].parent = cur mov.children[l].parent = cur
@ -638,48 +597,6 @@ func (tree *Tree) rrotate(cur *Node) {
// 连接转移后的节点 由于mov只是与cur交换值,parent不变 // 连接转移后的节点 由于mov只是与cur交换值,parent不变
cur.children[r] = mov 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 mov.height = getMaxChildrenHeight(mov) + 1
cur.height = getMaxChildrenHeight(cur) + 1 cur.height = getMaxChildrenHeight(cur) + 1
} }
@ -691,7 +608,8 @@ func (tree *Tree) lrotate(cur *Node) {
mov := cur.children[l] 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 // 不可能为nil
mov.children[l].parent = cur mov.children[l].parent = cur
@ -712,9 +630,6 @@ func (tree *Tree) lrotate(cur *Node) {
cur.children[r] = mov cur.children[r] = mov
// mov.size = getChildrenSumSize(mov) + 1
// cur.size = getChildrenSumSize(cur) + 1
mov.height = getMaxChildrenHeight(mov) + 1 mov.height = getMaxChildrenHeight(mov) + 1
cur.height = getMaxChildrenHeight(cur) + 1 cur.height = getMaxChildrenHeight(cur) + 1
} }
@ -845,7 +760,7 @@ func output(node *Node, prefix string, isTail bool, str *string) {
*str += "┌── " *str += "┌── "
} }
*str += spew.Sprint(node.key) + "\n" *str += spew.Sprint(node.value) + "\n"
if node.children[0] != nil { if node.children[0] != nil {
newPrefix := prefix newPrefix := prefix
@ -885,7 +800,7 @@ func outputfordebug(node *Node, prefix string, isTail bool, str *string) {
parentv = spew.Sprint(node.parent.value) parentv = spew.Sprint(node.parent.value)
} }
suffix += parentv + "|" + spew.Sprint(node.height) + ")" 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 { if node.children[0] != nil {
newPrefix := prefix newPrefix := prefix

View File

@ -5,7 +5,6 @@ import (
"encoding/gob" "encoding/gob"
"io/ioutil" "io/ioutil"
"log" "log"
"os"
"testing" "testing"
"474420502.top/eson/structure/compare" "474420502.top/eson/structure/compare"
@ -13,7 +12,6 @@ import (
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/emirpasic/gods/trees/avltree" "github.com/emirpasic/gods/trees/avltree"
"github.com/emirpasic/gods/trees/redblacktree" "github.com/emirpasic/gods/trees/redblacktree"
"github.com/emirpasic/gods/utils"
) )
func loadTestData() []int { func loadTestData() []int {
@ -28,7 +26,7 @@ func loadTestData() []int {
} }
func TestIterator(t *testing.T) { func TestIterator(t *testing.T) {
tree := New(utils.IntComparator) tree := New(compare.Int)
for _, v := range []int{1, 2, 7, 4, 5, 6, 7, 14, 15, 20, 30, 21, 3} { for _, v := range []int{1, 2, 7, 4, 5, 6, 7, 14, 15, 20, 30, 21, 3} {
// t.Error(v) // t.Error(v)
tree.Put(v, v) tree.Put(v, v)
@ -50,6 +48,7 @@ func TestIterator(t *testing.T) {
// └── 1` // └── 1`
iter := tree.Iterator() // root start point iter := tree.Iterator() // root start point
l := []int{14, 15, 20, 21, 30} l := []int{14, 15, 20, 21, 30}
for i := 0; iter.Next(); i++ { for i := 0; iter.Next(); i++ {
@ -214,30 +213,23 @@ func TestGetAround(t *testing.T) {
// for test error case // for test error case
func TestPutStable(t *testing.T) { func TestPutStable(t *testing.T) {
f, _ := os.OpenFile("./test.log", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666) // f, _ := os.OpenFile("./test.log", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
log.SetOutput(f) // 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} // 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 < 100000; i++ { // tree := New(compare.Int)
var l []int // for i := 0; i < 10; i++ {
for len(l) < 1000 { // tree.Put(randomdata.Number(0, 100))
l = append(l, randomdata.Number(0, 100)) // }
} // t.Error(tree.debugString())
tree := New(compare.Int)
for _, v := range l {
tree.Put(v, v)
}
}
// t.Error(avl.debugString(), avl.TraversalBreadth(), "\n", "-----------")
// t.Error(tree.debugString(), tree.TraversalBreadth(), "\n", "-----------")
} }
func TestPutComparatorRandom(t *testing.T) { func TestPutComparatorRandom(t *testing.T) {
for n := 0; n < 300000; n++ { for n := 0; n < 300000; n++ {
tree := New(utils.IntComparator) tree := New(compare.Int)
godsavl := avltree.NewWithIntComparator() godsavl := avltree.NewWithIntComparator()
content := "" content := ""
@ -262,7 +254,7 @@ func TestPutComparatorRandom(t *testing.T) {
} }
func TestGet(t *testing.T) { func TestGet(t *testing.T) {
tree := New(utils.IntComparator) tree := New(compare.Int)
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} { for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
tree.Put(v, v) tree.Put(v, v)
} }
@ -294,7 +286,7 @@ func TestGet(t *testing.T) {
} }
if v, ok := tree.Get(10000); ok { if v, ok := tree.Get(10000); ok {
t.Error("the val(1000) is not in tree, but is found", v) t.Error("the val(10000) is not in tree, but is found", v)
} }
} }
@ -302,13 +294,13 @@ func TestGet(t *testing.T) {
func TestRemoveAll(t *testing.T) { func TestRemoveAll(t *testing.T) {
ALL: ALL:
for c := 0; c < 5000; c++ { for c := 0; c < 50000; c++ {
tree := New(utils.IntComparator) tree := New(compare.Int)
gods := avltree.NewWithIntComparator() gods := avltree.NewWithIntComparator()
var l []int var l []int
m := make(map[int]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) v := randomdata.Number(0, 100000)
if _, ok := m[v]; !ok { if _, ok := m[v]; !ok {
m[v] = v m[v] = v
@ -318,7 +310,7 @@ ALL:
} }
} }
for i := 0; i < 100; i++ { for i := 0; i < 50; i++ {
tree.Remove(l[i]) tree.Remove(l[i])
gods.Remove(l[i]) gods.Remove(l[i])
s1 := spew.Sprint(tree.Values()) s1 := spew.Sprint(tree.Values())
@ -338,7 +330,7 @@ func TestRemove(t *testing.T) {
ALL: ALL:
for N := 0; N < 500000; N++ { for N := 0; N < 500000; N++ {
tree := New(utils.IntComparator) tree := New(compare.Int)
gods := avltree.NewWithIntComparator() gods := avltree.NewWithIntComparator()
var l []int var l []int
@ -361,13 +353,13 @@ ALL:
tree.Remove(l[i]) tree.Remove(l[i])
gods.Remove(l[i]) gods.Remove(l[i])
if spew.Sprint(gods.Values()) != spew.Sprint(tree.Values()) && tree.size != 0 { if spew.Sprint(gods.Values()) != spew.Sprint(tree.Values()) && tree.size != 0 {
// if gods.String() != avl.String() && gods.Size() != 0 && avl.size != 0 { // if gods.String() != tree.String() && gods.Size() != 0 && tree.size != 0 {
t.Error(src1) t.Error(src1)
t.Error(src2) t.Error(src2)
t.Error(tree.debugString()) t.Error(tree.debugString())
t.Error(gods.String()) t.Error(gods.String())
t.Error(l[i]) t.Error(l[i])
// t.Error(avl.TraversalDepth(-1)) // t.Error(tree.TraversalDepth(-1))
// t.Error(gods.Values()) // t.Error(gods.Values())
break ALL break ALL
} }
@ -459,15 +451,24 @@ func BenchmarkGodsRBRemove(b *testing.B) {
func BenchmarkGet(b *testing.B) { func BenchmarkGet(b *testing.B) {
tree := New(utils.IntComparator) tree := New(compare.Int)
l := loadTestData() l := loadTestData()
b.N = len(l) b.N = len(l)
for i := 0; i < b.N; i++ {
tree.Put(l[i], l[i])
}
b.ResetTimer() b.ResetTimer()
b.StartTimer() 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)
}
} }
} }
@ -476,11 +477,20 @@ func BenchmarkGodsRBGet(b *testing.B) {
l := loadTestData() l := loadTestData()
b.N = len(l) b.N = len(l)
for i := 0; i < b.N; i++ {
tree.Put(l[i], l[i])
}
b.ResetTimer() b.ResetTimer()
b.StartTimer() 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)
}
} }
} }
@ -489,11 +499,20 @@ func BenchmarkGodsAvlGet(b *testing.B) {
l := loadTestData() l := loadTestData()
b.N = len(l) b.N = len(l)
for i := 0; i < b.N; i++ {
tree.Put(l[i], l[i])
}
b.ResetTimer() b.ResetTimer()
b.StartTimer() 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)
}
} }
} }
@ -504,15 +523,15 @@ func BenchmarkPut(b *testing.B) {
b.ResetTimer() b.ResetTimer()
b.StartTimer() b.StartTimer()
execCount := 50 execCount := 10
b.N = len(l) * execCount b.N = len(l) * execCount
for i := 0; i < execCount; i++ { for i := 0; i < execCount; i++ {
tree := New(utils.IntComparator) tree := New(compare.Int)
for _, v := range l { for _, v := range l {
tree.Put(v, v) tree.Put(v, v)
} }
} }
// b.Log(avl.count) // b.Log(tree.count)
} }
func BenchmarkGodsRBPut(b *testing.B) { func BenchmarkGodsRBPut(b *testing.B) {