Compare commits
No commits in common. "feature/lsv" and "develop" have entirely different histories.
feature/ls
...
develop
@ -1,6 +1,6 @@
|
|||||||
# structure
|
# structure
|
||||||
|
|
||||||
暂时没时间整理, 后期才整理完整.
|
暂时没时间整理, 后期才整理完整
|
||||||
|
|
||||||
## PriorityQueue
|
## PriorityQueue
|
||||||
|
|
||||||
|
|||||||
3
TODO
3
TODO
@ -1,2 +1 @@
|
|||||||
1.旋转的size数值计算错误
|
add iterator test every file
|
||||||
2.当左数比右数小的时候, 应该把split的数分配左边, 现在测试是一直保持拆分到右边
|
|
||||||
|
|||||||
5
go.mod
5
go.mod
@ -1,9 +1,8 @@
|
|||||||
module focus
|
module github.com/474420502/focus
|
||||||
|
|
||||||
go 1.14
|
go 1.12
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/474420502/focus v0.8.1 // indirect
|
|
||||||
github.com/Pallinder/go-randomdata v1.1.0
|
github.com/Pallinder/go-randomdata v1.1.0
|
||||||
github.com/davecgh/go-spew v1.1.1
|
github.com/davecgh/go-spew v1.1.1
|
||||||
)
|
)
|
||||||
|
|||||||
2
go.sum
2
go.sum
@ -1,5 +1,3 @@
|
|||||||
github.com/474420502/focus v0.8.1 h1:PZwCgzcnxwx7ZZCWc/XKLVaZPH9e4YX9cP4ckyT2HDA=
|
|
||||||
github.com/474420502/focus v0.8.1/go.mod h1:jrDXvK1CnUJ3PCR3ZJVYinbS2Yz5kM8OoAbCLe6AF7Y=
|
|
||||||
github.com/Pallinder/go-randomdata v1.1.0 h1:gUubB1IEUliFmzjqjhf+bgkg1o6uoFIkRsP3VrhEcx8=
|
github.com/Pallinder/go-randomdata v1.1.0 h1:gUubB1IEUliFmzjqjhf+bgkg1o6uoFIkRsP3VrhEcx8=
|
||||||
github.com/Pallinder/go-randomdata v1.1.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y=
|
github.com/Pallinder/go-randomdata v1.1.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
|||||||
@ -3,8 +3,7 @@ package arraylist
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"focus/list"
|
"github.com/474420502/focus/list"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -3,8 +3,7 @@ package linkedlist
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"focus/list"
|
"github.com/474420502/focus/list"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -3,8 +3,8 @@ package plist
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
"focus/list"
|
"github.com/474420502/focus/list"
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -7,8 +7,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,7 @@
|
|||||||
package linkedhashmap
|
package linkedhashmap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
linkedlist "focus/list/linked_list"
|
linkedlist "github.com/474420502/focus/list/linked_list"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -5,8 +5,8 @@ import (
|
|||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
"focus/tree/avlkeydup"
|
"github.com/474420502/focus/tree/avlkeydup"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TreeMap struct {
|
type TreeMap struct {
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package pqueue
|
package pqueue
|
||||||
|
|
||||||
import (
|
import (
|
||||||
lastack "focus/stack/listarraystack"
|
lastack "github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package pqueue
|
package pqueue
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestQueuePush(t *testing.T) {
|
func TestQueuePush(t *testing.T) {
|
||||||
|
|||||||
@ -3,7 +3,7 @@ package pqueue
|
|||||||
import (
|
import (
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
@ -451,9 +451,9 @@ func (tree *vbTree) Put(key interface{}) {
|
|||||||
for cur := tree.root; ; {
|
for cur := tree.root; ; {
|
||||||
|
|
||||||
if cur.size > 8 {
|
if cur.size > 8 {
|
||||||
factor := cur.size >> 3 // or factor = 1
|
factor := cur.size / 10 // or factor = 1
|
||||||
ls, rs := cur.children[0].size, cur.children[1].size
|
ls, rs := cur.children[0].size, cur.children[1].size
|
||||||
if rs >= (ls<<1)+factor || ls >= (rs<<1)+factor {
|
if rs >= ls*2+factor || ls >= rs*2+factor {
|
||||||
cur = tree.fixSize(cur, ls, rs)
|
cur = tree.fixSize(cur, ls, rs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -861,9 +861,9 @@ func (tree *vbTree) fixSizeWithRemove(cur *Node) {
|
|||||||
for cur != nil {
|
for cur != nil {
|
||||||
cur.size--
|
cur.size--
|
||||||
if cur.size > 8 {
|
if cur.size > 8 {
|
||||||
factor := cur.size >> 3 // or factor = 1
|
factor := cur.size / 10 // or factor = 1
|
||||||
ls, rs := getChildrenSize(cur)
|
ls, rs := getChildrenSize(cur)
|
||||||
if rs >= (ls<<1)+factor || ls >= (rs<<1)+factor {
|
if rs >= ls*2+factor || ls >= rs*2+factor {
|
||||||
cur = tree.fixSize(cur, ls, rs)
|
cur = tree.fixSize(cur, ls, rs)
|
||||||
}
|
}
|
||||||
} else if cur.size == 3 {
|
} else if cur.size == 3 {
|
||||||
|
|||||||
@ -7,7 +7,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package pqueuekey
|
package pqueuekey
|
||||||
|
|
||||||
import (
|
import (
|
||||||
lastack "focus/stack/listarraystack"
|
lastack "github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
@ -1,8 +1,7 @@
|
|||||||
package pqueuekey
|
package pqueuekey
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -4,8 +4,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|||||||
@ -3,7 +3,7 @@ package pqueuekey
|
|||||||
import (
|
import (
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
@ -451,9 +451,9 @@ func (tree *vbTree) Put(key, value interface{}) {
|
|||||||
for cur := tree.root; ; {
|
for cur := tree.root; ; {
|
||||||
|
|
||||||
if cur.size > 8 {
|
if cur.size > 8 {
|
||||||
factor := cur.size >> 3 // or factor = 1
|
factor := cur.size / 10 // or factor = 1
|
||||||
ls, rs := cur.children[0].size, cur.children[1].size
|
ls, rs := cur.children[0].size, cur.children[1].size
|
||||||
if rs >= (ls<<1)+factor || ls >= (rs<<1)+factor {
|
if rs >= ls*2+factor || ls >= rs*2+factor {
|
||||||
cur = tree.fixSize(cur, ls, rs)
|
cur = tree.fixSize(cur, ls, rs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -861,9 +861,9 @@ func (tree *vbTree) fixSizeWithRemove(cur *Node) {
|
|||||||
for cur != nil {
|
for cur != nil {
|
||||||
cur.size--
|
cur.size--
|
||||||
if cur.size > 8 {
|
if cur.size > 8 {
|
||||||
factor := cur.size >> 3 // or factor = 1
|
factor := cur.size / 10 // or factor = 1
|
||||||
ls, rs := getChildrenSize(cur)
|
ls, rs := getChildrenSize(cur)
|
||||||
if rs >= (ls<<1)+factor || ls >= (rs<<1)+factor {
|
if rs >= ls*2+factor || ls >= rs*2+factor {
|
||||||
cur = tree.fixSize(cur, ls, rs)
|
cur = tree.fixSize(cur, ls, rs)
|
||||||
}
|
}
|
||||||
} else if cur.size == 3 {
|
} else if cur.size == 3 {
|
||||||
|
|||||||
@ -4,8 +4,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
"focus/tree/avldup"
|
"github.com/474420502/focus/tree/avldup"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TreeSet
|
// TreeSet
|
||||||
|
|||||||
@ -6,8 +6,8 @@ import (
|
|||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
"focus/tree/avldup"
|
"github.com/474420502/focus/tree/avldup"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTreeSet_Add(t *testing.T) {
|
func TestTreeSet_Add(t *testing.T) {
|
||||||
|
|||||||
@ -1,8 +1,7 @@
|
|||||||
package lastack
|
package lastack
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"focus/stack"
|
"github.com/474420502/focus/stack"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,7 @@
|
|||||||
package lastack
|
package lastack
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"focus/stack"
|
"github.com/474420502/focus/stack"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -20,6 +19,7 @@ func assertImplementation() {
|
|||||||
var _ stack.IStack = (*Stack)(nil)
|
var _ stack.IStack = (*Stack)(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func New() *Stack {
|
func New() *Stack {
|
||||||
s := &Stack{}
|
s := &Stack{}
|
||||||
s.size = 0
|
s.size = 0
|
||||||
|
|||||||
@ -3,8 +3,8 @@ package avl
|
|||||||
import (
|
import (
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
"focus/tree"
|
"github.com/474420502/focus/tree"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
|
|||||||
@ -7,8 +7,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package avl
|
package avl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
lastack "focus/stack/listarraystack"
|
lastack "github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIerator(t *testing.T) {
|
func TestIerator(t *testing.T) {
|
||||||
|
|||||||
@ -3,8 +3,8 @@ package avldup
|
|||||||
import (
|
import (
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
"focus/tree"
|
"github.com/474420502/focus/tree"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
|
|||||||
@ -7,8 +7,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package avldup
|
package avldup
|
||||||
|
|
||||||
import (
|
import (
|
||||||
lastack "focus/stack/listarraystack"
|
lastack "github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIerator(t *testing.T) {
|
func TestIerator(t *testing.T) {
|
||||||
|
|||||||
@ -3,8 +3,8 @@ package avlkey
|
|||||||
import (
|
import (
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
"focus/tree"
|
"github.com/474420502/focus/tree"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
|
|||||||
@ -7,8 +7,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package avlkey
|
package avlkey
|
||||||
|
|
||||||
import (
|
import (
|
||||||
lastack "focus/stack/listarraystack"
|
lastack "github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIerator(t *testing.T) {
|
func TestIerator(t *testing.T) {
|
||||||
|
|||||||
@ -3,8 +3,8 @@ package avlkeydup
|
|||||||
import (
|
import (
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
"focus/tree"
|
"github.com/474420502/focus/tree"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
|
|||||||
@ -7,8 +7,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package avlkeydup
|
package avlkeydup
|
||||||
|
|
||||||
import (
|
import (
|
||||||
lastack "focus/stack/listarraystack"
|
lastack "github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIerator(t *testing.T) {
|
func TestIerator(t *testing.T) {
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package heap
|
package heap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tree struct {
|
type Tree struct {
|
||||||
|
|||||||
@ -4,8 +4,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
|
|
||||||
"github.com/Pallinder/go-randomdata"
|
"github.com/Pallinder/go-randomdata"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -1,252 +0,0 @@
|
|||||||
package lsv
|
|
||||||
|
|
||||||
import (
|
|
||||||
lastack "focus/stack/listarraystack"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Iterator struct {
|
|
||||||
dir int
|
|
||||||
up *DNode
|
|
||||||
cur *DNode
|
|
||||||
tstack *lastack.Stack
|
|
||||||
// curnext *Node
|
|
||||||
}
|
|
||||||
|
|
||||||
func initIterator(avltree *DTree) *Iterator {
|
|
||||||
iter := &Iterator{tstack: lastack.New()}
|
|
||||||
iter.up = avltree.root
|
|
||||||
return iter
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewIterator(n *DNode) *Iterator {
|
|
||||||
iter := &Iterator{tstack: lastack.New()}
|
|
||||||
iter.up = n
|
|
||||||
return iter
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewIteratorWithCap(n *DNode, cap int) *Iterator {
|
|
||||||
iter := &Iterator{tstack: lastack.NewWithCap(cap)}
|
|
||||||
iter.up = n
|
|
||||||
return iter
|
|
||||||
}
|
|
||||||
|
|
||||||
func (iter *Iterator) GetNode() *DNode {
|
|
||||||
return iter.cur
|
|
||||||
}
|
|
||||||
|
|
||||||
func (iter *Iterator) ToHead() {
|
|
||||||
if iter.cur == nil {
|
|
||||||
iter.cur = iter.up
|
|
||||||
}
|
|
||||||
|
|
||||||
for iter.cur.family[0] != nil {
|
|
||||||
iter.cur = iter.cur.family[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
for iter.cur.family[1] != nil {
|
|
||||||
iter.cur = iter.cur.family[1]
|
|
||||||
}
|
|
||||||
iter.SetNode(iter.cur)
|
|
||||||
iter.cur = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (iter *Iterator) ToTail() {
|
|
||||||
|
|
||||||
if iter.cur == nil {
|
|
||||||
iter.cur = iter.up
|
|
||||||
}
|
|
||||||
|
|
||||||
for iter.cur.family[0] != nil {
|
|
||||||
iter.cur = iter.cur.family[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
for iter.cur.family[2] != nil {
|
|
||||||
iter.cur = iter.cur.family[2]
|
|
||||||
}
|
|
||||||
iter.SetNode(iter.cur)
|
|
||||||
iter.cur = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (iter *Iterator) SetNode(n *DNode) {
|
|
||||||
iter.up = n
|
|
||||||
iter.dir = 0
|
|
||||||
iter.tstack.Clear()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (iter *Iterator) Key() interface{} {
|
|
||||||
return iter.cur.key
|
|
||||||
}
|
|
||||||
|
|
||||||
func (iter *Iterator) Value() interface{} {
|
|
||||||
return iter.cur.value
|
|
||||||
}
|
|
||||||
|
|
||||||
func (iter *Iterator) GetNext(cur *DNode, idx int) *DNode {
|
|
||||||
|
|
||||||
// iter := NewIterator(cur)
|
|
||||||
iter.SetNode(cur)
|
|
||||||
iter.curPushNextStack(iter.up)
|
|
||||||
iter.up = iter.getNextUp(iter.up)
|
|
||||||
|
|
||||||
for i := 0; i < idx; i++ {
|
|
||||||
|
|
||||||
if iter.tstack.Size() == 0 {
|
|
||||||
if iter.up == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
iter.tstack.Push(iter.up)
|
|
||||||
iter.up = iter.getNextUp(iter.up)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v, ok := iter.tstack.Pop(); ok {
|
|
||||||
iter.cur = v.(*DNode)
|
|
||||||
if i == idx-1 {
|
|
||||||
return iter.cur
|
|
||||||
}
|
|
||||||
iter.curPushNextStack(iter.cur)
|
|
||||||
} else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return cur
|
|
||||||
}
|
|
||||||
|
|
||||||
func (iter *Iterator) Next() (result bool) {
|
|
||||||
|
|
||||||
if iter.dir > -1 {
|
|
||||||
if iter.dir == 1 && iter.cur != nil {
|
|
||||||
iter.tstack.Clear()
|
|
||||||
iter.curPushNextStack(iter.cur)
|
|
||||||
iter.up = iter.getNextUp(iter.cur)
|
|
||||||
}
|
|
||||||
iter.dir = -1
|
|
||||||
}
|
|
||||||
|
|
||||||
if iter.tstack.Size() == 0 {
|
|
||||||
if iter.up == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
iter.tstack.Push(iter.up)
|
|
||||||
iter.up = iter.getNextUp(iter.up)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v, ok := iter.tstack.Pop(); ok {
|
|
||||||
iter.cur = v.(*DNode)
|
|
||||||
iter.curPushNextStack(iter.cur)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
func (iter *Iterator) GetPrev(cur *DNode, idx int) *DNode {
|
|
||||||
|
|
||||||
// iter := NewIterator(cur)
|
|
||||||
iter.SetNode(cur)
|
|
||||||
iter.curPushPrevStack(iter.up)
|
|
||||||
iter.up = iter.getPrevUp(iter.up)
|
|
||||||
|
|
||||||
for i := 0; i < idx; i++ {
|
|
||||||
|
|
||||||
if iter.tstack.Size() == 0 {
|
|
||||||
if iter.up == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
iter.tstack.Push(iter.up)
|
|
||||||
iter.up = iter.getPrevUp(iter.up)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v, ok := iter.tstack.Pop(); ok {
|
|
||||||
iter.cur = v.(*DNode)
|
|
||||||
if i == idx-1 {
|
|
||||||
return iter.cur
|
|
||||||
}
|
|
||||||
iter.curPushPrevStack(iter.cur)
|
|
||||||
} else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return cur
|
|
||||||
}
|
|
||||||
|
|
||||||
func (iter *Iterator) Prev() (result bool) {
|
|
||||||
|
|
||||||
if iter.dir < 1 { // 非 1(next 方向定义 -1 为 prev)
|
|
||||||
if iter.dir == -1 && iter.cur != nil { // 如果上次为prev方向, 则清空辅助计算的栈
|
|
||||||
iter.tstack.Clear()
|
|
||||||
iter.curPushPrevStack(iter.cur) // 把当前cur计算的回朔
|
|
||||||
iter.up = iter.getPrevUp(iter.cur) // cur 寻找下个要计算up
|
|
||||||
}
|
|
||||||
iter.dir = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果栈空了, 把up的递归计算入栈, 重新计算 下次的up值
|
|
||||||
if iter.tstack.Size() == 0 {
|
|
||||||
if iter.up == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
iter.tstack.Push(iter.up)
|
|
||||||
iter.up = iter.getPrevUp(iter.up)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v, ok := iter.tstack.Pop(); ok {
|
|
||||||
iter.cur = v.(*DNode)
|
|
||||||
iter.curPushPrevStack(iter.cur)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果再次计算的栈为空, 则只能返回false
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func getRelationship(cur *DNode) int {
|
|
||||||
if cur.family[0].family[2] == cur {
|
|
||||||
return 2
|
|
||||||
}
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (iter *Iterator) getPrevUp(cur *DNode) *DNode {
|
|
||||||
for cur.family[0] != nil {
|
|
||||||
if getRelationship(cur) == 1 { // next 在 降序 小值. 如果child在右边, parent 比 child 小, parent才有效, 符合降序
|
|
||||||
return cur.family[0]
|
|
||||||
}
|
|
||||||
cur = cur.family[0]
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (iter *Iterator) curPushPrevStack(cur *DNode) {
|
|
||||||
Prev := cur.family[1] // 当前的左然后向右找, 找到最大, 就是最接近cur 并且小于cur的值
|
|
||||||
|
|
||||||
if Prev != nil {
|
|
||||||
iter.tstack.Push(Prev)
|
|
||||||
for Prev.family[2] != nil {
|
|
||||||
Prev = Prev.family[2]
|
|
||||||
iter.tstack.Push(Prev) // 入栈 用于回溯
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (iter *Iterator) getNextUp(cur *DNode) *DNode {
|
|
||||||
for cur.family[0] != nil {
|
|
||||||
if getRelationship(cur) == 0 { // Prev 在 降序 大值. 如果child在左边, parent 比 child 大, parent才有效 , 符合降序
|
|
||||||
return cur.family[0]
|
|
||||||
}
|
|
||||||
cur = cur.family[0]
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (iter *Iterator) curPushNextStack(cur *DNode) {
|
|
||||||
next := cur.family[2]
|
|
||||||
|
|
||||||
if next != nil {
|
|
||||||
iter.tstack.Push(next)
|
|
||||||
for next.family[1] != nil {
|
|
||||||
next = next.family[1]
|
|
||||||
iter.tstack.Push(next)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
1094
tree/lsv/dtree.go
1094
tree/lsv/dtree.go
File diff suppressed because it is too large
Load Diff
@ -1,28 +0,0 @@
|
|||||||
package lsv
|
|
||||||
|
|
||||||
// func TestIerator(t *testing.T) {
|
|
||||||
// tree := New(compare.Int)
|
|
||||||
// l := []int{5, 10, 100, 30, 40, 70, 45, 35, 23}
|
|
||||||
// for _, v := range l {
|
|
||||||
// tree.Put(v, v)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// sort.Ints(l)
|
|
||||||
|
|
||||||
// iter := tree.Iterator()
|
|
||||||
// iter.ToHead()
|
|
||||||
// for i := 0; iter.Next(); i++ {
|
|
||||||
|
|
||||||
// if iter.Value() != l[i] {
|
|
||||||
// t.Error(iter.Value(), l[i])
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// iter.ToTail()
|
|
||||||
// iter.Prev()
|
|
||||||
// for i := len(l) - 1; iter.Next(); i-- {
|
|
||||||
|
|
||||||
// if iter.Value() != l[i] {
|
|
||||||
// t.Error(iter.Value(), l[i])
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
@ -1,514 +0,0 @@
|
|||||||
package lsv
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
|
||||||
)
|
|
||||||
|
|
||||||
// INode 用于索引的节点
|
|
||||||
type INode struct {
|
|
||||||
family [3]*INode
|
|
||||||
size int
|
|
||||||
tree *DTree
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewINode 生成inode节点
|
|
||||||
func NewINode() *INode {
|
|
||||||
inode := &INode{}
|
|
||||||
inode.size = 1
|
|
||||||
inode.tree = newDataTree(compareRunes)
|
|
||||||
return inode
|
|
||||||
}
|
|
||||||
|
|
||||||
// ITree 用于索引的树
|
|
||||||
type ITree struct {
|
|
||||||
root *INode
|
|
||||||
limit int
|
|
||||||
Compare func(s1, s2 []rune) int
|
|
||||||
}
|
|
||||||
|
|
||||||
// New 生成一颗索引树
|
|
||||||
func New(Compare func(s1, s2 []rune) int) *ITree {
|
|
||||||
return &ITree{Compare: Compare, limit: 10}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put return bool
|
|
||||||
func (tree *ITree) Put(key, value []rune) (isInsert bool) {
|
|
||||||
|
|
||||||
// node := &INode{key: key, value: value, size: 1}
|
|
||||||
if tree.root == nil {
|
|
||||||
|
|
||||||
tree.root = NewINode()
|
|
||||||
return tree.root.tree.Put(key, value)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for cur := tree.root; ; {
|
|
||||||
|
|
||||||
if cur.size > 8 {
|
|
||||||
factor := cur.size >> 3 // or factor = 1
|
|
||||||
ls, rs := cur.family[1].size, cur.family[2].size
|
|
||||||
if rs >= (ls<<1)+factor || ls >= (rs<<1)+factor {
|
|
||||||
tree.ifixSize(cur, ls, rs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
c := tree.Compare(key, cur.tree.feature.key)
|
|
||||||
switch {
|
|
||||||
case c < 0:
|
|
||||||
if cur.family[1] == nil {
|
|
||||||
|
|
||||||
if cur.tree.root.size >= tree.limit {
|
|
||||||
// 子树的节点分解 操作
|
|
||||||
lspilt := cur.tree.root.family[1]
|
|
||||||
rspilt := cur.tree.root.family[2]
|
|
||||||
rspilt.family[0] = nil //清空右节点的父类, rsplit为根
|
|
||||||
lspilt.family[0] = nil // 上
|
|
||||||
|
|
||||||
if cur.family[1].size < cur.family[2].size {
|
|
||||||
ilnode := NewINode()
|
|
||||||
ilnode.tree.root = lspilt
|
|
||||||
}
|
|
||||||
|
|
||||||
// 根树 新节点要插入到 右节点的最小值位置 即是 左~
|
|
||||||
irnode := NewINode()
|
|
||||||
irnode.tree.root = rspilt
|
|
||||||
irnode.tree.feature = cur.tree.feature // 右树最大值
|
|
||||||
|
|
||||||
icur := cur.family[2]
|
|
||||||
|
|
||||||
if icur == nil {
|
|
||||||
|
|
||||||
cur.family[2] = irnode
|
|
||||||
irnode.family[0] = cur
|
|
||||||
|
|
||||||
icur = cur
|
|
||||||
} else {
|
|
||||||
|
|
||||||
for ; icur.family[1] != nil; icur = icur.family[1] {
|
|
||||||
} // 找右最左的值为最小值节点处
|
|
||||||
|
|
||||||
icur.family[1] = irnode // 挂接分离的右节点
|
|
||||||
irnode.family[0] = icur
|
|
||||||
}
|
|
||||||
|
|
||||||
for temp := icur; temp != nil; temp = temp.family[0] {
|
|
||||||
temp.size++
|
|
||||||
} // 往上加+1 达到每个节点能统计size正确
|
|
||||||
|
|
||||||
// 调整3节点失衡的情况
|
|
||||||
if icur.family[0] != nil && icur.family[0].size == 3 {
|
|
||||||
if icur.family[0].family[1] == nil {
|
|
||||||
tree.ilrrotate3(icur.family[0])
|
|
||||||
} else {
|
|
||||||
tree.irrotate3(icur.family[0])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tempRoot := cur.tree.root
|
|
||||||
cur.tree.root = lspilt
|
|
||||||
|
|
||||||
tempRoot.size = 1
|
|
||||||
for i := 1; i < len(tempRoot.family); i++ {
|
|
||||||
tempRoot.family[i] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
cur.tree.putfeature(tempRoot)
|
|
||||||
}
|
|
||||||
|
|
||||||
return cur.tree.Put(key, value)
|
|
||||||
}
|
|
||||||
cur = cur.family[1]
|
|
||||||
case c > 0:
|
|
||||||
if cur.family[2] == nil {
|
|
||||||
|
|
||||||
if cur.tree.root.size >= tree.limit {
|
|
||||||
lspilt := cur.tree.root.family[1]
|
|
||||||
rspilt := cur.tree.root.family[2]
|
|
||||||
rspilt.family[0] = nil //清空右节点的父类, rsplit为根
|
|
||||||
lspilt.family[0] = nil // 上
|
|
||||||
|
|
||||||
// cur.family
|
|
||||||
irnode := NewINode()
|
|
||||||
irnode.tree.root = rspilt
|
|
||||||
|
|
||||||
cur.family[2] = irnode
|
|
||||||
irnode.family[0] = cur
|
|
||||||
icur := cur
|
|
||||||
|
|
||||||
for temp := icur; temp != nil; temp = temp.family[0] {
|
|
||||||
temp.size++
|
|
||||||
} // 往上加+1 达到每个节点能统计size正确
|
|
||||||
|
|
||||||
// 调整3节点失衡的情况
|
|
||||||
if icur.family[0] != nil && icur.family[0].size == 3 {
|
|
||||||
if icur.family[0].family[2] == nil {
|
|
||||||
tree.irlrotate3(icur.family[0])
|
|
||||||
} else {
|
|
||||||
tree.ilrotate3(icur.family[0])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// cur.family irnode
|
|
||||||
|
|
||||||
tempRoot := cur.tree.root
|
|
||||||
|
|
||||||
cur.tree.root = lspilt
|
|
||||||
|
|
||||||
tempRoot.size = 1
|
|
||||||
for i := 1; i < len(tempRoot.family); i++ {
|
|
||||||
tempRoot.family[i] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
cur.tree.putfeature(tempRoot)
|
|
||||||
}
|
|
||||||
|
|
||||||
return cur.tree.Put(key, value)
|
|
||||||
}
|
|
||||||
cur = cur.family[2]
|
|
||||||
default:
|
|
||||||
return cur.tree.Put(key, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tree *ITree) String() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tree *ITree) ifixSize(cur *INode, ls, rs int) {
|
|
||||||
if ls > rs {
|
|
||||||
llsize, lrsize := igetChildrenSize(cur.family[1])
|
|
||||||
if lrsize > llsize {
|
|
||||||
tree.irlrotate(cur)
|
|
||||||
} else {
|
|
||||||
tree.irrotate(cur)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
rlsize, rrsize := igetChildrenSize(cur.family[2])
|
|
||||||
if rlsize > rrsize {
|
|
||||||
tree.ilrrotate(cur)
|
|
||||||
} else {
|
|
||||||
tree.ilrotate(cur)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tree *ITree) ilrrotate3(cur *INode) {
|
|
||||||
const l = 2
|
|
||||||
const r = 1
|
|
||||||
|
|
||||||
movparent := cur.family[l]
|
|
||||||
mov := movparent.family[r]
|
|
||||||
|
|
||||||
mov.tree, cur.tree = cur.tree, mov.tree //交换值达到, 相对位移
|
|
||||||
|
|
||||||
cur.family[r] = mov
|
|
||||||
mov.family[0] = cur
|
|
||||||
|
|
||||||
cur.family[l] = movparent
|
|
||||||
movparent.family[r] = nil
|
|
||||||
|
|
||||||
cur.family[r] = mov
|
|
||||||
mov.family[0] = cur
|
|
||||||
|
|
||||||
cur.family[l].size = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tree *ITree) ilrrotate(cur *INode) {
|
|
||||||
|
|
||||||
const l = 2
|
|
||||||
const r = 1
|
|
||||||
|
|
||||||
movparent := cur.family[l]
|
|
||||||
mov := movparent.family[r]
|
|
||||||
|
|
||||||
mov.tree, cur.tree = cur.tree, mov.tree //交换值达到, 相对位移
|
|
||||||
|
|
||||||
if mov.family[l] != nil {
|
|
||||||
movparent.family[r] = mov.family[l]
|
|
||||||
movparent.family[r].family[0] = movparent
|
|
||||||
//movparent.family[r].child = l
|
|
||||||
} else {
|
|
||||||
movparent.family[r] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if mov.family[r] != nil {
|
|
||||||
mov.family[l] = mov.family[r]
|
|
||||||
//mov.family[l].child = l
|
|
||||||
} else {
|
|
||||||
mov.family[l] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if cur.family[r] != nil {
|
|
||||||
mov.family[r] = cur.family[r]
|
|
||||||
mov.family[r].family[0] = mov
|
|
||||||
} else {
|
|
||||||
mov.family[r] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
cur.family[r] = mov
|
|
||||||
mov.family[0] = cur
|
|
||||||
|
|
||||||
movparent.size = igetChildrenSumSize(movparent) + 1
|
|
||||||
mov.size = igetChildrenSumSize(mov) + 1
|
|
||||||
cur.size = igetChildrenSumSize(cur) + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tree *ITree) irlrotate3(cur *INode) {
|
|
||||||
const l = 1
|
|
||||||
const r = 2
|
|
||||||
|
|
||||||
movparent := cur.family[l]
|
|
||||||
mov := movparent.family[r]
|
|
||||||
|
|
||||||
mov.tree, cur.tree = cur.tree, mov.tree //交换值达到, 相对位移
|
|
||||||
|
|
||||||
cur.family[r] = mov
|
|
||||||
mov.family[0] = cur
|
|
||||||
|
|
||||||
cur.family[l] = movparent
|
|
||||||
movparent.family[r] = nil
|
|
||||||
|
|
||||||
cur.family[r] = mov
|
|
||||||
mov.family[0] = cur
|
|
||||||
|
|
||||||
// cur.size = 3
|
|
||||||
// cur.family[r].size = 1
|
|
||||||
cur.family[l].size = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tree *ITree) irlrotate(cur *INode) {
|
|
||||||
|
|
||||||
const l = 1
|
|
||||||
const r = 2
|
|
||||||
|
|
||||||
movparent := cur.family[l]
|
|
||||||
mov := movparent.family[r]
|
|
||||||
|
|
||||||
mov.tree, cur.tree = cur.tree, mov.tree //交换值达到, 相对位移
|
|
||||||
|
|
||||||
if mov.family[l] != nil {
|
|
||||||
movparent.family[r] = mov.family[l]
|
|
||||||
movparent.family[r].family[0] = movparent
|
|
||||||
} else {
|
|
||||||
movparent.family[r] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if mov.family[r] != nil {
|
|
||||||
mov.family[l] = mov.family[r]
|
|
||||||
} else {
|
|
||||||
mov.family[l] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if cur.family[r] != nil {
|
|
||||||
mov.family[r] = cur.family[r]
|
|
||||||
mov.family[r].family[0] = mov
|
|
||||||
} else {
|
|
||||||
mov.family[r] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
cur.family[r] = mov
|
|
||||||
mov.family[0] = cur
|
|
||||||
|
|
||||||
movparent.size = igetChildrenSumSize(movparent) + 1
|
|
||||||
mov.size = igetChildrenSumSize(mov) + 1
|
|
||||||
cur.size = igetChildrenSumSize(cur) + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tree *ITree) irrotate3(cur *INode) {
|
|
||||||
const l = 1
|
|
||||||
const r = 2
|
|
||||||
// 1 right 0 left
|
|
||||||
mov := cur.family[l]
|
|
||||||
|
|
||||||
mov.tree, cur.tree = cur.tree, mov.tree //交换值达到, 相对位移
|
|
||||||
|
|
||||||
cur.family[r] = mov
|
|
||||||
|
|
||||||
cur.family[l] = mov.family[l]
|
|
||||||
cur.family[l].family[0] = cur
|
|
||||||
|
|
||||||
mov.family[l] = nil
|
|
||||||
|
|
||||||
mov.size = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tree *ITree) irrotate(cur *INode) {
|
|
||||||
|
|
||||||
const l = 1
|
|
||||||
const r = 2
|
|
||||||
// 1 right 0 left
|
|
||||||
mov := cur.family[l]
|
|
||||||
|
|
||||||
mov.tree, cur.tree = cur.tree, mov.tree //交换值达到, 相对位移
|
|
||||||
|
|
||||||
// mov.family[l]不可能为nil
|
|
||||||
mov.family[l].family[0] = cur
|
|
||||||
|
|
||||||
cur.family[l] = mov.family[l]
|
|
||||||
|
|
||||||
// 解决mov节点孩子转移的问题
|
|
||||||
if mov.family[r] != nil {
|
|
||||||
mov.family[l] = mov.family[r]
|
|
||||||
} else {
|
|
||||||
mov.family[l] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if cur.family[r] != nil {
|
|
||||||
mov.family[r] = cur.family[r]
|
|
||||||
mov.family[r].family[0] = mov
|
|
||||||
} else {
|
|
||||||
mov.family[r] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 连接转移后的节点 由于mov只是与cur交换值,parent不变
|
|
||||||
cur.family[r] = mov
|
|
||||||
|
|
||||||
mov.size = igetChildrenSumSize(mov) + 1
|
|
||||||
cur.size = igetChildrenSumSize(cur) + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tree *ITree) ilrotate3(cur *INode) {
|
|
||||||
const l = 2
|
|
||||||
const r = 1
|
|
||||||
// 1 right 0 left
|
|
||||||
mov := cur.family[l]
|
|
||||||
|
|
||||||
mov.tree, cur.tree = cur.tree, mov.tree //交换值达到, 相对位移
|
|
||||||
|
|
||||||
cur.family[r] = mov
|
|
||||||
|
|
||||||
cur.family[l] = mov.family[l]
|
|
||||||
cur.family[l].family[0] = cur
|
|
||||||
|
|
||||||
mov.family[l] = nil
|
|
||||||
|
|
||||||
mov.size = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tree *ITree) ilrotate(cur *INode) {
|
|
||||||
|
|
||||||
const l = 2
|
|
||||||
const r = 1
|
|
||||||
// 1 right 0 left
|
|
||||||
mov := cur.family[l]
|
|
||||||
|
|
||||||
mov.tree, cur.tree = cur.tree, mov.tree //交换值达到, 相对位移
|
|
||||||
|
|
||||||
// mov.family[l]不可能为nil
|
|
||||||
mov.family[l].family[0] = cur
|
|
||||||
|
|
||||||
cur.family[l] = mov.family[l]
|
|
||||||
|
|
||||||
// 解决mov节点孩子转移的问题
|
|
||||||
if mov.family[r] != nil {
|
|
||||||
mov.family[l] = mov.family[r]
|
|
||||||
} else {
|
|
||||||
mov.family[l] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if cur.family[r] != nil {
|
|
||||||
mov.family[r] = cur.family[r]
|
|
||||||
mov.family[r].family[0] = mov
|
|
||||||
} else {
|
|
||||||
mov.family[r] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 连接转移后的节点 由于mov只是与cur交换值,parent不变
|
|
||||||
cur.family[r] = mov
|
|
||||||
|
|
||||||
mov.size = igetChildrenSumSize(mov) + 1
|
|
||||||
cur.size = igetChildrenSumSize(cur) + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func igetChildrenSumSize(cur *INode) int {
|
|
||||||
return igetSize(cur.family[1]) + igetSize(cur.family[2])
|
|
||||||
}
|
|
||||||
|
|
||||||
func igetChildrenSize(cur *INode) (int, int) {
|
|
||||||
return igetSize(cur.family[1]), igetSize(cur.family[2])
|
|
||||||
}
|
|
||||||
|
|
||||||
func igetSize(cur *INode) int {
|
|
||||||
if cur == nil {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return cur.size
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tree *ITree) ifixSizeWithRemove(cur *INode) {
|
|
||||||
for cur != nil {
|
|
||||||
cur.size--
|
|
||||||
if cur.size > 8 {
|
|
||||||
factor := cur.size >> 3 // or factor = 1
|
|
||||||
ls, rs := igetChildrenSize(cur)
|
|
||||||
if rs >= (ls<<1)+factor || ls >= (rs<<1)+factor {
|
|
||||||
tree.ifixSize(cur, ls, rs)
|
|
||||||
}
|
|
||||||
} else if cur.size == 3 {
|
|
||||||
if cur.family[1] == nil {
|
|
||||||
if cur.family[2].family[1] == nil {
|
|
||||||
tree.ilrotate3(cur)
|
|
||||||
} else {
|
|
||||||
tree.ilrrotate3(cur)
|
|
||||||
}
|
|
||||||
} else if cur.family[2] == nil {
|
|
||||||
if cur.family[1].family[2] == nil {
|
|
||||||
tree.irrotate3(cur)
|
|
||||||
} else {
|
|
||||||
tree.irlrotate3(cur)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cur = cur.family[0]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ioutputfordebug(node *INode, prefix string, isTail bool, str *string) {
|
|
||||||
|
|
||||||
if node.family[2] != nil {
|
|
||||||
newPrefix := prefix
|
|
||||||
if isTail {
|
|
||||||
newPrefix += "│ "
|
|
||||||
} else {
|
|
||||||
newPrefix += " "
|
|
||||||
}
|
|
||||||
ioutputfordebug(node.family[2], newPrefix, false, str)
|
|
||||||
}
|
|
||||||
*str += prefix
|
|
||||||
if isTail {
|
|
||||||
*str += "└── "
|
|
||||||
} else {
|
|
||||||
*str += "┌── "
|
|
||||||
}
|
|
||||||
|
|
||||||
suffix := "("
|
|
||||||
parentv := ""
|
|
||||||
if node.family[0] == nil {
|
|
||||||
parentv = "nil"
|
|
||||||
} else {
|
|
||||||
parentv = spew.Sprint(string(node.family[0].tree.root.key[0:3]))
|
|
||||||
}
|
|
||||||
suffix += parentv + "|" + spew.Sprint(node.size) + ")"
|
|
||||||
*str += spew.Sprint(string(node.tree.root.key[0:3])) + suffix + "\n"
|
|
||||||
|
|
||||||
if node.family[1] != nil {
|
|
||||||
newPrefix := prefix
|
|
||||||
if isTail {
|
|
||||||
newPrefix += " "
|
|
||||||
} else {
|
|
||||||
newPrefix += "│ "
|
|
||||||
}
|
|
||||||
ioutputfordebug(node.family[1], newPrefix, true, str)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tree *ITree) debugString() string {
|
|
||||||
str := "VBTree-Dup\n"
|
|
||||||
if tree.root == nil {
|
|
||||||
return str + "nil"
|
|
||||||
}
|
|
||||||
ioutputfordebug(tree.root, "", true, &str)
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
@ -1,880 +0,0 @@
|
|||||||
package lsv
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sort"
|
|
||||||
"strconv"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/Pallinder/go-randomdata"
|
|
||||||
)
|
|
||||||
|
|
||||||
func loadTestData() [][]rune {
|
|
||||||
// data, err := ioutil.ReadFile("../l.log")
|
|
||||||
// if err != nil {
|
|
||||||
// log.Println(err)
|
|
||||||
// }
|
|
||||||
// var l []int
|
|
||||||
// decoder := gob.NewDecoder(bytes.NewReader(data))
|
|
||||||
// decoder.Decode(&l)
|
|
||||||
// return l
|
|
||||||
|
|
||||||
var result [][]rune
|
|
||||||
for i := 0; i < 200000; i++ {
|
|
||||||
c := randomdata.FullDate() + randomdata.RandStringRunes(6)
|
|
||||||
result = append(result, []rune(c))
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
// func TestIteratorHeadTail(t *testing.T) {
|
|
||||||
// tree := New(compare.Int)
|
|
||||||
// l := []int{1, 7, 14, 14, 16, 17, 20, 30, 21, 40, 50, 3, 40, 40, 40, 15, 100}
|
|
||||||
// for _, v := range l {
|
|
||||||
// tree.Put(v, v)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// iter := tree.Iterator()
|
|
||||||
// iter.ToHead() // 从小到大
|
|
||||||
// iter.Next()
|
|
||||||
// if iter.Value() != 1 {
|
|
||||||
// t.Error("value error", iter.Value())
|
|
||||||
// }
|
|
||||||
|
|
||||||
// iter.ToTail()
|
|
||||||
// iter.Prev()
|
|
||||||
// if iter.Value() != 100 {
|
|
||||||
// t.Error("value error", iter.Value())
|
|
||||||
// }
|
|
||||||
|
|
||||||
// for n := 0; n < 500; n++ {
|
|
||||||
// tree := New(compare.Int)
|
|
||||||
// var results []int
|
|
||||||
// for i := 0; i < 50; i++ {
|
|
||||||
// v := randomdata.Number(0, 5000)
|
|
||||||
// if ok := tree.Put(v, v); ok {
|
|
||||||
// results = append(results, v)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// result := tree.GetAround(5001)
|
|
||||||
|
|
||||||
// iter = tree.Iterator()
|
|
||||||
// iter.ToTail()
|
|
||||||
// iter.Prev()
|
|
||||||
// if iter.Value() != result[0] {
|
|
||||||
// t.Error("ToTail error", result, iter.Value())
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetAround(-1)
|
|
||||||
|
|
||||||
// iter.ToHead()
|
|
||||||
// iter.Next()
|
|
||||||
// if iter.Value() != result[2] {
|
|
||||||
// t.Error("ToTail error", result, iter.Value())
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func TestLargePushRemove(t *testing.T) {
|
|
||||||
// for n := 0; n < 10; n++ {
|
|
||||||
// tree := New(compare.Int)
|
|
||||||
// var results []int
|
|
||||||
// for i := 0; i < 50000; i++ {
|
|
||||||
// v := randomdata.Number(0, 100000000)
|
|
||||||
// if ok := tree.Put(v, v); ok {
|
|
||||||
// results = append(results, v)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if tree.Size() != len(results) {
|
|
||||||
// t.Error("Szie error", tree.Size(), len(results))
|
|
||||||
// }
|
|
||||||
|
|
||||||
// for i := 0; i < len(results)-10; i++ {
|
|
||||||
// tree.Remove(results[i])
|
|
||||||
// }
|
|
||||||
|
|
||||||
// results = results[len(results)-10:]
|
|
||||||
// if tree.Size() != 10 {
|
|
||||||
// t.Error("Szie error")
|
|
||||||
// }
|
|
||||||
// sort.Slice(results, func(i, j int) bool {
|
|
||||||
// if results[i] < results[j] {
|
|
||||||
// return true
|
|
||||||
// }
|
|
||||||
// return false
|
|
||||||
// })
|
|
||||||
// if spew.Sprint(results) != spew.Sprint(tree.Values()) {
|
|
||||||
// t.Error("tree is error")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// for i := 0; i < 10; i++ {
|
|
||||||
// v1 := results[i]
|
|
||||||
// v2, ok := tree.Index(i)
|
|
||||||
// if !ok {
|
|
||||||
// t.Error("not ok")
|
|
||||||
// }
|
|
||||||
// if v1 != v2 {
|
|
||||||
// t.Error("v1(", v1, ") != v2(", v2, ")??")
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// tree.Clear()
|
|
||||||
// if tree.String() != "VBTree-Dup\nnil" {
|
|
||||||
// t.Error("tree String is error")
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func TestRemoveIndex(t *testing.T) {
|
|
||||||
// tree := New(compare.Int)
|
|
||||||
// l := []int{7, 14, 14, 14, 16, 1, 40, 15}
|
|
||||||
// for _, v := range l {
|
|
||||||
// tree.Put(v, v)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // [1 7 14 14 14 15 16 40]
|
|
||||||
// var result string
|
|
||||||
// result = spew.Sprint(tree.Values())
|
|
||||||
// if result != "[1 7 14 15 16 40]" {
|
|
||||||
// t.Error("result = ", result, " should be [1 7 14 15 16 40]")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// tree.RemoveIndex(3)
|
|
||||||
// result = spew.Sprint(tree.Values())
|
|
||||||
// if result != "[1 7 14 16 40]" {
|
|
||||||
// t.Error("result is error")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// tree.RemoveIndex(-1)
|
|
||||||
// result = spew.Sprint(tree.Values())
|
|
||||||
// if result != "[1 7 14 16]" {
|
|
||||||
// t.Error("result is error")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// tree.RemoveIndex(0)
|
|
||||||
// result = spew.Sprint(tree.Values())
|
|
||||||
// if result != "[7 14 16]" {
|
|
||||||
// t.Error("result is error")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if tree.Size() != 3 {
|
|
||||||
// t.Error("size is error")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// for tree.Size() != 0 {
|
|
||||||
// tree.RemoveIndex(0)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if tree.root != nil {
|
|
||||||
// t.Error("tree roor is not error")
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func TestIndexRange(t *testing.T) {
|
|
||||||
// tree := New(compare.Int)
|
|
||||||
// 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, v)
|
|
||||||
// }
|
|
||||||
// // [3 7 14 15 16 17 20 21 30 40 50]
|
|
||||||
// // t.Error(tree.Values(), tree.Size())
|
|
||||||
// // t.Error(tree.debugString())
|
|
||||||
|
|
||||||
// var result string
|
|
||||||
// result = spew.Sprint(tree.IndexRange(0, 5))
|
|
||||||
// if result != "[3 7 14 15 16 17] true" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = spew.Sprint(tree.IndexRange(2, 5))
|
|
||||||
// if result != "[14 15 16 17] true" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = spew.Sprint(tree.IndexRange(5, 100))
|
|
||||||
// if result != "[17 20 21 30 40 50] false" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = spew.Sprint(tree.IndexRange(10, 0)) // size = 11, index max = 10
|
|
||||||
// if result != "[50 40 30 21 20 17 16 15 14 7 3] true" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = spew.Sprint(tree.IndexRange(11, 0)) // size = 11, index max = 10
|
|
||||||
// if result != "[50 40 30 21 20 17 16 15 14 7 3] false" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = spew.Sprint(tree.IndexRange(4, 1)) // size = 11, index max = 10
|
|
||||||
// if result != "[16 15 14 7] true" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = spew.Sprint(tree.IndexRange(-1, -5)) // size = 11, index max = 10
|
|
||||||
// if result != "[50 40 30 21 20] true" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = spew.Sprint(tree.IndexRange(-1, -11)) // size = 11, index max = 0 - 10 (-1,-11)
|
|
||||||
// if result != "[50 40 30 21 20 17 16 15 14 7 3] true" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = spew.Sprint(tree.IndexRange(-1, -12)) // size = 11, index max = 0 - 10 (-1,-11)
|
|
||||||
// if result != "[50 40 30 21 20 17 16 15 14 7 3] false" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = spew.Sprint(tree.IndexRange(-5, -1)) // size = 11, index max = 0 - 10 (-1,-11)
|
|
||||||
// if result != "[20 21 30 40 50] true" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // [3 7 14 15 16 17 20 21 30 40 50]
|
|
||||||
// result = spew.Sprint(tree.IndexRange(-5, 5)) //
|
|
||||||
// if result != "[20 17] true" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = spew.Sprint(tree.IndexRange(5, -5)) //
|
|
||||||
// if result != "[17 20] true" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = spew.Sprint(tree.IndexRange(-1, 6)) //
|
|
||||||
// if result != "[50 40 30 21 20] true" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func TestGetAround(t *testing.T) {
|
|
||||||
// tree := New(compare.Int)
|
|
||||||
// for _, v := range []int{7, 14, 14, 14, 16, 17, 20, 30, 21, 40, 50, 3, 40, 40, 40, 15} {
|
|
||||||
// tree.Put(v, v)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// var Result string
|
|
||||||
|
|
||||||
// Result = spew.Sprint(tree.GetAround(17))
|
|
||||||
// if Result != "[16 17 20]" {
|
|
||||||
// t.Error(tree.Values())
|
|
||||||
// t.Error("17 is root, tree.GetAround(17)) is error", Result)
|
|
||||||
// t.Error(tree.debugString())
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Result = spew.Sprint(tree.GetAround(3))
|
|
||||||
// if Result != "[<nil> 3 7]" {
|
|
||||||
// t.Error(tree.Values())
|
|
||||||
// t.Error("tree.GetAround(3)) is error", Result)
|
|
||||||
// t.Error(tree.debugString())
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Result = spew.Sprint(tree.GetAround(40))
|
|
||||||
// if Result != "[30 40 50]" {
|
|
||||||
// t.Error(tree.Values())
|
|
||||||
// t.Error("tree.GetAround(40)) is error", Result)
|
|
||||||
// t.Error(tree.debugString())
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Result = spew.Sprint(tree.GetAround(50))
|
|
||||||
// if Result != "[40 50 <nil>]" {
|
|
||||||
// t.Error(tree.Values())
|
|
||||||
// t.Error("tree.GetAround(50)) is error", Result)
|
|
||||||
// t.Error(tree.debugString())
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Result = spew.Sprint(tree.GetAround(18))
|
|
||||||
// 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> 7]" {
|
|
||||||
// t.Error(tree.Values())
|
|
||||||
// t.Error("5 is not in list, tree.GetAround(5)) is error", Result)
|
|
||||||
// t.Error(tree.debugString())
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Result = spew.Sprint(tree.GetAround(2))
|
|
||||||
// if Result != "[<nil> <nil> 3]" {
|
|
||||||
// t.Error(tree.Values())
|
|
||||||
// t.Error("2 is not in list, tree.GetAround(2)) is error", Result)
|
|
||||||
// t.Error(tree.debugString())
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Result = spew.Sprint(tree.GetAround(100))
|
|
||||||
// if Result != "[50 <nil> <nil>]" {
|
|
||||||
// t.Error(tree.Values())
|
|
||||||
// t.Error("50 is not in list, tree.GetAround(50)) is error", Result)
|
|
||||||
// t.Error(tree.debugString())
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // // for test error case
|
|
||||||
|
|
||||||
// // func TestPutComparatorRandom(t *testing.T) {
|
|
||||||
|
|
||||||
// // for n := 0; n < 300000; n++ {
|
|
||||||
// // tree := New(compare.Int)
|
|
||||||
// // godsavl := avltree.NewWithIntComparator()
|
|
||||||
|
|
||||||
// // content := ""
|
|
||||||
// // m := make(map[int]int)
|
|
||||||
// // for i := 0; len(m) < 10; i++ {
|
|
||||||
// // v := randomdata.Number(0, 65535)
|
|
||||||
// // if _, ok := m[v]; !ok {
|
|
||||||
// // m[v] = v
|
|
||||||
// // content += spew.Sprint(v) + ","
|
|
||||||
// // tree.Put(v, v)
|
|
||||||
// // godsavl.Put(v, v)
|
|
||||||
// // }
|
|
||||||
// // }
|
|
||||||
|
|
||||||
// // s1 := spew.Sprint(tree.Values())
|
|
||||||
// // s2 := spew.Sprint(godsavl.Values())
|
|
||||||
|
|
||||||
// // if s1 != s2 {
|
|
||||||
// // t.Error(godsavl.String())
|
|
||||||
// // t.Error(tree.debugString())
|
|
||||||
// // t.Error(content, n)
|
|
||||||
// // break
|
|
||||||
// // }
|
|
||||||
// // }
|
|
||||||
// // }
|
|
||||||
|
|
||||||
func TestPut(t *testing.T) {
|
|
||||||
tree := New(compareRunes)
|
|
||||||
temp := ""
|
|
||||||
for i := 0; i < 10000; i++ {
|
|
||||||
v := strconv.Itoa(i) + ":" + randomdata.FullDate() + randomdata.Street()
|
|
||||||
// v := strconv.Itoa(i)
|
|
||||||
tree.Put([]rune(v), []rune(v))
|
|
||||||
|
|
||||||
if temp != tree.debugString() {
|
|
||||||
t.Error(tree.debugString())
|
|
||||||
temp = tree.debugString()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
t.Error("123")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestValues(t *testing.T) {
|
|
||||||
tree := newDataTree(compareRunes)
|
|
||||||
var testdata = []string{"abc", "bca", "12xx", "ABC", "你好"}
|
|
||||||
|
|
||||||
for _, v := range testdata {
|
|
||||||
tree.PutString(v, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Strings(testdata)
|
|
||||||
for i, v := range tree.Values() {
|
|
||||||
if testdata[i] != string(v) {
|
|
||||||
t.Error(string(v))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGet(t *testing.T) {
|
|
||||||
tree := newDataTree(compareRunes)
|
|
||||||
var testdata = []string{"asdb", "bsas", "asdba"}
|
|
||||||
for _, v := range testdata {
|
|
||||||
tree.PutString(v, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, v := range testdata {
|
|
||||||
r, ok := tree.GetString(v)
|
|
||||||
if !ok && r != v {
|
|
||||||
t.Error("the val not found ", v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if v, ok := tree.Get([]rune("bsasa")); ok {
|
|
||||||
t.Error("the val(1000) is not in tree, but is found", v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// func TestDupGetRange(t *testing.T) {
|
|
||||||
// tree := New(compare.Int)
|
|
||||||
// for _, v := range []int{5, 10, 13, 5, 17, 1, 2, 10, 40, 30, 1} {
|
|
||||||
// tree.Put(v, v)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result := tree.GetRange(0, 20)
|
|
||||||
// if spew.Sprint(result) != "[1 2 5 10 13 17]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetRange(0, 8)
|
|
||||||
// if spew.Sprint(result) != "[1 2 5]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetRange(-5, -1)
|
|
||||||
// if spew.Sprint(result) != "[]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetRange(7, 20)
|
|
||||||
// if spew.Sprint(result) != "[10 13 17]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetRange(30, 40)
|
|
||||||
// if spew.Sprint(result) != "[30 40]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetRange(30, 60)
|
|
||||||
// if spew.Sprint(result) != "[30 40]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetRange(40, 40)
|
|
||||||
// if spew.Sprint(result) != "[40]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetRange(50, 60)
|
|
||||||
// if spew.Sprint(result) != "[]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetRange(50, 1)
|
|
||||||
// if spew.Sprint(result) != "[40 30 17 13 10 5 2 1]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetRange(30, 20)
|
|
||||||
// if spew.Sprint(result) != "[30]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func TestGetRange(t *testing.T) {
|
|
||||||
// tree := New(compare.Int)
|
|
||||||
// for _, v := range []int{5, 6, 8, 10, 13, 17, 1, 2, 40, 30} {
|
|
||||||
// tree.Put(v, v)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // t.Error(tree.debugString())
|
|
||||||
// // t.Error(tree.getArountNode(20))
|
|
||||||
// // t.Error(tree.Values())
|
|
||||||
|
|
||||||
// result := tree.GetRange(0, 20)
|
|
||||||
// if spew.Sprint(result) != "[1 2 5 6 8 10 13 17]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetRange(-5, -1)
|
|
||||||
// if spew.Sprint(result) != "[]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetRange(7, 20)
|
|
||||||
// if spew.Sprint(result) != "[8 10 13 17]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetRange(30, 40)
|
|
||||||
// if spew.Sprint(result) != "[30 40]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetRange(30, 60)
|
|
||||||
// if spew.Sprint(result) != "[30 40]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetRange(40, 40)
|
|
||||||
// if spew.Sprint(result) != "[40]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetRange(50, 60)
|
|
||||||
// if spew.Sprint(result) != "[]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetRange(50, 1)
|
|
||||||
// if spew.Sprint(result) != "[40 30 17 13 10 8 6 5 2 1]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// result = tree.GetRange(30, 20)
|
|
||||||
// if spew.Sprint(result) != "[30]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func TestTravalsal(t *testing.T) {
|
|
||||||
// tree := New(compare.Int)
|
|
||||||
// for _, v := range []int{5, 6, 8, 10, 13, 17, 1, 2, 40, 30} {
|
|
||||||
// tree.Put(v, v)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// i := 0
|
|
||||||
// var result []interface{}
|
|
||||||
// tree.Traversal(func(k, v interface{}) bool {
|
|
||||||
// result = append(result, k)
|
|
||||||
// i++
|
|
||||||
// if i >= 10 {
|
|
||||||
// return false
|
|
||||||
// }
|
|
||||||
// return true
|
|
||||||
// })
|
|
||||||
|
|
||||||
// if spew.Sprint(result) != "[1 2 5 6 8 10 13 17 30 40]" {
|
|
||||||
// t.Error(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func TestRemoveAll(t *testing.T) {
|
|
||||||
// ALL:
|
|
||||||
// for c := 0; c < 5000; c++ {
|
|
||||||
// tree := New(compare.Int)
|
|
||||||
// gods := avltree.NewWithIntComparator()
|
|
||||||
// var l []int
|
|
||||||
// m := make(map[int]int)
|
|
||||||
|
|
||||||
// for i := 0; len(l) < 50; i++ {
|
|
||||||
// v := randomdata.Number(0, 100000)
|
|
||||||
// if _, ok := m[v]; !ok {
|
|
||||||
// m[v] = v
|
|
||||||
// l = append(l, v)
|
|
||||||
// tree.Put(v, v)
|
|
||||||
// gods.Put(v, v)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// for i := 0; i < 50; i++ {
|
|
||||||
|
|
||||||
// tree.Remove(l[i])
|
|
||||||
// gods.Remove(l[i])
|
|
||||||
|
|
||||||
// s1 := spew.Sprint(tree.Values())
|
|
||||||
// s2 := spew.Sprint(gods.Values())
|
|
||||||
// if s1 != s2 {
|
|
||||||
// t.Error("avl remove error", "avlsize = ", tree.Size())
|
|
||||||
// t.Error(tree.root, i, l[i])
|
|
||||||
// t.Error(s1)
|
|
||||||
// t.Error(s2)
|
|
||||||
// break ALL
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func TestRemove(t *testing.T) {
|
|
||||||
|
|
||||||
// ALL:
|
|
||||||
// for N := 0; N < 5000; N++ {
|
|
||||||
// tree := New(compare.Int)
|
|
||||||
// gods := avltree.NewWithIntComparator()
|
|
||||||
|
|
||||||
// var l []int
|
|
||||||
// m := make(map[int]int)
|
|
||||||
|
|
||||||
// for i := 0; len(l) < 20; i++ {
|
|
||||||
// v := randomdata.Number(0, 100)
|
|
||||||
// if _, ok := m[v]; !ok {
|
|
||||||
// l = append(l, v)
|
|
||||||
// m[v] = v
|
|
||||||
// tree.Put(v, v)
|
|
||||||
// gods.Put(v, v)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// src1 := tree.String()
|
|
||||||
// src2 := gods.String()
|
|
||||||
|
|
||||||
// for i := 0; i < 20; i++ {
|
|
||||||
// tree.Remove(l[i])
|
|
||||||
// gods.Remove(l[i])
|
|
||||||
// if tree.root != nil && spew.Sprint(gods.Values()) != spew.Sprint(tree.Values()) {
|
|
||||||
// t.Error(src1)
|
|
||||||
// t.Error(src2)
|
|
||||||
// t.Error(tree.debugString())
|
|
||||||
// t.Error(gods.String())
|
|
||||||
// t.Error(l[i])
|
|
||||||
// break ALL
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func BenchmarkGetRange(b *testing.B) {
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func BenchmarkIndexRange(b *testing.B) {
|
|
||||||
// tree := New(compare.Int)
|
|
||||||
// l := loadTestData()
|
|
||||||
// b.N = len(l)
|
|
||||||
|
|
||||||
// for _, v := range l {
|
|
||||||
// tree.Put(v, v)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// b.ResetTimer()
|
|
||||||
// b.StartTimer()
|
|
||||||
|
|
||||||
// execCount := 5
|
|
||||||
// b.N = len(l) * execCount
|
|
||||||
|
|
||||||
// for i := 0; i < execCount; i++ {
|
|
||||||
// for range l {
|
|
||||||
// tree.IndexRange(i, i+49)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func BenchmarkSkipListGet(b *testing.B) {
|
|
||||||
// sl := skiplist.New(skiplist.Int)
|
|
||||||
// l := loadTestData()
|
|
||||||
// b.N = len(l)
|
|
||||||
|
|
||||||
// for _, v := range l {
|
|
||||||
// sl.Set(v, v)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// b.ResetTimer()
|
|
||||||
// b.StartTimer()
|
|
||||||
|
|
||||||
// execCount := 5
|
|
||||||
// b.N = len(l) * execCount
|
|
||||||
|
|
||||||
// for i := 0; i < execCount; i++ {
|
|
||||||
// for _, v := range l {
|
|
||||||
// sl.Get(v)
|
|
||||||
// // e := sl.Get(v)
|
|
||||||
// // var result [50]interface{}
|
|
||||||
// // for i := 0; i < 50 && e != nil; i++ {
|
|
||||||
// // result[i] = e.Value
|
|
||||||
// // e = e.Next()
|
|
||||||
// // }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func BenchmarkSkipListSet(b *testing.B) {
|
|
||||||
|
|
||||||
// l := loadTestData()
|
|
||||||
// execCount := 1
|
|
||||||
// b.N = len(l) * execCount
|
|
||||||
// for i := 0; i < execCount; i++ {
|
|
||||||
// sl := skiplist.New(skiplist.Int)
|
|
||||||
// for _, v := range l {
|
|
||||||
// sl.Set(v, v)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func BenchmarkIterator(b *testing.B) {
|
|
||||||
// tree := New(compare.Int)
|
|
||||||
|
|
||||||
// l := loadTestData()
|
|
||||||
// b.N = len(l)
|
|
||||||
|
|
||||||
// for _, v := range l {
|
|
||||||
// tree.Put(v, v)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// b.ResetTimer()
|
|
||||||
// b.StartTimer()
|
|
||||||
// iter := tree.Iterator()
|
|
||||||
// b.N = 0
|
|
||||||
// for iter.Next() {
|
|
||||||
// b.N++
|
|
||||||
// }
|
|
||||||
// for iter.Prev() {
|
|
||||||
// b.N++
|
|
||||||
// }
|
|
||||||
// for iter.Next() {
|
|
||||||
// b.N++
|
|
||||||
// }
|
|
||||||
// b.Log(b.N, len(l))
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func BenchmarkRemove(b *testing.B) {
|
|
||||||
// tree := New(compare.Int)
|
|
||||||
|
|
||||||
// l := loadTestData()
|
|
||||||
|
|
||||||
// b.N = len(l)
|
|
||||||
// for _, v := range l {
|
|
||||||
// tree.Put(v, v)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// b.ResetTimer()
|
|
||||||
// b.StartTimer()
|
|
||||||
|
|
||||||
// for i := 0; i < len(l); i++ {
|
|
||||||
// tree.Remove(l[i])
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func BenchmarkGodsRemove(b *testing.B) {
|
|
||||||
// tree := avltree.NewWithIntComparator()
|
|
||||||
|
|
||||||
// l := loadTestData()
|
|
||||||
|
|
||||||
// b.N = len(l)
|
|
||||||
// for _, v := range l {
|
|
||||||
// tree.Put(v, v)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// b.ResetTimer()
|
|
||||||
// b.StartTimer()
|
|
||||||
|
|
||||||
// for i := 0; i < len(l); i++ {
|
|
||||||
// tree.Remove(l[i])
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func BenchmarkGodsRBRemove(b *testing.B) {
|
|
||||||
// tree := redblacktree.NewWithIntComparator()
|
|
||||||
|
|
||||||
// l := loadTestData()
|
|
||||||
|
|
||||||
// b.N = len(l)
|
|
||||||
// for _, v := range l {
|
|
||||||
// tree.Put(v, v)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// b.ResetTimer()
|
|
||||||
// b.StartTimer()
|
|
||||||
|
|
||||||
// for i := 0; i < len(l); i++ {
|
|
||||||
// tree.Remove(l[i])
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func BenchmarkGet(b *testing.B) {
|
|
||||||
|
|
||||||
// tree := New(compare.Int)
|
|
||||||
|
|
||||||
// l := loadTestData()
|
|
||||||
// b.N = len(l)
|
|
||||||
// for i := 0; i < b.N; i++ {
|
|
||||||
// tree.Put(l[i], i)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// b.ResetTimer()
|
|
||||||
// b.StartTimer()
|
|
||||||
|
|
||||||
// execCount := 10
|
|
||||||
// b.N = len(l) * execCount
|
|
||||||
|
|
||||||
// for i := 0; i < execCount; i++ {
|
|
||||||
// for _, v := range l {
|
|
||||||
// tree.Get(v)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func BenchmarkGodsRBGet(b *testing.B) {
|
|
||||||
// tree := redblacktree.NewWithIntComparator()
|
|
||||||
|
|
||||||
// l := loadTestData()
|
|
||||||
// b.N = len(l)
|
|
||||||
// for i := 0; i < b.N; i++ {
|
|
||||||
// tree.Put(l[i], i)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// b.ResetTimer()
|
|
||||||
// b.StartTimer()
|
|
||||||
|
|
||||||
// execCount := 10
|
|
||||||
// b.N = len(l) * execCount
|
|
||||||
|
|
||||||
// for i := 0; i < execCount; i++ {
|
|
||||||
// for _, v := range l {
|
|
||||||
// tree.Get(v)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func BenchmarkGodsAvlGet(b *testing.B) {
|
|
||||||
// tree := avltree.NewWithIntComparator()
|
|
||||||
|
|
||||||
// l := loadTestData()
|
|
||||||
// b.N = len(l)
|
|
||||||
// for i := 0; i < b.N; i++ {
|
|
||||||
// tree.Put(l[i], i)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// b.ResetTimer()
|
|
||||||
// b.StartTimer()
|
|
||||||
|
|
||||||
// execCount := 10
|
|
||||||
// b.N = len(l) * execCount
|
|
||||||
|
|
||||||
// for i := 0; i < execCount; i++ {
|
|
||||||
// for _, v := range l {
|
|
||||||
// tree.Get(v)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
func BenchmarkPut(b *testing.B) {
|
|
||||||
l := loadTestData()
|
|
||||||
|
|
||||||
b.ResetTimer()
|
|
||||||
b.StartTimer()
|
|
||||||
|
|
||||||
execCount := 50
|
|
||||||
b.N = len(l) * execCount
|
|
||||||
for i := 0; i < execCount; i++ {
|
|
||||||
tree := newDataTree(compareRunes)
|
|
||||||
for _, v := range l {
|
|
||||||
tree.Put(v, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// func TestPutStable(t *testing.T) {
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func BenchmarkGodsRBPut(b *testing.B) {
|
|
||||||
// l := loadTestData()
|
|
||||||
|
|
||||||
// b.ResetTimer()
|
|
||||||
// b.StartTimer()
|
|
||||||
|
|
||||||
// execCount := 50
|
|
||||||
// b.N = len(l) * execCount
|
|
||||||
// for i := 0; i < execCount; i++ {
|
|
||||||
// tree := redblacktree.NewWithIntComparator()
|
|
||||||
// for _, v := range l {
|
|
||||||
// tree.Put(v, v)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func BenchmarkGodsPut(b *testing.B) {
|
|
||||||
// tree := avltree.NewWithIntComparator()
|
|
||||||
|
|
||||||
// l := loadTestData()
|
|
||||||
|
|
||||||
// b.ResetTimer()
|
|
||||||
// b.StartTimer()
|
|
||||||
|
|
||||||
// b.N = len(l)
|
|
||||||
// for _, v := range l {
|
|
||||||
// tree.Put(v, v)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
@ -1,7 +1,7 @@
|
|||||||
package vbt
|
package vbt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
lastack "focus/stack/listarraystack"
|
lastack "github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIerator(t *testing.T) {
|
func TestIerator(t *testing.T) {
|
||||||
|
|||||||
@ -3,8 +3,8 @@ package vbt
|
|||||||
import (
|
import (
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
"focus/tree"
|
"github.com/474420502/focus/tree"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
@ -443,9 +443,9 @@ func (tree *Tree) Put(key interface{}) {
|
|||||||
for cur := tree.root; ; {
|
for cur := tree.root; ; {
|
||||||
|
|
||||||
if cur.size > 8 {
|
if cur.size > 8 {
|
||||||
factor := cur.size >> 3 // or factor = 1
|
factor := cur.size / 10 // or factor = 1
|
||||||
ls, rs := cur.children[0].size, cur.children[1].size
|
ls, rs := cur.children[0].size, cur.children[1].size
|
||||||
if rs >= (ls<<1)+factor || ls >= (rs<<1)+factor {
|
if rs >= ls*2+factor || ls >= rs*2+factor {
|
||||||
tree.fixSize(cur, ls, rs)
|
tree.fixSize(cur, ls, rs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -874,9 +874,9 @@ func (tree *Tree) fixSizeWithRemove(cur *Node) {
|
|||||||
for cur != nil {
|
for cur != nil {
|
||||||
cur.size--
|
cur.size--
|
||||||
if cur.size > 8 {
|
if cur.size > 8 {
|
||||||
factor := cur.size >> 3 // or factor = 1
|
factor := cur.size / 10 // or factor = 1
|
||||||
ls, rs := getChildrenSize(cur)
|
ls, rs := getChildrenSize(cur)
|
||||||
if rs >= (ls<<1)+factor || ls >= (rs<<1)+factor {
|
if rs >= ls*2+factor || ls >= rs*2+factor {
|
||||||
tree.fixSize(cur, ls, rs)
|
tree.fixSize(cur, ls, rs)
|
||||||
}
|
}
|
||||||
} else if cur.size == 3 {
|
} else if cur.size == 3 {
|
||||||
|
|||||||
@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/Pallinder/go-randomdata"
|
"github.com/Pallinder/go-randomdata"
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadTestData() []int {
|
func loadTestData() []int {
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package vbtdup
|
package vbtdup
|
||||||
|
|
||||||
import (
|
import (
|
||||||
lastack "focus/stack/listarraystack"
|
lastack "github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIerator(t *testing.T) {
|
func TestIerator(t *testing.T) {
|
||||||
|
|||||||
@ -3,8 +3,8 @@ package vbtdup
|
|||||||
import (
|
import (
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
"focus/tree"
|
"github.com/474420502/focus/tree"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
@ -445,9 +445,9 @@ func (tree *Tree) Put(value interface{}) (isInsert bool) {
|
|||||||
for cur := tree.root; ; {
|
for cur := tree.root; ; {
|
||||||
|
|
||||||
if cur.size > 8 {
|
if cur.size > 8 {
|
||||||
factor := cur.size >> 3 // or factor = 1
|
factor := cur.size / 10 // or factor = 1
|
||||||
ls, rs := cur.children[0].size, cur.children[1].size
|
ls, rs := cur.children[0].size, cur.children[1].size
|
||||||
if rs >= (ls<<1)+factor || ls >= (rs<<1)+factor {
|
if rs >= ls*2+factor || ls >= rs*2+factor {
|
||||||
tree.fixSize(cur, ls, rs)
|
tree.fixSize(cur, ls, rs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -895,9 +895,9 @@ func (tree *Tree) fixSizeWithRemove(cur *Node) {
|
|||||||
for cur != nil {
|
for cur != nil {
|
||||||
cur.size--
|
cur.size--
|
||||||
if cur.size > 8 {
|
if cur.size > 8 {
|
||||||
factor := cur.size >> 3 // or factor = 1
|
factor := cur.size / 10 // or factor = 1
|
||||||
ls, rs := getChildrenSize(cur)
|
ls, rs := getChildrenSize(cur)
|
||||||
if rs >= (ls<<1)+factor || ls >= (rs<<1)+factor {
|
if rs >= ls*2+factor || ls >= rs*2+factor {
|
||||||
tree.fixSize(cur, ls, rs)
|
tree.fixSize(cur, ls, rs)
|
||||||
}
|
}
|
||||||
} else if cur.size == 3 {
|
} else if cur.size == 3 {
|
||||||
|
|||||||
@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/Pallinder/go-randomdata"
|
"github.com/Pallinder/go-randomdata"
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadTestData() []int {
|
func loadTestData() []int {
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package vbtkey
|
package vbtkey
|
||||||
|
|
||||||
import (
|
import (
|
||||||
lastack "focus/stack/listarraystack"
|
lastack "github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIerator(t *testing.T) {
|
func TestIerator(t *testing.T) {
|
||||||
|
|||||||
@ -3,8 +3,8 @@ package vbtkey
|
|||||||
import (
|
import (
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
"focus/tree"
|
"github.com/474420502/focus/tree"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
@ -446,9 +446,9 @@ func (tree *Tree) Put(key, value interface{}) {
|
|||||||
for cur := tree.root; ; {
|
for cur := tree.root; ; {
|
||||||
|
|
||||||
if cur.size > 8 {
|
if cur.size > 8 {
|
||||||
factor := cur.size >> 3 // or factor = 1
|
factor := cur.size / 10 // or factor = 1
|
||||||
ls, rs := cur.children[0].size, cur.children[1].size
|
ls, rs := cur.children[0].size, cur.children[1].size
|
||||||
if rs >= (ls<<1)+factor || ls >= (rs<<1)+factor {
|
if rs >= ls*2+factor || ls >= rs*2+factor {
|
||||||
tree.fixSize(cur, ls, rs)
|
tree.fixSize(cur, ls, rs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -884,9 +884,9 @@ func (tree *Tree) fixSizeWithRemove(cur *Node) {
|
|||||||
for cur != nil {
|
for cur != nil {
|
||||||
cur.size--
|
cur.size--
|
||||||
if cur.size > 8 {
|
if cur.size > 8 {
|
||||||
factor := cur.size >> 3 // or factor = 1
|
factor := cur.size / 10 // or factor = 1
|
||||||
ls, rs := getChildrenSize(cur)
|
ls, rs := getChildrenSize(cur)
|
||||||
if rs >= (ls<<1)+factor || ls >= (rs<<1)+factor {
|
if rs >= ls*2+factor || ls >= rs*2+factor {
|
||||||
tree.fixSize(cur, ls, rs)
|
tree.fixSize(cur, ls, rs)
|
||||||
}
|
}
|
||||||
} else if cur.size == 3 {
|
} else if cur.size == 3 {
|
||||||
|
|||||||
@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/Pallinder/go-randomdata"
|
"github.com/Pallinder/go-randomdata"
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadTestData() []int {
|
func loadTestData() []int {
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package vbtkeydup
|
package vbtkeydup
|
||||||
|
|
||||||
import (
|
import (
|
||||||
lastack "focus/stack/listarraystack"
|
lastack "github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIerator(t *testing.T) {
|
func TestIerator(t *testing.T) {
|
||||||
|
|||||||
@ -3,8 +3,8 @@ package vbtkeydup
|
|||||||
import (
|
import (
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
"focus/tree"
|
"github.com/474420502/focus/tree"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
@ -446,9 +446,9 @@ func (tree *Tree) Put(key, value interface{}) (isInsert bool) {
|
|||||||
for cur := tree.root; ; {
|
for cur := tree.root; ; {
|
||||||
|
|
||||||
if cur.size > 8 {
|
if cur.size > 8 {
|
||||||
factor := cur.size >> 3 // or factor = 1
|
factor := cur.size / 10 // or factor = 1
|
||||||
ls, rs := cur.children[0].size, cur.children[1].size
|
ls, rs := cur.children[0].size, cur.children[1].size
|
||||||
if rs >= (ls<<1)+factor || ls >= (rs<<1)+factor {
|
if rs >= ls*2+factor || ls >= rs*2+factor {
|
||||||
tree.fixSize(cur, ls, rs)
|
tree.fixSize(cur, ls, rs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -896,9 +896,9 @@ func (tree *Tree) fixSizeWithRemove(cur *Node) {
|
|||||||
for cur != nil {
|
for cur != nil {
|
||||||
cur.size--
|
cur.size--
|
||||||
if cur.size > 8 {
|
if cur.size > 8 {
|
||||||
factor := cur.size >> 3 // or factor = 1
|
factor := cur.size / 10 // or factor = 1
|
||||||
ls, rs := getChildrenSize(cur)
|
ls, rs := getChildrenSize(cur)
|
||||||
if rs >= (ls<<1)+factor || ls >= (rs<<1)+factor {
|
if rs >= ls*2+factor || ls >= rs*2+factor {
|
||||||
tree.fixSize(cur, ls, rs)
|
tree.fixSize(cur, ls, rs)
|
||||||
}
|
}
|
||||||
} else if cur.size == 3 {
|
} else if cur.size == 3 {
|
||||||
|
|||||||
@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/Pallinder/go-randomdata"
|
"github.com/Pallinder/go-randomdata"
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadTestData() []int {
|
func loadTestData() []int {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user