213
This commit is contained in:
parent
cbeafe0974
commit
b9fa01abdb
441
avl/avl.go
441
avl/avl.go
|
@ -1,9 +1,8 @@
|
|||
package avl
|
||||
|
||||
import (
|
||||
"474420502.top/eson/structure/compare"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
|
||||
"github.com/emirpasic/gods/utils"
|
||||
)
|
||||
|
||||
type Node struct {
|
||||
|
@ -26,13 +25,13 @@ func (n *Node) String() string {
|
|||
}
|
||||
|
||||
type Tree struct {
|
||||
root *Node
|
||||
size int
|
||||
comparator utils.Comparator
|
||||
root *Node
|
||||
size int
|
||||
compare compare.Compare
|
||||
}
|
||||
|
||||
func New(comparator utils.Comparator) *Tree {
|
||||
return &Tree{comparator: comparator}
|
||||
func New(compare compare.Compare) *Tree {
|
||||
return &Tree{compare: compare}
|
||||
}
|
||||
|
||||
func (avl *Tree) String() string {
|
||||
|
@ -118,18 +117,83 @@ func (avl *Tree) Remove(key interface{}) *Node {
|
|||
}
|
||||
|
||||
// Values 返回先序遍历的值
|
||||
func (avl *Tree) Values() []interface{} {
|
||||
func (tree *Tree) Values() []interface{} {
|
||||
mszie := 0
|
||||
if avl.root != nil {
|
||||
mszie = avl.size
|
||||
if tree.root != nil {
|
||||
mszie = tree.size
|
||||
}
|
||||
result := make([]interface{}, 0, mszie)
|
||||
avl.Traversal(func(v interface{}) {
|
||||
tree.Traversal(func(v interface{}) bool {
|
||||
result = append(result, v)
|
||||
}, DLR)
|
||||
return true
|
||||
}, LDR)
|
||||
return result
|
||||
}
|
||||
|
||||
func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
||||
c := tree.compare(k2, k1)
|
||||
switch c {
|
||||
case 1:
|
||||
|
||||
var min, max *Node
|
||||
resultmin := tree.getArountNode(k1)
|
||||
resultmax := tree.getArountNode(k2)
|
||||
for i := 1; i < 3 && min == nil; i++ {
|
||||
min = resultmin[i]
|
||||
}
|
||||
|
||||
for i := 1; i > -1 && max == nil; i-- {
|
||||
max = resultmax[i]
|
||||
}
|
||||
|
||||
if max == nil {
|
||||
return []interface{}{}
|
||||
}
|
||||
|
||||
result = make([]interface{}, 0, 16)
|
||||
|
||||
iter := NewIterator(min)
|
||||
for iter.Prev() {
|
||||
result = append(result, iter.Value())
|
||||
if iter.cur == max {
|
||||
break
|
||||
}
|
||||
}
|
||||
case -1:
|
||||
|
||||
var min, max *Node
|
||||
resultmin := tree.getArountNode(k2)
|
||||
resultmax := tree.getArountNode(k1)
|
||||
for i := 1; i < 3 && min == nil; i++ {
|
||||
min = resultmin[i]
|
||||
}
|
||||
for i := 1; i > -1 && max == nil; i-- {
|
||||
max = resultmax[i]
|
||||
}
|
||||
|
||||
if min == nil {
|
||||
return []interface{}{}
|
||||
}
|
||||
|
||||
result = make([]interface{}, 0, 16)
|
||||
|
||||
iter := NewIterator(max)
|
||||
for iter.Next() {
|
||||
result = append(result, iter.Value())
|
||||
if iter.cur == min {
|
||||
break
|
||||
}
|
||||
}
|
||||
case 0:
|
||||
if n, ok := tree.GetNode(k1); ok {
|
||||
return []interface{}{n.value}
|
||||
}
|
||||
return []interface{}{}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (avl *Tree) Get(key interface{}) (interface{}, bool) {
|
||||
n, ok := avl.GetNode(key)
|
||||
if ok {
|
||||
|
@ -138,12 +202,8 @@ func (avl *Tree) Get(key interface{}) (interface{}, bool) {
|
|||
return n, false
|
||||
}
|
||||
|
||||
func (avl *Tree) GetRange(min, max interface{}) []interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (avl *Tree) GetAround(key interface{}) (result [3]interface{}) {
|
||||
an := avl.GetAroundNode(key)
|
||||
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
|
||||
|
@ -152,41 +212,165 @@ func (avl *Tree) GetAround(key interface{}) (result [3]interface{}) {
|
|||
return
|
||||
}
|
||||
|
||||
func (avl *Tree) GetAroundNode(value interface{}) (result [3]*Node) {
|
||||
if cur, ok := avl.GetNode(value); ok {
|
||||
func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
||||
var last *Node
|
||||
var lastc int
|
||||
|
||||
var iter *Iterator
|
||||
for n := tree.root; n != nil; {
|
||||
last = n
|
||||
c := tree.compare(key, n.value)
|
||||
switch c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
lastc = c
|
||||
case 1:
|
||||
n = n.children[1]
|
||||
lastc = c
|
||||
case 0:
|
||||
result[1] = n
|
||||
n = nil
|
||||
default:
|
||||
panic("Get compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
|
||||
iter = NewIterator(cur)
|
||||
iter.curPushPrevStack(iter.up)
|
||||
iter.up = iter.getPrevUp(iter.up)
|
||||
switch lastc {
|
||||
case 1:
|
||||
const il = 0
|
||||
const ir = 1
|
||||
|
||||
if result[1] == nil {
|
||||
result[0] = last
|
||||
|
||||
parent := last
|
||||
for ; parent != nil && parent.parent != nil; parent = parent.parent {
|
||||
child := getRelationship(parent)
|
||||
if child == (-lastc+2)/2 { // child 与 compare 后左右的关系
|
||||
result[2] = parent.parent
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := iter.tstack.Pop(); ok {
|
||||
result[0] = v.(*Node)
|
||||
// iter.curPushPrevStack(iter.cur)
|
||||
} else {
|
||||
result[0] = iter.up
|
||||
l := result[1].children[il]
|
||||
r := result[1].children[ir]
|
||||
|
||||
if l == nil {
|
||||
result[0] = result[1].parent
|
||||
} else {
|
||||
for l.children[ir] != nil {
|
||||
l = l.children[ir]
|
||||
}
|
||||
result[0] = l
|
||||
}
|
||||
|
||||
if r == nil {
|
||||
|
||||
parent := result[1].parent
|
||||
for ; parent != nil && parent.parent != nil; parent = parent.parent {
|
||||
child := getRelationship(parent)
|
||||
if child == (-lastc+2)/2 { // child 与 compare 后左右的关系
|
||||
result[2] = parent.parent
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
for r.children[il] != nil {
|
||||
r = r.children[il]
|
||||
}
|
||||
result[2] = r
|
||||
}
|
||||
}
|
||||
|
||||
iter = NewIterator(cur)
|
||||
iter.curPushNextStack(iter.up)
|
||||
iter.up = iter.getNextUp(iter.up)
|
||||
case -1:
|
||||
|
||||
const il = 1
|
||||
const ir = 0
|
||||
|
||||
if result[1] == nil {
|
||||
result[2] = last
|
||||
|
||||
parent := last
|
||||
for ; parent != nil && parent.parent != nil; parent = parent.parent {
|
||||
child := getRelationship(parent)
|
||||
if child == (-lastc+2)/2 { // child 与 compare 后左右的关系
|
||||
result[0] = parent.parent
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := iter.tstack.Pop(); ok {
|
||||
result[2] = v.(*Node)
|
||||
// iter.curPushNextStack(iter.cur)
|
||||
} else {
|
||||
result[2] = iter.up
|
||||
|
||||
l := result[1].children[il]
|
||||
r := result[1].children[ir]
|
||||
|
||||
if l == nil {
|
||||
result[2] = result[1].parent
|
||||
} else {
|
||||
for l.children[ir] != nil {
|
||||
l = l.children[ir]
|
||||
}
|
||||
result[2] = l
|
||||
}
|
||||
|
||||
if r == nil {
|
||||
|
||||
parent := result[1].parent
|
||||
for ; parent != nil && parent.parent != nil; parent = parent.parent {
|
||||
child := getRelationship(parent)
|
||||
if child == (-lastc+2)/2 { // child 与 compare 后左右的关系
|
||||
result[0] = parent.parent
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
for r.children[il] != nil {
|
||||
r = r.children[il]
|
||||
}
|
||||
result[0] = r
|
||||
|
||||
}
|
||||
}
|
||||
case 0:
|
||||
|
||||
const il = 0
|
||||
const ir = 1
|
||||
|
||||
if result[1] == nil {
|
||||
return
|
||||
}
|
||||
|
||||
result[1] = cur
|
||||
l := result[1].children[il]
|
||||
r := result[1].children[ir]
|
||||
|
||||
if l == nil {
|
||||
result[0] = nil
|
||||
} else {
|
||||
for l.children[ir] != nil {
|
||||
l = l.children[ir]
|
||||
}
|
||||
result[0] = l
|
||||
}
|
||||
|
||||
if r == nil {
|
||||
result[2] = nil
|
||||
} else {
|
||||
for r.children[il] != nil {
|
||||
r = r.children[il]
|
||||
}
|
||||
result[2] = r
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (avl *Tree) GetNode(value interface{}) (*Node, bool) {
|
||||
|
||||
for n := avl.root; n != nil; {
|
||||
switch c := avl.comparator(value, n.value); c {
|
||||
switch c := avl.compare(value, n.value); c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
case 1:
|
||||
|
@ -194,7 +378,7 @@ func (avl *Tree) GetNode(value interface{}) (*Node, bool) {
|
|||
case 0:
|
||||
return n, true
|
||||
default:
|
||||
panic("Get comparator only is allowed in -1, 0, 1")
|
||||
panic("Get compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
|
@ -224,36 +408,37 @@ func (avl *Tree) Put(value interface{}) {
|
|||
}
|
||||
|
||||
parent = cur
|
||||
c := avl.comparator(value, cur.value)
|
||||
c := avl.compare(value, cur.value)
|
||||
child = (c + 2) / 2
|
||||
cur = cur.children[child]
|
||||
}
|
||||
}
|
||||
|
||||
func (avl *Tree) debugString() string {
|
||||
if avl.size == 0 {
|
||||
return ""
|
||||
}
|
||||
str := "AVLTree\n"
|
||||
outputfordebug(avl.root, "", true, &str)
|
||||
return str
|
||||
}
|
||||
|
||||
type TraversalMethod int
|
||||
|
||||
const (
|
||||
// L = left R = right D = Value(dest)
|
||||
_ TraversalMethod = iota
|
||||
//DLR 前序遍历
|
||||
//DLR 先值 然后左递归 右递归 下面同理
|
||||
DLR
|
||||
//LDR 中序遍历
|
||||
//LDR 先从左边有序访问到右边 从小到大
|
||||
LDR
|
||||
//LRD 后序遍历
|
||||
// LRD 同理
|
||||
LRD
|
||||
|
||||
// DRL 同理
|
||||
DRL
|
||||
|
||||
// RDL 先从右边有序访问到左边 从大到小
|
||||
RDL
|
||||
|
||||
// RLD 同理
|
||||
RLD
|
||||
)
|
||||
|
||||
// Traversal 遍历的方法
|
||||
func (avl *Tree) Traversal(every func(v interface{}), traversalMethod ...interface{}) {
|
||||
if avl.root == nil {
|
||||
// Traversal 遍历的方法 默认是LDR 从小到大 compare 为 l < r
|
||||
func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...interface{}) {
|
||||
if tree.root == nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -264,40 +449,114 @@ func (avl *Tree) Traversal(every func(v interface{}), traversalMethod ...interfa
|
|||
|
||||
switch method {
|
||||
case DLR:
|
||||
var traverasl func(cur *Node)
|
||||
traverasl = func(cur *Node) {
|
||||
var traverasl func(cur *Node) bool
|
||||
traverasl = func(cur *Node) bool {
|
||||
if cur == nil {
|
||||
return
|
||||
return true
|
||||
}
|
||||
traverasl(cur.children[0])
|
||||
every(cur.value)
|
||||
traverasl(cur.children[1])
|
||||
}
|
||||
traverasl(avl.root)
|
||||
case LRD:
|
||||
var traverasl func(cur *Node)
|
||||
traverasl = func(cur *Node) {
|
||||
if cur == nil {
|
||||
return
|
||||
if !every(cur.value) {
|
||||
return false
|
||||
}
|
||||
traverasl(cur.children[1])
|
||||
every(cur.value)
|
||||
traverasl(cur.children[0])
|
||||
if !traverasl(cur.children[0]) {
|
||||
return false
|
||||
}
|
||||
if !traverasl(cur.children[1]) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
traverasl(avl.root)
|
||||
traverasl(tree.root)
|
||||
case LDR:
|
||||
var traverasl func(cur *Node)
|
||||
traverasl = func(cur *Node) {
|
||||
var traverasl func(cur *Node) bool
|
||||
traverasl = func(cur *Node) bool {
|
||||
if cur == nil {
|
||||
return
|
||||
return true
|
||||
}
|
||||
every(cur.value)
|
||||
traverasl(cur.children[0])
|
||||
traverasl(cur.children[1])
|
||||
if !traverasl(cur.children[0]) {
|
||||
return false
|
||||
}
|
||||
if !every(cur.value) {
|
||||
return false
|
||||
}
|
||||
if !traverasl(cur.children[1]) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
traverasl(avl.root)
|
||||
traverasl(tree.root)
|
||||
case LRD:
|
||||
var traverasl func(cur *Node) bool
|
||||
traverasl = func(cur *Node) bool {
|
||||
if cur == nil {
|
||||
return true
|
||||
}
|
||||
if !traverasl(cur.children[0]) {
|
||||
return false
|
||||
}
|
||||
if !traverasl(cur.children[1]) {
|
||||
return false
|
||||
}
|
||||
if !every(cur.value) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
traverasl(tree.root)
|
||||
case DRL:
|
||||
var traverasl func(cur *Node) bool
|
||||
traverasl = func(cur *Node) bool {
|
||||
if cur == nil {
|
||||
return true
|
||||
}
|
||||
if !every(cur.value) {
|
||||
return false
|
||||
}
|
||||
if !traverasl(cur.children[0]) {
|
||||
return false
|
||||
}
|
||||
if !traverasl(cur.children[1]) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
traverasl(tree.root)
|
||||
case RDL:
|
||||
var traverasl func(cur *Node) bool
|
||||
traverasl = func(cur *Node) bool {
|
||||
if cur == nil {
|
||||
return true
|
||||
}
|
||||
if !traverasl(cur.children[1]) {
|
||||
return false
|
||||
}
|
||||
if !every(cur.value) {
|
||||
return false
|
||||
}
|
||||
if !traverasl(cur.children[0]) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
traverasl(tree.root)
|
||||
case RLD:
|
||||
var traverasl func(cur *Node) bool
|
||||
traverasl = func(cur *Node) bool {
|
||||
if cur == nil {
|
||||
return true
|
||||
}
|
||||
if !traverasl(cur.children[1]) {
|
||||
return false
|
||||
}
|
||||
if !traverasl(cur.children[0]) {
|
||||
return false
|
||||
}
|
||||
if !every(cur.value) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
traverasl(tree.root)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (avl *Tree) lrrotate(cur *Node) {
|
||||
|
@ -563,23 +822,14 @@ func getHeight(cur *Node) int {
|
|||
return cur.height
|
||||
}
|
||||
|
||||
func abs(n int) int {
|
||||
y := n >> 31
|
||||
return (n ^ y) - y
|
||||
}
|
||||
|
||||
func (avl *Tree) fixRemoveHeight(cur *Node) {
|
||||
for {
|
||||
|
||||
lefth, rigthh, lrmax := getMaxAndChildrenHeight(cur)
|
||||
|
||||
// 判断当前节点是否有变化, 如果没变化的时候, 不需要往上修复
|
||||
isBreak := false
|
||||
if cur.height == lrmax+1 {
|
||||
isBreak = true
|
||||
} else {
|
||||
cur.height = lrmax + 1
|
||||
}
|
||||
curheight := lrmax + 1
|
||||
cur.height = curheight
|
||||
|
||||
// 计算高度的差值 绝对值大于2的时候需要旋转
|
||||
diff := lefth - rigthh
|
||||
|
@ -598,7 +848,7 @@ func (avl *Tree) fixRemoveHeight(cur *Node) {
|
|||
avl.rrotate(cur)
|
||||
}
|
||||
} else {
|
||||
if isBreak {
|
||||
if cur.height == curheight {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -722,3 +972,12 @@ func outputfordebug(node *Node, prefix string, isTail bool, str *string) {
|
|||
outputfordebug(node.children[0], newPrefix, true, str)
|
||||
}
|
||||
}
|
||||
|
||||
func (avl *Tree) debugString() string {
|
||||
if avl.size == 0 {
|
||||
return ""
|
||||
}
|
||||
str := "AVLTree\n"
|
||||
outputfordebug(avl.root, "", true, &str)
|
||||
return str
|
||||
}
|
||||
|
|
|
@ -95,6 +95,7 @@ func TestIterator(t *testing.T) {
|
|||
}
|
||||
|
||||
l = []int{21, 20, 15, 14, 7, 7, 6, 5, 4, 3, 2, 1}
|
||||
t.Error(iter.cur, iter.tstack)
|
||||
for i := 0; iter.Next(); i++ { // cur is 30 next is 21
|
||||
if iter.Value().(int) != l[i] {
|
||||
t.Error(iter.Value())
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
package avl
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"474420502.top/eson/structure/lastack"
|
||||
)
|
||||
|
||||
type Iterator struct {
|
||||
op *Tree
|
||||
|
||||
dir int
|
||||
up *Node
|
||||
cur *Node
|
||||
|
@ -15,7 +15,7 @@ type Iterator struct {
|
|||
}
|
||||
|
||||
func initIterator(avltree *Tree) *Iterator {
|
||||
iter := &Iterator{op: avltree, tstack: lastack.New()}
|
||||
iter := &Iterator{tstack: lastack.New()}
|
||||
iter.up = avltree.root
|
||||
return iter
|
||||
}
|
||||
|
@ -48,6 +48,36 @@ func (iter *Iterator) Right() bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func GetPrev(cur *Node, idx int) *Node {
|
||||
|
||||
iter := NewIterator(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.(*Node)
|
||||
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 {
|
||||
|
@ -75,6 +105,35 @@ func (iter *Iterator) Prev() (result bool) {
|
|||
|
||||
return false
|
||||
}
|
||||
func GetNext(cur *Node, idx int) *Node {
|
||||
|
||||
iter := NewIterator(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.(*Node)
|
||||
if i == idx-1 {
|
||||
return iter.cur
|
||||
}
|
||||
iter.curPushNextStack(iter.cur)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return cur
|
||||
}
|
||||
|
||||
func (iter *Iterator) Next() (result bool) {
|
||||
|
||||
|
@ -127,6 +186,7 @@ func (iter *Iterator) curPushNextStack(cur *Node) {
|
|||
next := cur.children[0] // 当前的左然后向右找, 找到最大, 就是最接近cur 并且小于cur的值
|
||||
|
||||
if next != nil {
|
||||
log.Println(iter.tstack)
|
||||
iter.tstack.Push(next)
|
||||
for next.children[1] != nil {
|
||||
next = next.children[1]
|
||||
|
|
215
vbt/iterator.go
Normal file
215
vbt/iterator.go
Normal file
|
@ -0,0 +1,215 @@
|
|||
package vbt
|
||||
|
||||
import (
|
||||
"474420502.top/eson/structure/lastack"
|
||||
)
|
||||
|
||||
type Iterator struct {
|
||||
dir int
|
||||
up *Node
|
||||
cur *Node
|
||||
tstack *lastack.Stack
|
||||
// curnext *Node
|
||||
}
|
||||
|
||||
func initIterator(avltree *Tree) *Iterator {
|
||||
iter := &Iterator{tstack: lastack.New()}
|
||||
iter.up = avltree.root
|
||||
return iter
|
||||
}
|
||||
|
||||
func NewIterator(n *Node) *Iterator {
|
||||
iter := &Iterator{tstack: lastack.New()}
|
||||
iter.up = n
|
||||
return iter
|
||||
}
|
||||
|
||||
func (iter *Iterator) Value() interface{} {
|
||||
return iter.cur.value
|
||||
}
|
||||
|
||||
func (iter *Iterator) Left() bool {
|
||||
if iter.cur.children[0] != nil {
|
||||
iter.dir = 0
|
||||
iter.cur = iter.cur.children[0]
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (iter *Iterator) Right() bool {
|
||||
if iter.cur.children[1] != nil {
|
||||
iter.dir = 0
|
||||
iter.cur = iter.cur.children[1]
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func GetPrev(cur *Node, idx int) *Node {
|
||||
|
||||
iter := NewIterator(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.(*Node)
|
||||
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 {
|
||||
if iter.dir == 1 && iter.cur != nil {
|
||||
iter.tstack.Clear()
|
||||
iter.curPushPrevStack(iter.cur)
|
||||
iter.up = iter.getPrevUp(iter.cur)
|
||||
}
|
||||
iter.dir = -1
|
||||
}
|
||||
|
||||
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.(*Node)
|
||||
iter.curPushPrevStack(iter.cur)
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
func GetNext(cur *Node, idx int) *Node {
|
||||
|
||||
iter := NewIterator(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.(*Node)
|
||||
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 { // 非 1(next 方向定义 -1 为 prev)
|
||||
if iter.dir == -1 && iter.cur != nil { // 如果上次为prev方向, 则清空辅助计算的栈
|
||||
iter.tstack.Clear()
|
||||
iter.curPushNextStack(iter.cur) // 把当前cur计算的逆向回朔
|
||||
iter.up = iter.getNextUp(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.getNextUp(iter.up)
|
||||
}
|
||||
|
||||
if v, ok := iter.tstack.Pop(); ok {
|
||||
iter.cur = v.(*Node)
|
||||
iter.curPushNextStack(iter.cur)
|
||||
return true
|
||||
}
|
||||
|
||||
// 如果再次计算的栈为空, 则只能返回false
|
||||
return false
|
||||
}
|
||||
|
||||
func getRelationship(cur *Node) int {
|
||||
if cur.parent.children[1] == cur {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (iter *Iterator) getNextUp(cur *Node) *Node {
|
||||
for cur.parent != nil {
|
||||
if getRelationship(cur) == 1 { // next 在 降序 小值. 如果child在右边, parent 比 child 小, parent才有效, 符合降序
|
||||
return cur.parent
|
||||
}
|
||||
cur = cur.parent
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (iter *Iterator) curPushNextStack(cur *Node) {
|
||||
next := cur.children[0] // 当前的左然后向右找, 找到最大, 就是最接近cur 并且小于cur的值
|
||||
|
||||
if next != nil {
|
||||
iter.tstack.Push(next)
|
||||
for next.children[1] != nil {
|
||||
next = next.children[1]
|
||||
iter.tstack.Push(next) // 入栈 用于回溯
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *Iterator) getPrevUp(cur *Node) *Node {
|
||||
for cur.parent != nil {
|
||||
if getRelationship(cur) == 0 { // Prev 在 降序 大值. 如果child在左边, parent 比 child 大, parent才有效 , 符合降序
|
||||
return cur.parent
|
||||
}
|
||||
cur = cur.parent
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (iter *Iterator) curPushPrevStack(cur *Node) {
|
||||
prev := cur.children[1]
|
||||
|
||||
if prev != nil {
|
||||
iter.tstack.Push(prev)
|
||||
for prev.children[0] != nil {
|
||||
prev = prev.children[0]
|
||||
iter.tstack.Push(prev)
|
||||
}
|
||||
}
|
||||
}
|
1064
vbt/vbt.go
Normal file
1064
vbt/vbt.go
Normal file
File diff suppressed because it is too large
Load Diff
767
vbt/vbt_test.go
Normal file
767
vbt/vbt_test.go
Normal file
|
@ -0,0 +1,767 @@
|
|||
package vbt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"474420502.top/eson/structure/compare"
|
||||
"github.com/huandu/skiplist"
|
||||
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/emirpasic/gods/trees/avltree"
|
||||
"github.com/emirpasic/gods/trees/redblacktree"
|
||||
)
|
||||
|
||||
const CompareSize = 1000000
|
||||
const NumberMax = 50000000
|
||||
|
||||
func Save(t *testing.T) {
|
||||
|
||||
f, err := os.OpenFile("../l.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
var l []int
|
||||
// for i := 0; len(l) < 1000; i++ {
|
||||
// v := randomdata.Number(0, 65535)
|
||||
// l = append(l, v)
|
||||
// }
|
||||
|
||||
//m := make(map[int]int)
|
||||
for i := 0; len(l) < CompareSize; i++ {
|
||||
v := randomdata.Number(0, NumberMax)
|
||||
// if _, ok := m[v]; !ok {
|
||||
// m[v] = v
|
||||
l = append(l, v)
|
||||
// }
|
||||
}
|
||||
|
||||
var result bytes.Buffer
|
||||
encoder := gob.NewEncoder(&result)
|
||||
encoder.Encode(l)
|
||||
lbytes := result.Bytes()
|
||||
f.Write(lbytes)
|
||||
|
||||
}
|
||||
|
||||
func loadTestData() []int {
|
||||
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
|
||||
}
|
||||
|
||||
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 14 14 15 16 17 20 21 30 40 40 40 40 50]
|
||||
// t.Error(tree.Values(), tree.Size())
|
||||
|
||||
var result string
|
||||
result = spew.Sprint(tree.IndexRange(0, 5))
|
||||
if result != "[3 7 14 14 14 15] true" {
|
||||
t.Error(result)
|
||||
}
|
||||
|
||||
result = spew.Sprint(tree.IndexRange(2, 5))
|
||||
if result != "[14 14 14 15] true" {
|
||||
t.Error(result)
|
||||
}
|
||||
|
||||
result = spew.Sprint(tree.IndexRange(10, 100))
|
||||
if result != "[30 40 40 40 40 50] false" {
|
||||
t.Error(result)
|
||||
}
|
||||
|
||||
result = spew.Sprint(tree.IndexRange(15, 0)) // size = 16, index max = 15
|
||||
if result != "[50 40 40 40 40 30 21 20 17 16 15 14 14 14 7 3] true" {
|
||||
t.Error(result)
|
||||
}
|
||||
|
||||
result = spew.Sprint(tree.IndexRange(16, 0)) // size = 16, index max = 15
|
||||
if result != "[50 40 40 40 40 30 21 20 17 16 15 14 14 14 7 3] false" {
|
||||
t.Error(result)
|
||||
}
|
||||
|
||||
result = spew.Sprint(tree.IndexRange(5, 1)) // size = 16, index max = 15
|
||||
if result != "[15 14 14 14 7] true" {
|
||||
t.Error(result)
|
||||
}
|
||||
|
||||
result = spew.Sprint(tree.IndexRange(-1, -5)) // size = 16, index max = 15
|
||||
if result != "[50 40 40 40 40] true" {
|
||||
t.Error(result)
|
||||
}
|
||||
|
||||
result = spew.Sprint(tree.IndexRange(-1, -16)) // size = 16, index max = 0 - 15 (-1,-16)
|
||||
if result != "[50 40 40 40 40 30 21 20 17 16 15 14 14 14 7 3] true" {
|
||||
t.Error(result)
|
||||
}
|
||||
|
||||
result = spew.Sprint(tree.IndexRange(-1, -17)) // size = 16, index max = 0 - 15 (-1,-16)
|
||||
if result != "[50 40 40 40 40 30 21 20 17 16 15 14 14 14 7 3] false" {
|
||||
t.Error(result)
|
||||
}
|
||||
|
||||
result = spew.Sprint(tree.IndexRange(-5, -1)) // size = 16, index max = 0 - 15 (-1,-16)
|
||||
if result != "[40 40 40 40 50] 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 40]" {
|
||||
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 TestGet(t *testing.T) {
|
||||
tree := New(compare.Int)
|
||||
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
|
||||
tree.Put(v, v)
|
||||
}
|
||||
|
||||
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
|
||||
v, ok := tree.Get(v)
|
||||
if !ok {
|
||||
t.Error("the val not found ", v)
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := tree.Get(10000); ok {
|
||||
t.Error("the val(1000) is not in tree, but is found", v)
|
||||
}
|
||||
}
|
||||
|
||||
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(v interface{}) bool {
|
||||
result = append(result, v)
|
||||
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) < 20; 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 < 20; 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 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 {
|
||||
e := sl.Get(v)
|
||||
var result [50]interface{}
|
||||
for i := 0; i < 50 && e != nil; i++ {
|
||||
result[i] = e.Value
|
||||
e = e.Next()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 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 := 10
|
||||
b.N = len(l) * execCount
|
||||
for i := 0; i < execCount; i++ {
|
||||
tree := New(compare.Int)
|
||||
for _, v := range l {
|
||||
tree.Put(v, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPutStable(t *testing.T) {
|
||||
|
||||
// l := []int{14, 18, 20, 21, 22, 23, 19}
|
||||
// for n := 0; n < 1000000; n++ {
|
||||
// var l []int
|
||||
// l = append(l, 60, 5, 15)
|
||||
// for i := 0; len(l) < 11; i++ {
|
||||
// l = append(l, randomdata.Number(3, 69))
|
||||
// }
|
||||
|
||||
// tree := New(compare.Int)
|
||||
// for _, v := range l {
|
||||
// tree.Put(v, v)
|
||||
// }
|
||||
// result := tree.getArountNode(60)
|
||||
|
||||
// if result[2] == nil && tree.indexNode(-1) != result[1] {
|
||||
// t.Error(tree.debugString())
|
||||
// t.Error(tree.Values())
|
||||
// t.Error(result)
|
||||
// }
|
||||
|
||||
// result = tree.getArountNode(5)
|
||||
// if result[0] == nil && tree.indexNode(0) != result[1] {
|
||||
// t.Error(tree.debugString())
|
||||
// t.Error(tree.Values())
|
||||
// t.Error(result)
|
||||
// }
|
||||
|
||||
// result = tree.getArountNode(2)
|
||||
// if result[2] == nil && tree.indexNode(0) != result[2] {
|
||||
// t.Error(tree.debugString())
|
||||
// t.Error(tree.Values())
|
||||
// t.Error(result)
|
||||
// }
|
||||
|
||||
// result = tree.getArountNode(70)
|
||||
// if result[0] == nil && tree.indexNode(-1) != result[0] {
|
||||
// t.Error(tree.debugString())
|
||||
// t.Error(tree.Values())
|
||||
// t.Error(result)
|
||||
// }
|
||||
|
||||
// }
|
||||
// for _, v := range []int{10, 0, 9, 5, -11, -10, -1, -5} {
|
||||
// t.Error(tree.Index(v))
|
||||
// // t.Error(tree.debugString())
|
||||
// }
|
||||
|
||||
// tree.RemoveIndex(4)
|
||||
// t.Error(tree.Index(4))
|
||||
// t.Error(tree.Values())
|
||||
// t.Error(tree.debugString())
|
||||
// t.Error(len(l), tree.debugString(), "\n", "-----------") // 3 6(4)
|
||||
|
||||
}
|
||||
|
||||
func BenchmarkIndex(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()
|
||||
|
||||
b.N = 1000000
|
||||
|
||||
var result [50]interface{}
|
||||
for n := 0; n < b.N; n++ {
|
||||
i := 0
|
||||
tree.Traversal(func(v interface{}) bool {
|
||||
result[i] = v
|
||||
i++
|
||||
if i < 50 {
|
||||
return true
|
||||
}
|
||||
log.Print(i)
|
||||
return false
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkTraversal(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 n := 0; n < execCount; n++ {
|
||||
i := 0
|
||||
var result []interface{}
|
||||
tree.Traversal(func(v interface{}) bool {
|
||||
result = append(result, v)
|
||||
i++
|
||||
if i >= 50 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGodsRBPut(b *testing.B) {
|
||||
tree := redblacktree.NewWithIntComparator()
|
||||
|
||||
l := loadTestData()
|
||||
|
||||
b.ResetTimer()
|
||||
b.StartTimer()
|
||||
|
||||
b.N = len(l)
|
||||
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,4 +1,4 @@
|
|||
package avlindex
|
||||
package vbtkey
|
||||
|
||||
import (
|
||||
"474420502.top/eson/structure/lastack"
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package avlindex
|
||||
package vbtkey
|
||||
|
||||
import (
|
||||
"474420502.top/eson/structure/compare"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
|
@ -26,10 +27,10 @@ func (n *Node) String() string {
|
|||
|
||||
type Tree struct {
|
||||
root *Node
|
||||
comparator comparator.Comparator
|
||||
comparator compare.Compare
|
||||
}
|
||||
|
||||
func New(comparator comparator.Comparator) *Tree {
|
||||
func New(comparator compare.Compare) *Tree {
|
||||
return &Tree{comparator: comparator}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package avlindex
|
||||
package vbtkey
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
@ -8,16 +8,16 @@ import (
|
|||
"os"
|
||||
"testing"
|
||||
|
||||
"474420502.top/eson/structure/compare"
|
||||
"github.com/huandu/skiplist"
|
||||
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/emirpasic/gods/trees/avltree"
|
||||
"github.com/emirpasic/gods/trees/redblacktree"
|
||||
"github.com/emirpasic/gods/utils"
|
||||
)
|
||||
|
||||
const CompartorSize = 1000000
|
||||
const CompareSize = 1000000
|
||||
const NumberMax = 50000000
|
||||
|
||||
func Save(t *testing.T) {
|
||||
|
@ -33,7 +33,7 @@ func Save(t *testing.T) {
|
|||
// }
|
||||
|
||||
//m := make(map[int]int)
|
||||
for i := 0; len(l) < CompartorSize; i++ {
|
||||
for i := 0; len(l) < CompareSize; i++ {
|
||||
v := randomdata.Number(0, NumberMax)
|
||||
// if _, ok := m[v]; !ok {
|
||||
// m[v] = v
|
||||
|
@ -61,7 +61,7 @@ func loadTestData() []int {
|
|||
}
|
||||
|
||||
func TestIndexRange(t *testing.T) {
|
||||
tree := New(utils.IntComparator)
|
||||
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)
|
||||
|
@ -122,7 +122,7 @@ func TestIndexRange(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestGetAround(t *testing.T) {
|
||||
tree := New(utils.IntComparator)
|
||||
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)
|
||||
}
|
||||
|
@ -192,7 +192,7 @@ func TestGetAround(t *testing.T) {
|
|||
func TestPutComparatorRandom(t *testing.T) {
|
||||
|
||||
for n := 0; n < 300000; n++ {
|
||||
tree := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
godsavl := avltree.NewWithIntComparator()
|
||||
|
||||
content := ""
|
||||
|
@ -220,7 +220,7 @@ func TestPutComparatorRandom(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
tree := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
|
||||
tree.Put(v, v)
|
||||
}
|
||||
|
@ -238,7 +238,7 @@ func TestGet(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestGetRange(t *testing.T) {
|
||||
tree := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
for _, v := range []int{5, 6, 8, 10, 13, 17, 1, 2, 40, 30} {
|
||||
tree.Put(v, v)
|
||||
}
|
||||
|
@ -295,7 +295,7 @@ func TestGetRange(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestTravalsal(t *testing.T) {
|
||||
tree := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
for _, v := range []int{5, 6, 8, 10, 13, 17, 1, 2, 40, 30} {
|
||||
tree.Put(v, v)
|
||||
}
|
||||
|
@ -320,7 +320,7 @@ func TestTravalsal(t *testing.T) {
|
|||
func TestRemoveAll(t *testing.T) {
|
||||
ALL:
|
||||
for c := 0; c < 5000; c++ {
|
||||
tree := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
gods := avltree.NewWithIntComparator()
|
||||
var l []int
|
||||
m := make(map[int]int)
|
||||
|
@ -357,7 +357,7 @@ func TestRemove(t *testing.T) {
|
|||
|
||||
ALL:
|
||||
for N := 0; N < 5000; N++ {
|
||||
tree := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
gods := avltree.NewWithIntComparator()
|
||||
|
||||
var l []int
|
||||
|
@ -423,7 +423,7 @@ func BenchmarkGetRange(b *testing.B) {
|
|||
}
|
||||
|
||||
func BenchmarkIndexRange(b *testing.B) {
|
||||
tree := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
l := loadTestData()
|
||||
b.N = len(l)
|
||||
|
||||
|
@ -460,7 +460,7 @@ func BenchmarkSkipListSet(b *testing.B) {
|
|||
}
|
||||
|
||||
func BenchmarkIterator(b *testing.B) {
|
||||
tree := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
|
||||
l := loadTestData()
|
||||
b.N = len(l)
|
||||
|
@ -486,7 +486,7 @@ func BenchmarkIterator(b *testing.B) {
|
|||
}
|
||||
|
||||
func BenchmarkRemove(b *testing.B) {
|
||||
tree := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
|
||||
l := loadTestData()
|
||||
|
||||
|
@ -541,7 +541,7 @@ func BenchmarkGodsRBRemove(b *testing.B) {
|
|||
|
||||
func BenchmarkGet(b *testing.B) {
|
||||
|
||||
tree := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
|
||||
l := loadTestData()
|
||||
b.N = len(l)
|
||||
|
@ -615,7 +615,7 @@ func BenchmarkPut(b *testing.B) {
|
|||
execCount := 10
|
||||
b.N = len(l) * execCount
|
||||
for i := 0; i < execCount; i++ {
|
||||
tree := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
for _, v := range l {
|
||||
tree.Put(v, v)
|
||||
}
|
||||
|
@ -632,7 +632,7 @@ func TestPutStable(t *testing.T) {
|
|||
// l = append(l, randomdata.Number(3, 69))
|
||||
// }
|
||||
|
||||
// tree := New(utils.IntComparator)
|
||||
// tree := New(compare.Int)
|
||||
// for _, v := range l {
|
||||
// tree.Put(v, v)
|
||||
// }
|
||||
|
@ -680,7 +680,7 @@ func TestPutStable(t *testing.T) {
|
|||
}
|
||||
|
||||
func BenchmarkIndex(b *testing.B) {
|
||||
tree := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
|
||||
l := loadTestData()
|
||||
b.N = len(l)
|
||||
|
@ -709,7 +709,7 @@ func BenchmarkIndex(b *testing.B) {
|
|||
}
|
||||
|
||||
func BenchmarkTraversal(b *testing.B) {
|
||||
tree := New(utils.IntComparator)
|
||||
tree := New(compare.Int)
|
||||
|
||||
l := loadTestData()
|
||||
b.N = len(l)
|
||||
|
|
Loading…
Reference in New Issue
Block a user