添加Index方法进
This commit is contained in:
parent
06d55a2f9e
commit
cc2f390743
|
@ -54,6 +54,37 @@ func (avl *Tree) Size() int {
|
|||
return avl.root.size
|
||||
}
|
||||
|
||||
func (avl *Tree) Index(idx int) (interface{}, bool) {
|
||||
cur := avl.root
|
||||
if idx >= 0 {
|
||||
for cur != nil {
|
||||
ls := getSize(cur.children[0])
|
||||
if idx == ls {
|
||||
return cur.value, true
|
||||
} else if idx < ls {
|
||||
cur = cur.children[0]
|
||||
} else {
|
||||
idx = idx - ls - 1
|
||||
cur = cur.children[1]
|
||||
}
|
||||
}
|
||||
} else {
|
||||
idx = -idx - 1
|
||||
for cur != nil {
|
||||
rs := getSize(cur.children[1])
|
||||
if idx == rs {
|
||||
return cur.value, true
|
||||
} else if idx < rs {
|
||||
cur = cur.children[1]
|
||||
} else {
|
||||
idx = idx - rs - 1
|
||||
cur = cur.children[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (avl *Tree) Remove(key interface{}) *Node {
|
||||
|
||||
if n, ok := avl.GetNode(key); ok {
|
||||
|
@ -135,39 +166,6 @@ func (avl *Tree) Get(key interface{}) (interface{}, bool) {
|
|||
return n, false
|
||||
}
|
||||
|
||||
func (avl *Tree) GetRange(min, max interface{}) []interface{} {
|
||||
|
||||
var minN *Node
|
||||
for minN = avl.root; minN != nil; {
|
||||
switch c := avl.comparator(min, minN.value); c {
|
||||
case -1:
|
||||
minN = minN.children[0]
|
||||
case 1:
|
||||
minN = minN.children[1]
|
||||
case 0:
|
||||
break
|
||||
default:
|
||||
panic("Get comparator only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
|
||||
var maxN *Node
|
||||
for maxN = avl.root; maxN != nil; {
|
||||
switch c := avl.comparator(min, maxN.value); c {
|
||||
case -1:
|
||||
maxN = maxN.children[0]
|
||||
case 1:
|
||||
maxN = maxN.children[1]
|
||||
case 0:
|
||||
break
|
||||
default:
|
||||
panic("Get comparator only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (avl *Tree) GetAround(key interface{}) (result [3]interface{}) {
|
||||
an := avl.GetAroundNode(key)
|
||||
for i, n := range an {
|
||||
|
|
|
@ -18,7 +18,7 @@ import (
|
|||
const CompartorSize = 1000000
|
||||
const NumberMax = 50000000
|
||||
|
||||
func TestSave(t *testing.T) {
|
||||
func Save(t *testing.T) {
|
||||
|
||||
f, err := os.OpenFile("../l.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
|
@ -63,12 +63,12 @@ func loadTestData() []int {
|
|||
}
|
||||
|
||||
func TestIterator(t *testing.T) {
|
||||
avl := New(utils.IntComparator)
|
||||
for _, v := range []int{7, 14, 14, 14, 16, 17, 20, 30, 21, 40, 40, 50, 3, 40, 40, 40, 15} {
|
||||
avl.Put(v)
|
||||
}
|
||||
t.Error(avl.Values())
|
||||
t.Error(avl.debugString())
|
||||
// avl := New(utils.IntComparator)
|
||||
// for _, v := range []int{7, 14, 14, 14, 16, 17, 20, 30, 21, 40, 40, 50, 3, 40, 40, 40, 15} {
|
||||
// avl.Put(v)
|
||||
// }
|
||||
// t.Error(avl.Values())
|
||||
// t.Error(avl.debugString())
|
||||
}
|
||||
|
||||
func TestGetAround(t *testing.T) {
|
||||
|
@ -357,17 +357,20 @@ func BenchmarkPut(b *testing.B) {
|
|||
func TestPutStable(t *testing.T) {
|
||||
|
||||
// l := []int{14, 18, 20, 21, 22, 23, 19}
|
||||
var l []int
|
||||
for i := 0; len(l) < 100; i++ {
|
||||
l = append(l, randomdata.Number(0, 65))
|
||||
}
|
||||
|
||||
avl := New(utils.IntComparator)
|
||||
for _, v := range l {
|
||||
avl.Put(v)
|
||||
t.Error(avl.debugString(), v)
|
||||
}
|
||||
// var l []int
|
||||
// for i := 0; len(l) < 10; 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(avl.Values())
|
||||
// for _, v := range []int{10, 0, 9, 5, -11, -10, -1, -5} {
|
||||
// t.Error(avl.Index(v))
|
||||
// }
|
||||
// t.Error(len(l), avl.debugString(), "\n", "-----------") // 3 6(4)
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user