vbt 测试通过
This commit is contained in:
parent
47fea0ca3b
commit
bf96ae47cd
|
@ -12,6 +12,7 @@ type Node struct {
|
|||
children [2]*Node
|
||||
parent *Node
|
||||
size int
|
||||
key interface{}
|
||||
value interface{}
|
||||
}
|
||||
|
||||
|
@ -207,7 +208,7 @@ func (tree *Tree) removeNode(n *Node) {
|
|||
|
||||
cparent := cur.parent
|
||||
// 修改为interface 交换
|
||||
n.value, cur.value = cur.value, n.value
|
||||
n.key, n.value, cur.key, cur.value = cur.key, cur.value, n.key, n.value
|
||||
|
||||
// 考虑到刚好替换的节点是 被替换节点的孩子节点的时候, 从自身修复高度
|
||||
if cparent == n {
|
||||
|
@ -244,14 +245,14 @@ func (tree *Tree) Values() []interface{} {
|
|||
return result
|
||||
}
|
||||
|
||||
func (tree *Tree) GetRange(v1, v2 interface{}) (result []interface{}) {
|
||||
c := tree.comparator(v2, v1)
|
||||
func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
||||
c := tree.comparator(k2, k1)
|
||||
switch c {
|
||||
case 1:
|
||||
|
||||
var min, max *Node
|
||||
resultmin := tree.getArountNode(v1)
|
||||
resultmax := tree.getArountNode(v2)
|
||||
resultmin := tree.getArountNode(k1)
|
||||
resultmax := tree.getArountNode(k2)
|
||||
for i := 1; i < 3 && min == nil; i++ {
|
||||
min = resultmin[i]
|
||||
}
|
||||
|
@ -276,8 +277,8 @@ func (tree *Tree) GetRange(v1, v2 interface{}) (result []interface{}) {
|
|||
case -1:
|
||||
|
||||
var min, max *Node
|
||||
resultmin := tree.getArountNode(v2)
|
||||
resultmax := tree.getArountNode(v1)
|
||||
resultmin := tree.getArountNode(k2)
|
||||
resultmax := tree.getArountNode(k1)
|
||||
for i := 1; i < 3 && min == nil; i++ {
|
||||
min = resultmin[i]
|
||||
}
|
||||
|
@ -299,7 +300,7 @@ func (tree *Tree) GetRange(v1, v2 interface{}) (result []interface{}) {
|
|||
}
|
||||
}
|
||||
case 0:
|
||||
if n, ok := tree.GetNode(v1); ok {
|
||||
if n, ok := tree.GetNode(k1); ok {
|
||||
return []interface{}{n.value}
|
||||
}
|
||||
return []interface{}{}
|
||||
|
@ -308,16 +309,16 @@ func (tree *Tree) GetRange(v1, v2 interface{}) (result []interface{}) {
|
|||
return
|
||||
}
|
||||
|
||||
func (tree *Tree) Get(value interface{}) (interface{}, bool) {
|
||||
n, ok := tree.GetNode(value)
|
||||
func (tree *Tree) Get(key interface{}) (interface{}, bool) {
|
||||
n, ok := tree.GetNode(key)
|
||||
if ok {
|
||||
return n.value, true
|
||||
}
|
||||
return n, false
|
||||
}
|
||||
|
||||
func (tree *Tree) GetAround(value interface{}) (result [3]interface{}) {
|
||||
an := tree.getArountNode(value)
|
||||
func (tree *Tree) GetAround(key interface{}) (result [3]interface{}) {
|
||||
an := tree.getArountNode(key)
|
||||
for i, n := range an {
|
||||
if n != nil {
|
||||
result[i] = n.value
|
||||
|
@ -326,13 +327,13 @@ func (tree *Tree) GetAround(value interface{}) (result [3]interface{}) {
|
|||
return
|
||||
}
|
||||
|
||||
func (tree *Tree) getArountNode(value interface{}) (result [3]*Node) {
|
||||
func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
||||
var last *Node
|
||||
var lastc int
|
||||
|
||||
for n := tree.root; n != nil; {
|
||||
last = n
|
||||
c := tree.comparator(value, n.value)
|
||||
c := tree.comparator(key, n.key)
|
||||
switch c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
|
@ -360,7 +361,7 @@ func (tree *Tree) getArountNode(value interface{}) (result [3]*Node) {
|
|||
for ; parent != nil && parent.parent != nil; parent = parent.parent {
|
||||
child := getRelationship(parent)
|
||||
if child == (-lastc+2)/2 { // child 与 comparator 后左右的关系
|
||||
result[0] = parent.parent
|
||||
result[2] = parent.parent
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -482,10 +483,10 @@ func (tree *Tree) getArountNode(value interface{}) (result [3]*Node) {
|
|||
return
|
||||
}
|
||||
|
||||
func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
|
||||
func (tree *Tree) GetNode(key interface{}) (*Node, bool) {
|
||||
|
||||
for n := tree.root; n != nil; {
|
||||
switch c := tree.comparator(value, n.value); c {
|
||||
switch c := tree.comparator(key, n.key); c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
case 1:
|
||||
|
@ -499,9 +500,9 @@ func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
|
|||
return nil, false
|
||||
}
|
||||
|
||||
func (tree *Tree) Put(value interface{}) {
|
||||
func (tree *Tree) Put(key, value interface{}) {
|
||||
|
||||
node := &Node{value: value, size: 1}
|
||||
node := &Node{key: key, value: value, size: 1}
|
||||
if tree.root == nil {
|
||||
tree.root = node
|
||||
return
|
||||
|
@ -536,7 +537,7 @@ func (tree *Tree) Put(value interface{}) {
|
|||
|
||||
cur.size++
|
||||
parent = cur
|
||||
c := tree.comparator(value, cur.value)
|
||||
c := tree.comparator(key, cur.key)
|
||||
child = (c + 2) / 2
|
||||
cur = cur.children[child]
|
||||
}
|
||||
|
@ -564,7 +565,7 @@ const (
|
|||
RLD
|
||||
)
|
||||
|
||||
// Traversal 遍历的方法
|
||||
// Traversal 遍历的方法 默认是LDR 从小到大 comparator 为 l < r
|
||||
func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...interface{}) {
|
||||
if tree.root == nil {
|
||||
return
|
||||
|
@ -695,7 +696,7 @@ func (tree *Tree) lrrotate3(cur *Node) {
|
|||
movparent := cur.children[l]
|
||||
mov := movparent.children[r]
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
|
||||
|
||||
cur.children[r] = mov
|
||||
mov.parent = cur
|
||||
|
@ -719,7 +720,7 @@ func (tree *Tree) lrrotate(cur *Node) {
|
|||
movparent := cur.children[l]
|
||||
mov := movparent.children[r]
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
|
||||
|
||||
if mov.children[l] != nil {
|
||||
movparent.children[r] = mov.children[l]
|
||||
|
@ -758,7 +759,7 @@ func (tree *Tree) rlrotate3(cur *Node) {
|
|||
movparent := cur.children[l]
|
||||
mov := movparent.children[r]
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
|
||||
|
||||
cur.children[r] = mov
|
||||
mov.parent = cur
|
||||
|
@ -782,7 +783,7 @@ func (tree *Tree) rlrotate(cur *Node) {
|
|||
movparent := cur.children[l]
|
||||
mov := movparent.children[r]
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值, 达到相对位移
|
||||
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
|
||||
|
||||
if mov.children[l] != nil {
|
||||
movparent.children[r] = mov.children[l]
|
||||
|
@ -818,7 +819,7 @@ func (tree *Tree) rrotate3(cur *Node) {
|
|||
// 1 right 0 left
|
||||
mov := cur.children[l]
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
|
||||
|
||||
cur.children[r] = mov
|
||||
mov.size = 1
|
||||
|
@ -838,7 +839,7 @@ func (tree *Tree) rrotate(cur *Node) {
|
|||
// 1 right 0 left
|
||||
mov := cur.children[l]
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
|
||||
|
||||
// mov.children[l]不可能为nil
|
||||
mov.children[l].parent = cur
|
||||
|
@ -871,7 +872,7 @@ func (tree *Tree) lrotate3(cur *Node) {
|
|||
// 1 right 0 left
|
||||
mov := cur.children[l]
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
|
||||
|
||||
cur.children[r] = mov
|
||||
mov.size = 1
|
||||
|
@ -891,7 +892,7 @@ func (tree *Tree) lrotate(cur *Node) {
|
|||
// 1 right 0 left
|
||||
mov := cur.children[l]
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
|
||||
|
||||
// mov.children[l]不可能为nil
|
||||
mov.children[l].parent = cur
|
||||
|
@ -1005,7 +1006,7 @@ func output(node *Node, prefix string, isTail bool, str *string) {
|
|||
*str += "┌── "
|
||||
}
|
||||
|
||||
*str += spew.Sprint(node.value) + "\n"
|
||||
*str += spew.Sprint(node.key) + ":" + spew.Sprint(node.value) + "\n"
|
||||
|
||||
if node.children[0] != nil {
|
||||
newPrefix := prefix
|
||||
|
@ -1042,10 +1043,10 @@ func outputfordebug(node *Node, prefix string, isTail bool, str *string) {
|
|||
if node.parent == nil {
|
||||
parentv = "nil"
|
||||
} else {
|
||||
parentv = spew.Sprint(node.parent.value)
|
||||
parentv = spew.Sprint(node.key) + ":" + spew.Sprint(node.value)
|
||||
}
|
||||
suffix += parentv + "|" + spew.Sprint(node.size) + ")"
|
||||
*str += spew.Sprint(node.value) + suffix + "\n"
|
||||
*str += spew.Sprint(node.key) + ":" + spew.Sprint(node.value) + suffix + "\n"
|
||||
|
||||
if node.children[0] != nil {
|
||||
newPrefix := prefix
|
|
@ -20,7 +20,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 {
|
||||
|
@ -60,21 +60,11 @@ func loadTestData() []int {
|
|||
return l
|
||||
}
|
||||
|
||||
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} {
|
||||
// tree.Put(v)
|
||||
// }
|
||||
// t.Error(tree.Values())
|
||||
// t.Error(tree.debugString())
|
||||
|
||||
}
|
||||
|
||||
func TestIndexRange(t *testing.T) {
|
||||
tree := New(utils.IntComparator)
|
||||
l := []int{7, 14, 14, 14, 16, 17, 20, 30, 21, 40, 50, 3, 40, 40, 40, 15}
|
||||
for _, v := range l {
|
||||
tree.Put(v)
|
||||
tree.Put(v, v)
|
||||
}
|
||||
// [3 7 14 14 14 15 16 17 20 21 30 40 40 40 40 50]
|
||||
// t.Error(tree.Values(), tree.Size())
|
||||
|
@ -134,7 +124,7 @@ func TestIndexRange(t *testing.T) {
|
|||
func TestGetAround(t *testing.T) {
|
||||
tree := New(utils.IntComparator)
|
||||
for _, v := range []int{7, 14, 14, 14, 16, 17, 20, 30, 21, 40, 50, 3, 40, 40, 40, 15} {
|
||||
tree.Put(v)
|
||||
tree.Put(v, v)
|
||||
}
|
||||
|
||||
var Result string
|
||||
|
@ -168,14 +158,14 @@ func TestGetAround(t *testing.T) {
|
|||
}
|
||||
|
||||
Result = spew.Sprint(tree.GetAround(18))
|
||||
if Result != "[<nil> <nil> 20]" {
|
||||
if Result != "[17 <nil> 20]" {
|
||||
t.Error(tree.Values())
|
||||
t.Error("18 is not in list, tree.GetAround(18)) is error", Result)
|
||||
t.Error(tree.debugString())
|
||||
}
|
||||
|
||||
Result = spew.Sprint(tree.GetAround(5))
|
||||
if Result != "[3 <nil> <nil>]" {
|
||||
if Result != "[3 <nil> 7]" {
|
||||
t.Error(tree.Values())
|
||||
t.Error("5 is not in list, tree.GetAround(5)) is error", Result)
|
||||
t.Error(tree.debugString())
|
||||
|
@ -212,7 +202,7 @@ func TestPutComparatorRandom(t *testing.T) {
|
|||
if _, ok := m[v]; !ok {
|
||||
m[v] = v
|
||||
content += spew.Sprint(v) + ","
|
||||
tree.Put(v)
|
||||
tree.Put(v, v)
|
||||
godsavl.Put(v, v)
|
||||
}
|
||||
}
|
||||
|
@ -232,7 +222,7 @@ func TestPutComparatorRandom(t *testing.T) {
|
|||
func TestGet(t *testing.T) {
|
||||
tree := New(utils.IntComparator)
|
||||
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
|
||||
tree.Put(v)
|
||||
tree.Put(v, v)
|
||||
}
|
||||
|
||||
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
|
||||
|
@ -250,7 +240,7 @@ func TestGet(t *testing.T) {
|
|||
func TestGetRange(t *testing.T) {
|
||||
tree := New(utils.IntComparator)
|
||||
for _, v := range []int{5, 6, 8, 10, 13, 17, 1, 2, 40, 30} {
|
||||
tree.Put(v)
|
||||
tree.Put(v, v)
|
||||
}
|
||||
|
||||
// t.Error(tree.debugString())
|
||||
|
@ -306,11 +296,8 @@ func TestGetRange(t *testing.T) {
|
|||
|
||||
func TestTravalsal(t *testing.T) {
|
||||
tree := New(utils.IntComparator)
|
||||
|
||||
l := loadTestData()
|
||||
N := len(l)
|
||||
for i := 0; i < N; i++ {
|
||||
tree.Put(l[i])
|
||||
for _, v := range []int{5, 6, 8, 10, 13, 17, 1, 2, 40, 30} {
|
||||
tree.Put(v, v)
|
||||
}
|
||||
|
||||
i := 0
|
||||
|
@ -323,7 +310,11 @@ func TestTravalsal(t *testing.T) {
|
|||
}
|
||||
return true
|
||||
})
|
||||
|
||||
if spew.Sprint(result) != "[1 2 5 6 8 10 13 17 30 40]" {
|
||||
t.Error(result)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestRemoveAll(t *testing.T) {
|
||||
|
@ -339,7 +330,7 @@ ALL:
|
|||
if _, ok := m[v]; !ok {
|
||||
m[v] = v
|
||||
l = append(l, v)
|
||||
tree.Put(v)
|
||||
tree.Put(v, v)
|
||||
gods.Put(v, v)
|
||||
}
|
||||
}
|
||||
|
@ -377,7 +368,7 @@ ALL:
|
|||
if _, ok := m[v]; !ok {
|
||||
l = append(l, v)
|
||||
m[v] = v
|
||||
tree.Put(v)
|
||||
tree.Put(v, v)
|
||||
gods.Put(v, v)
|
||||
}
|
||||
}
|
||||
|
@ -437,7 +428,7 @@ func BenchmarkIndexRange(b *testing.B) {
|
|||
b.N = len(l)
|
||||
|
||||
for _, v := range l {
|
||||
tree.Put(v)
|
||||
tree.Put(v, v)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
|
@ -475,7 +466,7 @@ func BenchmarkIterator(b *testing.B) {
|
|||
b.N = len(l)
|
||||
|
||||
for _, v := range l {
|
||||
tree.Put(v)
|
||||
tree.Put(v, v)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
|
@ -501,7 +492,7 @@ func BenchmarkRemove(b *testing.B) {
|
|||
|
||||
b.N = len(l)
|
||||
for _, v := range l {
|
||||
tree.Put(v)
|
||||
tree.Put(v, v)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
|
@ -555,7 +546,7 @@ func BenchmarkGet(b *testing.B) {
|
|||
l := loadTestData()
|
||||
b.N = len(l)
|
||||
for i := 0; i < b.N; i++ {
|
||||
tree.Put(l[i])
|
||||
tree.Put(l[i], i)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
|
@ -626,7 +617,7 @@ func BenchmarkPut(b *testing.B) {
|
|||
for i := 0; i < execCount; i++ {
|
||||
tree := New(utils.IntComparator)
|
||||
for _, v := range l {
|
||||
tree.Put(v)
|
||||
tree.Put(v, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -643,7 +634,7 @@ func TestPutStable(t *testing.T) {
|
|||
|
||||
tree := New(utils.IntComparator)
|
||||
for _, v := range l {
|
||||
tree.Put(v)
|
||||
tree.Put(v, v)
|
||||
}
|
||||
result := tree.getArountNode(60)
|
||||
|
||||
|
@ -694,7 +685,7 @@ func BenchmarkIndex(b *testing.B) {
|
|||
l := loadTestData()
|
||||
b.N = len(l)
|
||||
for i := 0; i < b.N; i++ {
|
||||
tree.Put(l[i])
|
||||
tree.Put(l[i], i)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
|
@ -723,7 +714,7 @@ func BenchmarkTraversal(b *testing.B) {
|
|||
l := loadTestData()
|
||||
b.N = len(l)
|
||||
for i := 0; i < b.N; i++ {
|
||||
tree.Put(l[i])
|
||||
tree.Put(l[i], i)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
Loading…
Reference in New Issue
Block a user