TODO: Remove error

This commit is contained in:
eson 2019-03-19 01:52:34 +08:00
parent b26bfdde9b
commit 50e4bd754f
2 changed files with 115 additions and 96 deletions

View File

@ -46,6 +46,9 @@ func (avl *Tree) Iterator() *Iterator {
}
func (avl *Tree) Size() int {
if avl.root == nil {
return 0
}
return avl.root.size
}
@ -66,7 +69,7 @@ func (avl *Tree) Remove(key interface{}) *Node {
}
var cur *Node
if ls > ls {
if ls > rs {
cur = n.children[0]
for cur.children[1] != nil {
cur = cur.children[1]
@ -250,6 +253,10 @@ func (avl *Tree) debugString() string {
}
func (avl *Tree) TraversalBreadth() (result []interface{}) {
if avl.root == nil {
return
}
result = make([]interface{}, 0, avl.root.size)
var traverasl func(cur *Node)
traverasl = func(cur *Node) {
@ -265,6 +272,10 @@ func (avl *Tree) TraversalBreadth() (result []interface{}) {
}
func (avl *Tree) TraversalDepth(leftright int) (result []interface{}) {
if avl.root == nil {
return
}
result = make([]interface{}, 0, avl.root.size)
if leftright < 0 {
var traverasl func(cur *Node)
@ -480,7 +491,16 @@ func getSize(cur *Node) int {
}
func (avl *Tree) fixRemoveHeight(cur *Node) {
for cur != nil {
cur.size--
if cur.size > 9 {
ls, rs := cur.children[0].size, cur.children[1].size
if rs >= ls*2 || ls >= rs*2 {
avl.fixPutHeight(cur, ls, rs)
}
}
cur = cur.parent
}
}
// func abs(n int) int {

View File

@ -9,12 +9,13 @@ import (
"testing"
"github.com/Pallinder/go-randomdata"
"github.com/davecgh/go-spew/spew"
"github.com/emirpasic/gods/trees/avltree"
"github.com/emirpasic/gods/trees/redblacktree"
"github.com/emirpasic/gods/utils"
)
const CompartorSize = 1000
const CompartorSize = 1000000
const NumberMax = 50000000
func TestSave(t *testing.T) {
@ -184,80 +185,80 @@ func TestGet(t *testing.T) {
}
// func TestRemoveAll(t *testing.T) {
func TestRemoveAll(t *testing.T) {
// ALL:
// for c := 0; c < 5000; c++ {
// avl := New(utils.IntComparator)
// gods := avltree.NewWithIntComparator()
// var l []int
// m := make(map[int]int)
ALL:
for c := 0; c < 5000; c++ {
avl := New(utils.IntComparator)
gods := avltree.NewWithIntComparator()
var l []int
m := make(map[int]int)
// for i := 0; len(l) < 100; i++ {
// v := randomdata.Number(0, 100000)
// if _, ok := m[v]; !ok {
// m[v] = v
// l = append(l, v)
// avl.Put(v)
// gods.Put(v, v)
// }
// }
for i := 0; len(l) < 100; i++ {
v := randomdata.Number(0, 100000)
if _, ok := m[v]; !ok {
m[v] = v
l = append(l, v)
avl.Put(v)
gods.Put(v, v)
}
}
// for i := 0; i < 100; i++ {
// avl.Remove(l[i])
// gods.Remove(l[i])
// s1 := spew.Sprint(avl.TraversalDepth(-1))
// s2 := spew.Sprint(gods.Values())
// if s1 != s2 {
// t.Error("avl remove error", "avlsize = ", avl.Size())
// t.Error(s1)
// t.Error(s2)
// break ALL
// }
// }
// }
// }
for i := 0; i < 100; i++ {
avl.Remove(l[i])
gods.Remove(l[i])
s1 := spew.Sprint(avl.TraversalDepth(-1))
s2 := spew.Sprint(gods.Values())
if s1 != s2 {
t.Error("avl remove error", "avlsize = ", avl.Size())
t.Error(s1)
t.Error(s2)
break ALL
}
}
}
}
// func TestRemove(t *testing.T) {
func TestRemove(t *testing.T) {
// ALL:
// for N := 0; N < 500000; N++ {
// avl := New(utils.IntComparator)
// gods := avltree.NewWithIntComparator()
ALL:
for N := 0; N < 50000; N++ {
avl := New(utils.IntComparator)
gods := avltree.NewWithIntComparator()
// var l []int
// m := make(map[int]int)
var l []int
m := make(map[int]int)
// for i := 0; len(l) < 10; i++ {
// v := randomdata.Number(0, 100)
// if _, ok := m[v]; !ok {
// l = append(l, v)
// m[v] = v
// avl.Put(v)
// gods.Put(v, v)
// }
// }
for i := 0; len(l) < 10; i++ {
v := randomdata.Number(0, 100)
if _, ok := m[v]; !ok {
l = append(l, v)
m[v] = v
avl.Put(v)
gods.Put(v, v)
}
}
// src1 := avl.String()
// src2 := gods.String()
src1 := avl.String()
src2 := gods.String()
// for i := 0; i < 10; i++ {
// avl.Remove(l[i])
// gods.Remove(l[i])
// if spew.Sprint(gods.Values()) != spew.Sprint(avl.TraversalDepth(-1)) && avl.size != 0 {
// // if gods.String() != avl.String() && gods.Size() != 0 && avl.size != 0 {
// t.Error(src1)
// t.Error(src2)
// t.Error(avl.debugString())
// t.Error(gods.String())
// t.Error(l[i])
// // t.Error(avl.TraversalDepth(-1))
// // t.Error(gods.Values())
// break ALL
// }
// }
// }
// }
for i := 0; i < 10; i++ {
avl.Remove(l[i])
gods.Remove(l[i])
if avl.root != nil && spew.Sprint(gods.Values()) != spew.Sprint(avl.TraversalDepth(-1)) {
// if gods.String() != avl.String() && gods.Size() != 0 && avl.size != 0 {
t.Error(src1)
t.Error(src2)
t.Error(avl.debugString())
t.Error(gods.String())
t.Error(l[i])
// t.Error(avl.TraversalDepth(-1))
// t.Error(gods.Values())
break ALL
}
}
}
}
// func BenchmarkIterator(b *testing.B) {
// tree := New(utils.IntComparator)
@ -302,41 +303,41 @@ func TestGet(t *testing.T) {
// }
// }
// func BenchmarkRemove(b *testing.B) {
// tree := New(utils.IntComparator)
func BenchmarkRemove(b *testing.B) {
tree := New(utils.IntComparator)
// l := loadTestData()
l := loadTestData()
// b.N = len(l)
// for _, v := range l {
// tree.Put(v)
// }
b.N = len(l)
for _, v := range l {
tree.Put(v)
}
// b.ResetTimer()
// b.StartTimer()
b.ResetTimer()
b.StartTimer()
// for i := 0; i < len(l); i++ {
// tree.Remove(l[i])
// }
// }
for i := 0; i < len(l); i++ {
tree.Remove(l[i])
}
}
// func BenchmarkGodsRemove(b *testing.B) {
// tree := avltree.NewWithIntComparator()
func BenchmarkGodsRemove(b *testing.B) {
tree := avltree.NewWithIntComparator()
// l := loadTestData()
l := loadTestData()
// b.N = len(l)
// for _, v := range l {
// tree.Put(v, v)
// }
b.N = len(l)
for _, v := range l {
tree.Put(v, v)
}
// b.ResetTimer()
// b.StartTimer()
b.ResetTimer()
b.StartTimer()
// for i := 0; i < len(l); i++ {
// tree.Remove(l[i])
// }
// }
for i := 0; i < len(l); i++ {
tree.Remove(l[i])
}
}
// func BenchmarkGodsRBRemove(b *testing.B) {
// tree := redblacktree.NewWithIntComparator()
@ -426,8 +427,6 @@ func TestPutStable(t *testing.T) {
t.Error(avl.debugString(), v)
}
t.Error(avl.count)
// t.Error(len(l), avl.debugString(), "\n", "-----------") // 3 6(4)
}