如果要保存最大值, 必须要Node交换不能值指针交换
This commit is contained in:
parent
51acc649a4
commit
dd4c76f144
24
avl/avl.go
24
avl/avl.go
|
@ -27,11 +27,11 @@ func (n *Node) String() string {
|
|||
type Tree struct {
|
||||
root *Node
|
||||
size int
|
||||
compare compare.Compare
|
||||
Compare compare.Compare
|
||||
}
|
||||
|
||||
func New(compare compare.Compare) *Tree {
|
||||
return &Tree{compare: compare}
|
||||
func New(Compare compare.Compare) *Tree {
|
||||
return &Tree{Compare: Compare}
|
||||
}
|
||||
|
||||
func (tree *Tree) String() string {
|
||||
|
@ -131,7 +131,7 @@ func (tree *Tree) Values() []interface{} {
|
|||
}
|
||||
|
||||
func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
||||
c := tree.compare(k2, k1)
|
||||
c := tree.Compare(k2, k1)
|
||||
switch c {
|
||||
case 1:
|
||||
|
||||
|
@ -218,7 +218,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
|
||||
for n := tree.root; n != nil; {
|
||||
last = n
|
||||
c := tree.compare(key, n.value)
|
||||
c := tree.Compare(key, n.value)
|
||||
switch c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
|
@ -230,7 +230,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
iter := NewIterator(n)
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.compare(iter.cur.value, n.value) == 0 {
|
||||
if tree.Compare(iter.cur.value, n.value) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
break
|
||||
|
@ -239,7 +239,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
result[1] = n
|
||||
n = nil
|
||||
default:
|
||||
panic("Get compare only is allowed in -1, 0, 1")
|
||||
panic("Get Compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -279,7 +279,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
|
||||
|
||||
for n := tree.root; n != nil; {
|
||||
switch c := tree.compare(value, n.value); c {
|
||||
switch c := tree.Compare(value, n.value); c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
case 1:
|
||||
|
@ -288,7 +288,7 @@ func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
|
|||
iter := NewIterator(n)
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.compare(iter.cur.value, n.value) == 0 {
|
||||
if tree.Compare(iter.cur.value, n.value) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
break
|
||||
|
@ -296,7 +296,7 @@ func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
|
|||
}
|
||||
return n, true
|
||||
default:
|
||||
panic("Get compare only is allowed in -1, 0, 1")
|
||||
panic("Get Compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
|
@ -326,7 +326,7 @@ func (tree *Tree) Put(value interface{}) {
|
|||
}
|
||||
|
||||
parent = cur
|
||||
c := tree.compare(value, cur.value)
|
||||
c := tree.Compare(value, cur.value)
|
||||
child = (c + 2) / 2
|
||||
cur = cur.children[child]
|
||||
}
|
||||
|
@ -354,7 +354,7 @@ const (
|
|||
RLD
|
||||
)
|
||||
|
||||
// Traversal 遍历的方法 默认是LDR 从小到大 compare 为 l < r
|
||||
// Traversal 遍历的方法 默认是LDR 从小到大 Compare 为 l < r
|
||||
func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...interface{}) {
|
||||
if tree.root == nil {
|
||||
return
|
||||
|
|
|
@ -27,11 +27,11 @@ func (n *Node) String() string {
|
|||
type Tree struct {
|
||||
root *Node
|
||||
size int
|
||||
compare compare.Compare
|
||||
Compare compare.Compare
|
||||
}
|
||||
|
||||
func New(compare compare.Compare) *Tree {
|
||||
return &Tree{compare: compare}
|
||||
func New(Compare compare.Compare) *Tree {
|
||||
return &Tree{Compare: Compare}
|
||||
}
|
||||
|
||||
func (tree *Tree) String() string {
|
||||
|
@ -131,7 +131,7 @@ func (tree *Tree) Values() []interface{} {
|
|||
}
|
||||
|
||||
func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
||||
c := tree.compare(k2, k1)
|
||||
c := tree.Compare(k2, k1)
|
||||
switch c {
|
||||
case 1:
|
||||
|
||||
|
@ -218,7 +218,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
|
||||
for n := tree.root; n != nil; {
|
||||
last = n
|
||||
c := tree.compare(key, n.value)
|
||||
c := tree.Compare(key, n.value)
|
||||
switch c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
|
@ -230,7 +230,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
result[1] = n
|
||||
n = nil
|
||||
default:
|
||||
panic("Get compare only is allowed in -1, 0, 1")
|
||||
panic("Get Compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -270,7 +270,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
|
||||
|
||||
for n := tree.root; n != nil; {
|
||||
switch c := tree.compare(value, n.value); c {
|
||||
switch c := tree.Compare(value, n.value); c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
case 1:
|
||||
|
@ -278,7 +278,7 @@ func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
|
|||
case 0:
|
||||
return n, true
|
||||
default:
|
||||
panic("Get compare only is allowed in -1, 0, 1")
|
||||
panic("Get Compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
|
@ -308,7 +308,7 @@ func (tree *Tree) Put(value interface{}) {
|
|||
}
|
||||
|
||||
parent = cur
|
||||
c := tree.compare(value, cur.value)
|
||||
c := tree.Compare(value, cur.value)
|
||||
if c == 0 {
|
||||
cur.value = value
|
||||
return
|
||||
|
@ -340,7 +340,7 @@ const (
|
|||
RLD
|
||||
)
|
||||
|
||||
// Traversal 遍历的方法 默认是LDR 从小到大 compare 为 l < r
|
||||
// Traversal 遍历的方法 默认是LDR 从小到大 Compare 为 l < r
|
||||
func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...interface{}) {
|
||||
if tree.root == nil {
|
||||
return
|
||||
|
|
|
@ -27,11 +27,11 @@ func (n *Node) String() string {
|
|||
type Tree struct {
|
||||
root *Node
|
||||
size int
|
||||
compare compare.Compare
|
||||
Compare compare.Compare
|
||||
}
|
||||
|
||||
func New(compare compare.Compare) *Tree {
|
||||
return &Tree{compare: compare}
|
||||
func New(Compare compare.Compare) *Tree {
|
||||
return &Tree{Compare: Compare}
|
||||
}
|
||||
|
||||
func (tree *Tree) String() string {
|
||||
|
@ -131,7 +131,7 @@ func (tree *Tree) Values() []interface{} {
|
|||
}
|
||||
|
||||
func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
||||
c := tree.compare(k2, k1)
|
||||
c := tree.Compare(k2, k1)
|
||||
switch c {
|
||||
case 1:
|
||||
|
||||
|
@ -218,7 +218,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
|
||||
for n := tree.root; n != nil; {
|
||||
last = n
|
||||
c := tree.compare(key, n.value)
|
||||
c := tree.Compare(key, n.value)
|
||||
switch c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
|
@ -230,7 +230,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
iter := NewIterator(n)
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.compare(iter.cur.value, n.value) == 0 {
|
||||
if tree.Compare(iter.cur.value, n.value) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
break
|
||||
|
@ -239,7 +239,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
result[1] = n
|
||||
n = nil
|
||||
default:
|
||||
panic("Get compare only is allowed in -1, 0, 1")
|
||||
panic("Get Compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -279,7 +279,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
func (tree *Tree) GetNode(key interface{}) (*Node, bool) {
|
||||
|
||||
for n := tree.root; n != nil; {
|
||||
switch c := tree.compare(key, n.key); c {
|
||||
switch c := tree.Compare(key, n.key); c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
case 1:
|
||||
|
@ -288,7 +288,7 @@ func (tree *Tree) GetNode(key interface{}) (*Node, bool) {
|
|||
iter := NewIterator(n)
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.compare(iter.cur.key, n.key) == 0 {
|
||||
if tree.Compare(iter.cur.key, n.key) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
break
|
||||
|
@ -296,7 +296,7 @@ func (tree *Tree) GetNode(key interface{}) (*Node, bool) {
|
|||
}
|
||||
return n, true
|
||||
default:
|
||||
panic("Get compare only is allowed in -1, 0, 1")
|
||||
panic("Get Compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
|
@ -326,7 +326,7 @@ func (tree *Tree) Put(key, value interface{}) {
|
|||
}
|
||||
|
||||
parent = cur
|
||||
c := tree.compare(key, cur.key)
|
||||
c := tree.Compare(key, cur.key)
|
||||
child = (c + 2) / 2
|
||||
cur = cur.children[child]
|
||||
}
|
||||
|
@ -354,7 +354,7 @@ const (
|
|||
RLD
|
||||
)
|
||||
|
||||
// Traversal 遍历的方法 默认是LDR 从小到大 compare 为 l < r
|
||||
// Traversal 遍历的方法 默认是LDR 从小到大 Compare 为 l < r
|
||||
func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...interface{}) {
|
||||
if tree.root == nil {
|
||||
return
|
||||
|
|
|
@ -27,11 +27,11 @@ func (n *Node) String() string {
|
|||
type Tree struct {
|
||||
root *Node
|
||||
size int
|
||||
compare compare.Compare
|
||||
Compare compare.Compare
|
||||
}
|
||||
|
||||
func New(compare compare.Compare) *Tree {
|
||||
return &Tree{compare: compare}
|
||||
func New(Compare compare.Compare) *Tree {
|
||||
return &Tree{Compare: Compare}
|
||||
}
|
||||
|
||||
func (tree *Tree) String() string {
|
||||
|
@ -131,7 +131,7 @@ func (tree *Tree) Values() []interface{} {
|
|||
}
|
||||
|
||||
func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
||||
c := tree.compare(k2, k1)
|
||||
c := tree.Compare(k2, k1)
|
||||
switch c {
|
||||
case 1:
|
||||
|
||||
|
@ -218,7 +218,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
|
||||
for n := tree.root; n != nil; {
|
||||
last = n
|
||||
c := tree.compare(key, n.value)
|
||||
c := tree.Compare(key, n.value)
|
||||
switch c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
|
@ -230,7 +230,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
iter := NewIterator(n)
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.compare(iter.cur.value, n.value) == 0 {
|
||||
if tree.Compare(iter.cur.value, n.value) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
break
|
||||
|
@ -239,7 +239,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
result[1] = n
|
||||
n = nil
|
||||
default:
|
||||
panic("Get compare only is allowed in -1, 0, 1")
|
||||
panic("Get Compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -279,7 +279,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
func (tree *Tree) GetNode(key interface{}) (*Node, bool) {
|
||||
|
||||
for n := tree.root; n != nil; {
|
||||
switch c := tree.compare(key, n.key); c {
|
||||
switch c := tree.Compare(key, n.key); c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
case 1:
|
||||
|
@ -288,7 +288,7 @@ func (tree *Tree) GetNode(key interface{}) (*Node, bool) {
|
|||
iter := NewIterator(n)
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.compare(iter.cur.key, n.key) == 0 {
|
||||
if tree.Compare(iter.cur.key, n.key) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
break
|
||||
|
@ -296,7 +296,7 @@ func (tree *Tree) GetNode(key interface{}) (*Node, bool) {
|
|||
}
|
||||
return n, true
|
||||
default:
|
||||
panic("Get compare only is allowed in -1, 0, 1")
|
||||
panic("Get Compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
|
@ -326,7 +326,7 @@ func (tree *Tree) Put(key, value interface{}) {
|
|||
}
|
||||
|
||||
parent = cur
|
||||
c := tree.compare(key, cur.key)
|
||||
c := tree.Compare(key, cur.key)
|
||||
if c == 0 {
|
||||
cur.key = key
|
||||
cur.value = value
|
||||
|
@ -359,7 +359,7 @@ const (
|
|||
RLD
|
||||
)
|
||||
|
||||
// Traversal 遍历的方法 默认是LDR 从小到大 compare 为 l < r
|
||||
// Traversal 遍历的方法 默认是LDR 从小到大 Compare 为 l < r
|
||||
func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...interface{}) {
|
||||
if tree.root == nil {
|
||||
return
|
||||
|
|
|
@ -3,8 +3,8 @@ package plist
|
|||
import (
|
||||
"strings"
|
||||
|
||||
"474420502.top/eson/structure/compare"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/emirpasic/gods/utils"
|
||||
)
|
||||
|
||||
type Node struct {
|
||||
|
@ -15,11 +15,11 @@ type Node struct {
|
|||
type PriorityList struct {
|
||||
head, tail *Node
|
||||
size int
|
||||
comparator utils.Comparator
|
||||
Compare compare.Compare
|
||||
}
|
||||
|
||||
func New(compartor utils.Comparator) *PriorityList {
|
||||
pl := &PriorityList{head: &Node{}, tail: &Node{}, size: 0, comparator: compartor}
|
||||
func New(Compare compare.Compare) *PriorityList {
|
||||
pl := &PriorityList{head: &Node{}, tail: &Node{}, size: 0, Compare: Compare}
|
||||
pl.head.next = pl.tail
|
||||
pl.tail.prev = pl.head
|
||||
return pl
|
||||
|
@ -57,9 +57,13 @@ func (pl *PriorityList) CircularIterator() *CircularIterator {
|
|||
return &CircularIterator{pl: pl, cur: pl.head}
|
||||
}
|
||||
|
||||
func (pl *PriorityList) Push(pvalue interface{}) {
|
||||
func (pl *PriorityList) Size() int {
|
||||
return pl.size
|
||||
}
|
||||
|
||||
func (pl *PriorityList) Push(value interface{}) {
|
||||
pl.size++
|
||||
pnode := &Node{value: pvalue}
|
||||
pnode := &Node{value: value}
|
||||
if pl.size == 1 {
|
||||
pl.head.next = pnode
|
||||
pl.tail.prev = pnode
|
||||
|
@ -70,7 +74,7 @@ func (pl *PriorityList) Push(pvalue interface{}) {
|
|||
|
||||
cur := pl.head
|
||||
for ; cur.next != pl.tail; cur = cur.next {
|
||||
if pl.comparator(pvalue, cur.next.value) > 0 {
|
||||
if pl.Compare(value, cur.next.value) > 0 {
|
||||
cnext := cur.next
|
||||
|
||||
cur.next = pnode
|
||||
|
@ -88,16 +92,37 @@ func (pl *PriorityList) Push(pvalue interface{}) {
|
|||
pl.tail.prev = pnode
|
||||
}
|
||||
|
||||
func (pl *PriorityList) Get(idx int) interface{} {
|
||||
return pl.GetNode(idx).value
|
||||
func (pl *PriorityList) Top() (result interface{}, ok bool) {
|
||||
if pl.size > 0 {
|
||||
return pl.head.next.value, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (pl *PriorityList) GetNode(idx int) *Node {
|
||||
func (pl *PriorityList) Pop() (result interface{}, ok bool) {
|
||||
if pl.size > 0 {
|
||||
pl.size--
|
||||
temp := pl.head.next
|
||||
temp.next.prev = pl.head
|
||||
pl.head.next = temp.next
|
||||
return temp.value, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (pl *PriorityList) Get(idx int) (interface{}, bool) {
|
||||
if n, ok := pl.GetNode(idx); ok {
|
||||
return n.value, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (pl *PriorityList) GetNode(idx int) (*Node, bool) {
|
||||
if idx >= 0 {
|
||||
cur := pl.head.next
|
||||
for i := 0; cur != pl.tail; i++ {
|
||||
if i == idx {
|
||||
return cur
|
||||
return cur, true
|
||||
}
|
||||
cur = cur.next
|
||||
}
|
||||
|
@ -105,16 +130,18 @@ func (pl *PriorityList) GetNode(idx int) *Node {
|
|||
cur := pl.tail.prev
|
||||
for i := -1; cur != pl.head; i-- {
|
||||
if i == idx {
|
||||
return cur
|
||||
return cur, true
|
||||
}
|
||||
cur = cur.prev
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (pl *PriorityList) RemoveWithIndex(idx int) {
|
||||
pl.Remove(pl.GetNode(idx))
|
||||
if n, ok := pl.GetNode(idx); ok {
|
||||
pl.Remove(n)
|
||||
}
|
||||
}
|
||||
|
||||
func (pl *PriorityList) Remove(node *Node) {
|
||||
|
|
|
@ -5,49 +5,11 @@ import (
|
|||
"encoding/gob"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
"github.com/emirpasic/gods/utils"
|
||||
"474420502.top/eson/structure/compare"
|
||||
)
|
||||
|
||||
const CompartorSize = 100
|
||||
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)
|
||||
}
|
||||
|
||||
//fmt.Println(userBytes)
|
||||
|
||||
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) < CompartorSize; 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 {
|
||||
|
@ -60,7 +22,7 @@ func loadTestData() []int {
|
|||
}
|
||||
|
||||
func TestInsert(t *testing.T) {
|
||||
pl := New(utils.IntComparator)
|
||||
pl := New(compare.Int)
|
||||
for i := 0; i < 10; i++ {
|
||||
pl.Push(i)
|
||||
}
|
||||
|
@ -91,7 +53,7 @@ func TestInsert(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestIterator(t *testing.T) {
|
||||
pl := New(utils.IntComparator)
|
||||
pl := New(compare.Int)
|
||||
for i := 0; i < 10; i++ {
|
||||
pl.Push(i)
|
||||
}
|
||||
|
@ -116,7 +78,7 @@ func TestIterator(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestCircularIterator(t *testing.T) {
|
||||
pl := New(utils.IntComparator)
|
||||
pl := New(compare.Int)
|
||||
for i := 0; i < 10; i++ {
|
||||
pl.Push(i)
|
||||
}
|
||||
|
@ -160,58 +122,118 @@ func TestCircularIterator(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
pl := New(utils.IntComparator)
|
||||
pl := New(compare.Int)
|
||||
for i := 0; i < 10; i++ {
|
||||
pl.Push(i)
|
||||
}
|
||||
|
||||
for _, v := range []int{0, 9, 5, 7} {
|
||||
if pl.Get(v) != (9 - v) {
|
||||
t.Error(v, "Get == ", pl.Get(v))
|
||||
if g, ok := pl.Get(v); ok {
|
||||
if g != (9 - v) {
|
||||
t.Error(v, "Get == ", g)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func() {
|
||||
defer func() {
|
||||
if err := recover(); err == nil {
|
||||
t.Error("out index, but is not error")
|
||||
}
|
||||
}()
|
||||
if n, ok := pl.Get(10); ok {
|
||||
t.Error("index 10 is over size", n)
|
||||
}
|
||||
}
|
||||
|
||||
pl.Get(10)
|
||||
}()
|
||||
func TestTop(t *testing.T) {
|
||||
pl := New(compare.Int)
|
||||
for i := 0; i < 10; i++ {
|
||||
pl.Push(i)
|
||||
}
|
||||
|
||||
i := 0
|
||||
for n, ok := pl.Pop(); ok; n, ok = pl.Pop() {
|
||||
if (9 - i) != n {
|
||||
t.Error("value is not equal ", i)
|
||||
}
|
||||
if top, tok := pl.Top(); tok {
|
||||
if (9 - i - 1) != top {
|
||||
t.Error("top is error cur i = ", i, "top is ", top)
|
||||
}
|
||||
}
|
||||
|
||||
i++
|
||||
}
|
||||
|
||||
if pl.Size() != 0 {
|
||||
t.Error("list size is not zero")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPop(t *testing.T) {
|
||||
pl := New(compare.Int)
|
||||
for i := 0; i < 10; i++ {
|
||||
pl.Push(i)
|
||||
}
|
||||
|
||||
i := 0
|
||||
for n, ok := pl.Pop(); ok; n, ok = pl.Pop() {
|
||||
if (9 - i) != n {
|
||||
t.Error("value is not equal ", i)
|
||||
}
|
||||
i++
|
||||
}
|
||||
|
||||
if pl.Size() != 0 {
|
||||
t.Error("list size is not zero")
|
||||
}
|
||||
|
||||
for i := 9; i >= 0; i-- {
|
||||
pl.Push(i)
|
||||
}
|
||||
|
||||
i = 0
|
||||
for n, ok := pl.Pop(); ok; n, ok = pl.Pop() {
|
||||
if (9 - i) != n {
|
||||
t.Error("value is not equal ", i)
|
||||
}
|
||||
i++
|
||||
}
|
||||
|
||||
if pl.Size() != 0 {
|
||||
t.Error("list size is not zero")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemove(t *testing.T) {
|
||||
pl := New(utils.IntComparator)
|
||||
pl := New(compare.Int)
|
||||
for i := 0; i < 10; i++ {
|
||||
pl.Push(i)
|
||||
}
|
||||
|
||||
pl.RemoveWithIndex(0)
|
||||
if pl.Get(0).(int) != 8 {
|
||||
t.Error(pl.Get(0))
|
||||
if g, ok := pl.Get(0); ok {
|
||||
if g != 8 {
|
||||
t.Error(g)
|
||||
}
|
||||
}
|
||||
|
||||
pl.RemoveWithIndex(-1)
|
||||
if pl.Get(-1).(int) != 1 {
|
||||
t.Error(pl.Get(-1))
|
||||
if g, ok := pl.Get(-1); ok {
|
||||
if g != 1 {
|
||||
t.Error(g)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func BenchmarkGet(b *testing.B) {
|
||||
pl := New(utils.IntComparator)
|
||||
pl := New(compare.Int)
|
||||
|
||||
b.N = 100
|
||||
l := loadTestData()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
v := randomdata.Number(0, 65535)
|
||||
for _, v := range l {
|
||||
pl.Push(v)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
b.StartTimer()
|
||||
b.N = len(l)
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
if i%2 == 0 {
|
||||
|
@ -227,10 +249,10 @@ func BenchmarkInsert(b *testing.B) {
|
|||
b.ResetTimer()
|
||||
b.StartTimer()
|
||||
|
||||
execCount := 500
|
||||
execCount := 1
|
||||
b.N = len(l) * execCount
|
||||
for i := 0; i < execCount; i++ {
|
||||
pl := New(utils.IntComparator)
|
||||
pl := New(compare.Int)
|
||||
for _, v := range l {
|
||||
pl.Push(v)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,60 @@
|
|||
package pqueue
|
||||
|
||||
import "474420502.top/eson/structure/avl"
|
||||
import (
|
||||
"474420502.top/eson/structure/compare"
|
||||
)
|
||||
|
||||
type PriorityQueue struct {
|
||||
datas *avl.Tree
|
||||
queue *vbTree
|
||||
head *tNode
|
||||
}
|
||||
|
||||
func (pq *PriorityQueue) Iterator() *Iterator {
|
||||
return nil
|
||||
}
|
||||
|
||||
func New(Compare compare.Compare) *PriorityQueue {
|
||||
return &PriorityQueue{queue: newVBT(Compare)}
|
||||
}
|
||||
|
||||
func (pq *PriorityQueue) Size() int {
|
||||
return pq.queue.Size()
|
||||
}
|
||||
|
||||
func (pq *PriorityQueue) Push(value interface{}) {
|
||||
n := pq.queue.Put(value)
|
||||
if pq.head == nil {
|
||||
pq.head = n
|
||||
return
|
||||
}
|
||||
if pq.queue.Compare(n, pq.head) == 1 {
|
||||
pq.head = n
|
||||
}
|
||||
}
|
||||
|
||||
func (pq *PriorityQueue) Top() (result interface{}, ok bool) {
|
||||
if pq.head != nil {
|
||||
return pq.head.value, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (pq *PriorityQueue) Pop() (result interface{}, ok bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (pq *PriorityQueue) Get(idx int) (interface{}, bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (pq *PriorityQueue) RemoveWithIndex(idx int) {
|
||||
|
||||
}
|
||||
|
||||
func (pq *PriorityQueue) Remove(key interface{}) {
|
||||
|
||||
}
|
||||
|
||||
func (pq *PriorityQueue) Values() []interface{} {
|
||||
return nil
|
||||
}
|
||||
|
|
981
priority_queue/vbt.go
Normal file
981
priority_queue/vbt.go
Normal file
|
@ -0,0 +1,981 @@
|
|||
package pqueue
|
||||
|
||||
import (
|
||||
"474420502.top/eson/structure/compare"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
type tNode struct {
|
||||
children [2]*tNode
|
||||
parent *tNode
|
||||
size int
|
||||
value interface{}
|
||||
}
|
||||
|
||||
func (n *tNode) String() string {
|
||||
if n == nil {
|
||||
return "nil"
|
||||
}
|
||||
|
||||
p := "nil"
|
||||
if n.parent != nil {
|
||||
p = spew.Sprint(n.parent.value)
|
||||
}
|
||||
return spew.Sprint(n.value) + "(" + p + "|" + spew.Sprint(n.size) + ")"
|
||||
}
|
||||
|
||||
type vbTree struct {
|
||||
root *tNode
|
||||
Compare compare.Compare
|
||||
}
|
||||
|
||||
func newVBT(Compare compare.Compare) *vbTree {
|
||||
return &vbTree{Compare: Compare}
|
||||
}
|
||||
|
||||
func (tree *vbTree) String() string {
|
||||
str := "AVLTree\n"
|
||||
if tree.root == nil {
|
||||
return str + "nil"
|
||||
}
|
||||
output(tree.root, "", true, &str)
|
||||
return str
|
||||
}
|
||||
|
||||
func (tree *vbTree) Iterator() *vbtIterator {
|
||||
return initIterator(tree)
|
||||
}
|
||||
|
||||
func (tree *vbTree) Size() int {
|
||||
if tree.root == nil {
|
||||
return 0
|
||||
}
|
||||
return tree.root.size
|
||||
}
|
||||
|
||||
func (tree *vbTree) indexNode(idx int) *tNode {
|
||||
cur := tree.root
|
||||
if idx >= 0 {
|
||||
for cur != nil {
|
||||
ls := getSize(cur.children[0])
|
||||
if idx == ls {
|
||||
return cur
|
||||
} else if idx < ls {
|
||||
cur = cur.children[0]
|
||||
} else {
|
||||
idx = idx - ls - 1
|
||||
cur = cur.children[1]
|
||||
}
|
||||
}
|
||||
} else {
|
||||
idx = -idx - 1
|
||||
for cur != nil {
|
||||
rs := getSize(cur.children[1])
|
||||
if idx == rs {
|
||||
return cur
|
||||
} else if idx < rs {
|
||||
cur = cur.children[1]
|
||||
} else {
|
||||
idx = idx - rs - 1
|
||||
cur = cur.children[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tree *vbTree) Index(idx int) (interface{}, bool) {
|
||||
n := tree.indexNode(idx)
|
||||
if n != nil {
|
||||
return n.value, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { // 0 -1
|
||||
|
||||
if idx1^idx2 < 0 {
|
||||
if idx1 < 0 {
|
||||
idx1 = tree.root.size + idx1 - 1
|
||||
} else {
|
||||
idx2 = tree.root.size + idx2 - 1
|
||||
}
|
||||
}
|
||||
|
||||
if idx1 > idx2 {
|
||||
ok = true
|
||||
if idx1 >= tree.root.size {
|
||||
idx1 = tree.root.size - 1
|
||||
ok = false
|
||||
}
|
||||
|
||||
n := tree.indexNode(idx1)
|
||||
iter := NewIterator(n)
|
||||
result = make([]interface{}, 0, idx1-idx2)
|
||||
for i := idx2; i <= idx1; i++ {
|
||||
if iter.Prev() {
|
||||
result = append(result, iter.Value())
|
||||
} else {
|
||||
ok = false
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
} else {
|
||||
ok = true
|
||||
if idx2 >= tree.root.size {
|
||||
idx2 = tree.root.size - 1
|
||||
ok = false
|
||||
}
|
||||
|
||||
if n := tree.indexNode(idx1); n != nil {
|
||||
iter := NewIterator(n)
|
||||
result = make([]interface{}, 0, idx2-idx1)
|
||||
for i := idx1; i <= idx2; i++ {
|
||||
if iter.Next() {
|
||||
result = append(result, iter.Value())
|
||||
} else {
|
||||
ok = false
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (tree *vbTree) RemoveIndex(idx int) bool {
|
||||
n := tree.indexNode(idx)
|
||||
if n != nil {
|
||||
tree.removeNode(n)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (tree *vbTree) removeNode(n *tNode) {
|
||||
if tree.root.size == 1 {
|
||||
tree.root = nil
|
||||
// return n
|
||||
return
|
||||
}
|
||||
|
||||
ls, rs := getChildrenSize(n)
|
||||
if ls == 0 && rs == 0 {
|
||||
p := n.parent
|
||||
p.children[getRelationship(n)] = nil
|
||||
tree.fixSizeWithRemove(p)
|
||||
// return n
|
||||
return
|
||||
}
|
||||
|
||||
var cur *tNode
|
||||
if ls > rs {
|
||||
cur = n.children[0]
|
||||
for cur.children[1] != nil {
|
||||
cur = cur.children[1]
|
||||
}
|
||||
|
||||
cleft := cur.children[0]
|
||||
cur.parent.children[getRelationship(cur)] = cleft
|
||||
if cleft != nil {
|
||||
cleft.parent = cur.parent
|
||||
}
|
||||
|
||||
} else {
|
||||
cur = n.children[1]
|
||||
for cur.children[0] != nil {
|
||||
cur = cur.children[0]
|
||||
}
|
||||
|
||||
cright := cur.children[1]
|
||||
cur.parent.children[getRelationship(cur)] = cright
|
||||
|
||||
if cright != nil {
|
||||
cright.parent = cur.parent
|
||||
}
|
||||
}
|
||||
|
||||
cparent := cur.parent
|
||||
// 修改为interface 交换
|
||||
n.value, cur.value = cur.value, n.value
|
||||
|
||||
// 考虑到刚好替换的节点是 被替换节点的孩子节点的时候, 从自身修复高度
|
||||
if cparent == n {
|
||||
tree.fixSizeWithRemove(n)
|
||||
} else {
|
||||
tree.fixSizeWithRemove(cparent)
|
||||
}
|
||||
|
||||
// return cur
|
||||
return
|
||||
}
|
||||
|
||||
func (tree *vbTree) Remove(key interface{}) bool {
|
||||
|
||||
if n, ok := tree.GetNode(key); ok {
|
||||
tree.removeNode(n)
|
||||
return true
|
||||
}
|
||||
// return nil
|
||||
return false
|
||||
}
|
||||
|
||||
// Values 返回先序遍历的值
|
||||
func (tree *vbTree) Values() []interface{} {
|
||||
mszie := 0
|
||||
if tree.root != nil {
|
||||
mszie = tree.root.size
|
||||
}
|
||||
result := make([]interface{}, 0, mszie)
|
||||
tree.Traversal(func(v interface{}) bool {
|
||||
result = append(result, v)
|
||||
return true
|
||||
}, LDR)
|
||||
return result
|
||||
}
|
||||
|
||||
func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
||||
c := tree.Compare(k2, k1)
|
||||
switch c {
|
||||
case 1:
|
||||
|
||||
var min, max *tNode
|
||||
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.Next() {
|
||||
result = append(result, iter.Value())
|
||||
if iter.cur == max {
|
||||
break
|
||||
}
|
||||
}
|
||||
case -1:
|
||||
|
||||
var min, max *tNode
|
||||
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.Prev() {
|
||||
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 (tree *vbTree) Get(key interface{}) (interface{}, bool) {
|
||||
n, ok := tree.GetNode(key)
|
||||
if ok {
|
||||
return n.value, true
|
||||
}
|
||||
return n, false
|
||||
}
|
||||
|
||||
func (tree *vbTree) GetAround(key interface{}) (result [3]interface{}) {
|
||||
an := tree.getArountNode(key)
|
||||
for i, n := range an {
|
||||
if n != nil {
|
||||
result[i] = n.value
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (tree *vbTree) getArountNode(key interface{}) (result [3]*tNode) {
|
||||
var last *tNode
|
||||
var lastc int
|
||||
|
||||
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:
|
||||
iter := NewIterator(n)
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.Compare(iter.cur.value, n.value) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
result[1] = n
|
||||
n = nil
|
||||
default:
|
||||
panic("Get Compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
|
||||
switch lastc {
|
||||
case 1:
|
||||
|
||||
if result[1] != nil {
|
||||
|
||||
result[0] = getPrev(result[1], 1)
|
||||
result[2] = getNext(result[1], 1)
|
||||
} else {
|
||||
result[0] = last
|
||||
result[2] = getNext(last, 1)
|
||||
}
|
||||
|
||||
case -1:
|
||||
|
||||
if result[1] != nil {
|
||||
result[0] = getPrev(result[1], 1)
|
||||
result[2] = getNext(result[1], 1)
|
||||
} else {
|
||||
result[2] = last
|
||||
result[0] = getPrev(last, 1)
|
||||
}
|
||||
|
||||
case 0:
|
||||
|
||||
if result[1] == nil {
|
||||
return
|
||||
}
|
||||
result[0] = getPrev(result[1], 1)
|
||||
result[2] = getNext(result[1], 1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (tree *vbTree) GetNode(value interface{}) (*tNode, bool) {
|
||||
|
||||
for n := tree.root; n != nil; {
|
||||
switch c := tree.Compare(value, n.value); c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
case 1:
|
||||
n = n.children[1]
|
||||
case 0:
|
||||
iter := NewIterator(n)
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.Compare(iter.cur.value, n.value) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
return n, true
|
||||
default:
|
||||
panic("Get Compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (tree *vbTree) Put(key interface{}) *tNode {
|
||||
|
||||
node := &tNode{value: key, size: 1}
|
||||
if tree.root == nil {
|
||||
tree.root = node
|
||||
return node
|
||||
}
|
||||
|
||||
cur := tree.root
|
||||
parent := cur.parent
|
||||
child := -1
|
||||
|
||||
for {
|
||||
|
||||
if cur == nil {
|
||||
parent.children[child] = node
|
||||
node.parent = parent
|
||||
|
||||
fixed := parent.parent
|
||||
fsize := getSize(fixed)
|
||||
if fsize == 3 {
|
||||
lefts, rigths := getChildrenSize(fixed)
|
||||
tree.fix3Size(fixed, lefts, rigths)
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
if cur.size > 8 {
|
||||
ls, rs := cur.children[0].size, cur.children[1].size
|
||||
factor := cur.size / 10 // or factor = 1
|
||||
if rs >= ls*2+factor || ls >= rs*2+factor {
|
||||
tree.fixSize(cur, ls, rs)
|
||||
}
|
||||
}
|
||||
|
||||
cur.size++
|
||||
parent = cur
|
||||
c := tree.Compare(key, cur.value)
|
||||
child = (c + 2) / 2
|
||||
cur = cur.children[child]
|
||||
}
|
||||
}
|
||||
|
||||
type TraversalMethod int
|
||||
|
||||
const (
|
||||
// L = left R = right D = Value(dest)
|
||||
_ TraversalMethod = iota
|
||||
//DLR 先值 然后左递归 右递归 下面同理
|
||||
DLR
|
||||
//LDR 先从左边有序访问到右边 从小到大
|
||||
LDR
|
||||
// LRD 同理
|
||||
LRD
|
||||
|
||||
// DRL 同理
|
||||
DRL
|
||||
|
||||
// RDL 先从右边有序访问到左边 从大到小
|
||||
RDL
|
||||
|
||||
// RLD 同理
|
||||
RLD
|
||||
)
|
||||
|
||||
// Traversal 遍历的方法 默认是LDR 从小到大 Compare 为 l < r
|
||||
func (tree *vbTree) Traversal(every func(v interface{}) bool, traversalMethod ...interface{}) {
|
||||
if tree.root == nil {
|
||||
return
|
||||
}
|
||||
|
||||
method := LDR
|
||||
if len(traversalMethod) != 0 {
|
||||
method = traversalMethod[0].(TraversalMethod)
|
||||
}
|
||||
|
||||
switch method {
|
||||
case DLR:
|
||||
var traverasl func(cur *tNode) bool
|
||||
traverasl = func(cur *tNode) 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 LDR:
|
||||
var traverasl func(cur *tNode) bool
|
||||
traverasl = func(cur *tNode) bool {
|
||||
if cur == nil {
|
||||
return true
|
||||
}
|
||||
if !traverasl(cur.children[0]) {
|
||||
return false
|
||||
}
|
||||
if !every(cur.value) {
|
||||
return false
|
||||
}
|
||||
if !traverasl(cur.children[1]) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
traverasl(tree.root)
|
||||
case LRD:
|
||||
var traverasl func(cur *tNode) bool
|
||||
traverasl = func(cur *tNode) 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 *tNode) bool
|
||||
traverasl = func(cur *tNode) 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 *tNode) bool
|
||||
traverasl = func(cur *tNode) 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 *tNode) bool
|
||||
traverasl = func(cur *tNode) 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)
|
||||
}
|
||||
}
|
||||
|
||||
func (tree *vbTree) lrrotate3(cur *tNode) {
|
||||
const l = 1
|
||||
const r = 0
|
||||
|
||||
movparent := cur.children[l]
|
||||
mov := movparent.children[r]
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
|
||||
cur.children[r] = mov
|
||||
mov.parent = cur
|
||||
|
||||
cur.children[l] = movparent
|
||||
movparent.children[r] = nil
|
||||
|
||||
cur.children[r] = mov
|
||||
mov.parent = cur
|
||||
|
||||
// cur.size = 3
|
||||
// cur.children[r].size = 1
|
||||
cur.children[l].size = 1
|
||||
}
|
||||
|
||||
func (tree *vbTree) lrrotate(cur *tNode) {
|
||||
|
||||
const l = 1
|
||||
const r = 0
|
||||
|
||||
movparent := cur.children[l]
|
||||
mov := movparent.children[r]
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
|
||||
if mov.children[l] != nil {
|
||||
movparent.children[r] = mov.children[l]
|
||||
movparent.children[r].parent = movparent
|
||||
//movparent.children[r].child = l
|
||||
} else {
|
||||
movparent.children[r] = nil
|
||||
}
|
||||
|
||||
if mov.children[r] != nil {
|
||||
mov.children[l] = mov.children[r]
|
||||
//mov.children[l].child = l
|
||||
} else {
|
||||
mov.children[l] = nil
|
||||
}
|
||||
|
||||
if cur.children[r] != nil {
|
||||
mov.children[r] = cur.children[r]
|
||||
mov.children[r].parent = mov
|
||||
} else {
|
||||
mov.children[r] = nil
|
||||
}
|
||||
|
||||
cur.children[r] = mov
|
||||
mov.parent = cur
|
||||
|
||||
movparent.size = getChildrenSumSize(movparent) + 1
|
||||
mov.size = getChildrenSumSize(mov) + 1
|
||||
cur.size = getChildrenSumSize(cur) + 1
|
||||
}
|
||||
|
||||
func (tree *vbTree) rlrotate3(cur *tNode) {
|
||||
const l = 0
|
||||
const r = 1
|
||||
|
||||
movparent := cur.children[l]
|
||||
mov := movparent.children[r]
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
|
||||
cur.children[r] = mov
|
||||
mov.parent = cur
|
||||
|
||||
cur.children[l] = movparent
|
||||
movparent.children[r] = nil
|
||||
|
||||
cur.children[r] = mov
|
||||
mov.parent = cur
|
||||
|
||||
// cur.size = 3
|
||||
// cur.children[r].size = 1
|
||||
cur.children[l].size = 1
|
||||
}
|
||||
|
||||
func (tree *vbTree) rlrotate(cur *tNode) {
|
||||
|
||||
const l = 0
|
||||
const r = 1
|
||||
|
||||
movparent := cur.children[l]
|
||||
mov := movparent.children[r]
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
|
||||
if mov.children[l] != nil {
|
||||
movparent.children[r] = mov.children[l]
|
||||
movparent.children[r].parent = movparent
|
||||
} else {
|
||||
movparent.children[r] = nil
|
||||
}
|
||||
|
||||
if mov.children[r] != nil {
|
||||
mov.children[l] = mov.children[r]
|
||||
} else {
|
||||
mov.children[l] = nil
|
||||
}
|
||||
|
||||
if cur.children[r] != nil {
|
||||
mov.children[r] = cur.children[r]
|
||||
mov.children[r].parent = mov
|
||||
} else {
|
||||
mov.children[r] = nil
|
||||
}
|
||||
|
||||
cur.children[r] = mov
|
||||
mov.parent = cur
|
||||
|
||||
movparent.size = getChildrenSumSize(movparent) + 1
|
||||
mov.size = getChildrenSumSize(mov) + 1
|
||||
cur.size = getChildrenSumSize(cur) + 1
|
||||
}
|
||||
|
||||
func (tree *vbTree) rrotate3(cur *tNode) {
|
||||
const l = 0
|
||||
const r = 1
|
||||
// 1 right 0 left
|
||||
mov := cur.children[l]
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
|
||||
cur.children[r] = mov
|
||||
mov.size = 1
|
||||
|
||||
cur.children[l] = mov.children[l]
|
||||
cur.children[l].parent = cur
|
||||
|
||||
mov.children[l] = nil
|
||||
|
||||
mov.size = 1
|
||||
}
|
||||
|
||||
func (tree *vbTree) rrotate(cur *tNode) {
|
||||
|
||||
const l = 0
|
||||
const r = 1
|
||||
// 1 right 0 left
|
||||
mov := cur.children[l]
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
|
||||
// mov.children[l]不可能为nil
|
||||
mov.children[l].parent = cur
|
||||
cur.children[l] = mov.children[l]
|
||||
|
||||
// 解决mov节点孩子转移的问题
|
||||
if mov.children[r] != nil {
|
||||
mov.children[l] = mov.children[r]
|
||||
} else {
|
||||
mov.children[l] = nil
|
||||
}
|
||||
|
||||
if cur.children[r] != nil {
|
||||
mov.children[r] = cur.children[r]
|
||||
mov.children[r].parent = mov
|
||||
} else {
|
||||
mov.children[r] = nil
|
||||
}
|
||||
|
||||
// 连接转移后的节点 由于mov只是与cur交换值,parent不变
|
||||
cur.children[r] = mov
|
||||
|
||||
mov.size = getChildrenSumSize(mov) + 1
|
||||
cur.size = getChildrenSumSize(cur) + 1
|
||||
}
|
||||
|
||||
func (tree *vbTree) lrotate3(cur *tNode) {
|
||||
const l = 1
|
||||
const r = 0
|
||||
// 1 right 0 left
|
||||
mov := cur.children[l]
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
|
||||
cur.children[r] = mov
|
||||
mov.size = 1
|
||||
|
||||
cur.children[l] = mov.children[l]
|
||||
cur.children[l].parent = cur
|
||||
|
||||
mov.children[l] = nil
|
||||
|
||||
mov.size = 1
|
||||
}
|
||||
|
||||
func (tree *vbTree) lrotate(cur *tNode) {
|
||||
|
||||
const l = 1
|
||||
const r = 0
|
||||
// 1 right 0 left
|
||||
mov := cur.children[l]
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
|
||||
// mov.children[l]不可能为nil
|
||||
mov.children[l].parent = cur
|
||||
cur.children[l] = mov.children[l]
|
||||
|
||||
// 解决mov节点孩子转移的问题
|
||||
if mov.children[r] != nil {
|
||||
mov.children[l] = mov.children[r]
|
||||
} else {
|
||||
mov.children[l] = nil
|
||||
}
|
||||
|
||||
if cur.children[r] != nil {
|
||||
mov.children[r] = cur.children[r]
|
||||
mov.children[r].parent = mov
|
||||
} else {
|
||||
mov.children[r] = nil
|
||||
}
|
||||
|
||||
// 连接转移后的节点 由于mov只是与cur交换值,parent不变
|
||||
cur.children[r] = mov
|
||||
|
||||
mov.size = getChildrenSumSize(mov) + 1
|
||||
cur.size = getChildrenSumSize(cur) + 1
|
||||
}
|
||||
|
||||
func getChildrenSumSize(cur *tNode) int {
|
||||
return getSize(cur.children[0]) + getSize(cur.children[1])
|
||||
}
|
||||
|
||||
func getChildrenSize(cur *tNode) (int, int) {
|
||||
return getSize(cur.children[0]), getSize(cur.children[1])
|
||||
}
|
||||
|
||||
func getSize(cur *tNode) int {
|
||||
if cur == nil {
|
||||
return 0
|
||||
}
|
||||
return cur.size
|
||||
}
|
||||
|
||||
func (tree *vbTree) fixSizeWithRemove(cur *tNode) {
|
||||
for cur != nil {
|
||||
cur.size--
|
||||
if cur.size > 8 {
|
||||
ls, rs := getChildrenSize(cur)
|
||||
factor := cur.size / 10 // or factor = 1
|
||||
if rs >= ls*2+factor || ls >= rs*2+factor {
|
||||
tree.fixSize(cur, ls, rs)
|
||||
}
|
||||
}
|
||||
cur = cur.parent
|
||||
}
|
||||
}
|
||||
|
||||
func (tree *vbTree) fix3Size(cur *tNode, lefts, rigths int) {
|
||||
if lefts > rigths {
|
||||
l := cur.children[0]
|
||||
llsize, lrsize := getChildrenSize(l)
|
||||
if lrsize > llsize {
|
||||
tree.rlrotate3(cur)
|
||||
} else {
|
||||
tree.rrotate3(cur)
|
||||
}
|
||||
} else {
|
||||
r := cur.children[1]
|
||||
rlsize, rrsize := getChildrenSize(r)
|
||||
if rlsize > rrsize {
|
||||
tree.lrrotate3(cur)
|
||||
} else {
|
||||
tree.lrotate3(cur)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (tree *vbTree) fixSize(cur *tNode, lefts, rigths int) {
|
||||
if lefts > rigths {
|
||||
l := cur.children[0]
|
||||
llsize, lrsize := getChildrenSize(l)
|
||||
if lrsize > llsize {
|
||||
tree.rlrotate(cur)
|
||||
} else {
|
||||
tree.rrotate(cur)
|
||||
}
|
||||
} else {
|
||||
r := cur.children[1]
|
||||
rlsize, rrsize := getChildrenSize(r)
|
||||
if rlsize > rrsize {
|
||||
tree.lrrotate(cur)
|
||||
} else {
|
||||
tree.lrotate(cur)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func output(node *tNode, prefix string, isTail bool, str *string) {
|
||||
|
||||
if node.children[1] != nil {
|
||||
newPrefix := prefix
|
||||
if isTail {
|
||||
newPrefix += "│ "
|
||||
} else {
|
||||
newPrefix += " "
|
||||
}
|
||||
output(node.children[1], newPrefix, false, str)
|
||||
}
|
||||
*str += prefix
|
||||
if isTail {
|
||||
*str += "└── "
|
||||
} else {
|
||||
*str += "┌── "
|
||||
}
|
||||
|
||||
*str += spew.Sprint(node.value) + "\n"
|
||||
|
||||
if node.children[0] != nil {
|
||||
newPrefix := prefix
|
||||
if isTail {
|
||||
newPrefix += " "
|
||||
} else {
|
||||
newPrefix += "│ "
|
||||
}
|
||||
output(node.children[0], newPrefix, true, str)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func outputfordebug(node *tNode, prefix string, isTail bool, str *string) {
|
||||
|
||||
if node.children[1] != nil {
|
||||
newPrefix := prefix
|
||||
if isTail {
|
||||
newPrefix += "│ "
|
||||
} else {
|
||||
newPrefix += " "
|
||||
}
|
||||
outputfordebug(node.children[1], newPrefix, false, str)
|
||||
}
|
||||
*str += prefix
|
||||
if isTail {
|
||||
*str += "└── "
|
||||
} else {
|
||||
*str += "┌── "
|
||||
}
|
||||
|
||||
suffix := "("
|
||||
parentv := ""
|
||||
if node.parent == nil {
|
||||
parentv = "nil"
|
||||
} else {
|
||||
parentv = spew.Sprint(node.value)
|
||||
}
|
||||
suffix += parentv + "|" + spew.Sprint(node.size) + ")"
|
||||
*str += spew.Sprint(node.value) + suffix + "\n"
|
||||
|
||||
if node.children[0] != nil {
|
||||
newPrefix := prefix
|
||||
if isTail {
|
||||
newPrefix += " "
|
||||
} else {
|
||||
newPrefix += "│ "
|
||||
}
|
||||
outputfordebug(node.children[0], newPrefix, true, str)
|
||||
}
|
||||
}
|
||||
|
||||
func (tree *vbTree) debugString() string {
|
||||
str := "AVLTree\n"
|
||||
if tree.root == nil {
|
||||
return str + "nil"
|
||||
}
|
||||
outputfordebug(tree.root, "", true, &str)
|
||||
return str
|
||||
}
|
215
priority_queue/vbt_iterator.go
Normal file
215
priority_queue/vbt_iterator.go
Normal file
|
@ -0,0 +1,215 @@
|
|||
package pqueue
|
||||
|
||||
import (
|
||||
"474420502.top/eson/structure/lastack"
|
||||
)
|
||||
|
||||
type vbtIterator struct {
|
||||
dir int
|
||||
up *tNode
|
||||
cur *tNode
|
||||
tstack *lastack.Stack
|
||||
// curnext *tNode
|
||||
}
|
||||
|
||||
func initIterator(avltree *vbTree) *vbtIterator {
|
||||
iter := &vbtIterator{tstack: lastack.New()}
|
||||
iter.up = avltree.root
|
||||
return iter
|
||||
}
|
||||
|
||||
func NewIterator(n *tNode) *vbtIterator {
|
||||
iter := &vbtIterator{tstack: lastack.New()}
|
||||
iter.up = n
|
||||
return iter
|
||||
}
|
||||
|
||||
func (iter *vbtIterator) Value() interface{} {
|
||||
return iter.cur.value
|
||||
}
|
||||
|
||||
func (iter *vbtIterator) Left() bool {
|
||||
if iter.cur.children[0] != nil {
|
||||
iter.dir = 0
|
||||
iter.cur = iter.cur.children[0]
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (iter *vbtIterator) Right() bool {
|
||||
if iter.cur.children[1] != nil {
|
||||
iter.dir = 0
|
||||
iter.cur = iter.cur.children[1]
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func getNext(cur *tNode, idx int) *tNode {
|
||||
|
||||
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.(*tNode)
|
||||
if i == idx-1 {
|
||||
return iter.cur
|
||||
}
|
||||
iter.curPushNextStack(iter.cur)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return cur
|
||||
}
|
||||
|
||||
func (iter *vbtIterator) 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.(*tNode)
|
||||
iter.curPushNextStack(iter.cur)
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
func getPrev(cur *tNode, idx int) *tNode {
|
||||
|
||||
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.(*tNode)
|
||||
if i == idx-1 {
|
||||
return iter.cur
|
||||
}
|
||||
iter.curPushPrevStack(iter.cur)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return cur
|
||||
}
|
||||
|
||||
func (iter *vbtIterator) 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.(*tNode)
|
||||
iter.curPushPrevStack(iter.cur)
|
||||
return true
|
||||
}
|
||||
|
||||
// 如果再次计算的栈为空, 则只能返回false
|
||||
return false
|
||||
}
|
||||
|
||||
func getRelationship(cur *tNode) int {
|
||||
if cur.parent.children[1] == cur {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (iter *vbtIterator) getPrevUp(cur *tNode) *tNode {
|
||||
for cur.parent != nil {
|
||||
if getRelationship(cur) == 1 { // next 在 降序 小值. 如果child在右边, parent 比 child 小, parent才有效, 符合降序
|
||||
return cur.parent
|
||||
}
|
||||
cur = cur.parent
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (iter *vbtIterator) curPushPrevStack(cur *tNode) {
|
||||
Prev := cur.children[0] // 当前的左然后向右找, 找到最大, 就是最接近cur 并且小于cur的值
|
||||
|
||||
if Prev != nil {
|
||||
iter.tstack.Push(Prev)
|
||||
for Prev.children[1] != nil {
|
||||
Prev = Prev.children[1]
|
||||
iter.tstack.Push(Prev) // 入栈 用于回溯
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *vbtIterator) getNextUp(cur *tNode) *tNode {
|
||||
for cur.parent != nil {
|
||||
if getRelationship(cur) == 0 { // Prev 在 降序 大值. 如果child在左边, parent 比 child 大, parent才有效 , 符合降序
|
||||
return cur.parent
|
||||
}
|
||||
cur = cur.parent
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (iter *vbtIterator) curPushNextStack(cur *tNode) {
|
||||
next := cur.children[1]
|
||||
|
||||
if next != nil {
|
||||
iter.tstack.Push(next)
|
||||
for next.children[0] != nil {
|
||||
next = next.children[0]
|
||||
iter.tstack.Push(next)
|
||||
}
|
||||
}
|
||||
}
|
24
vbt/vbt.go
24
vbt/vbt.go
|
@ -26,11 +26,11 @@ func (n *Node) String() string {
|
|||
|
||||
type Tree struct {
|
||||
root *Node
|
||||
compare compare.Compare
|
||||
Compare compare.Compare
|
||||
}
|
||||
|
||||
func New(compare compare.Compare) *Tree {
|
||||
return &Tree{compare: compare}
|
||||
func New(Compare compare.Compare) *Tree {
|
||||
return &Tree{Compare: Compare}
|
||||
}
|
||||
|
||||
func (tree *Tree) String() string {
|
||||
|
@ -242,7 +242,7 @@ func (tree *Tree) Values() []interface{} {
|
|||
}
|
||||
|
||||
func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
||||
c := tree.compare(k2, k1)
|
||||
c := tree.Compare(k2, k1)
|
||||
switch c {
|
||||
case 1:
|
||||
|
||||
|
@ -329,7 +329,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
|
||||
for n := tree.root; n != nil; {
|
||||
last = n
|
||||
c := tree.compare(key, n.value)
|
||||
c := tree.Compare(key, n.value)
|
||||
switch c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
|
@ -341,7 +341,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
iter := NewIterator(n)
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.compare(iter.cur.value, n.value) == 0 {
|
||||
if tree.Compare(iter.cur.value, n.value) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
break
|
||||
|
@ -350,7 +350,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
result[1] = n
|
||||
n = nil
|
||||
default:
|
||||
panic("Get compare only is allowed in -1, 0, 1")
|
||||
panic("Get Compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -390,7 +390,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
|
||||
|
||||
for n := tree.root; n != nil; {
|
||||
switch c := tree.compare(value, n.value); c {
|
||||
switch c := tree.Compare(value, n.value); c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
case 1:
|
||||
|
@ -399,7 +399,7 @@ func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
|
|||
iter := NewIterator(n)
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.compare(iter.cur.value, n.value) == 0 {
|
||||
if tree.Compare(iter.cur.value, n.value) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
break
|
||||
|
@ -407,7 +407,7 @@ func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
|
|||
}
|
||||
return n, true
|
||||
default:
|
||||
panic("Get compare only is allowed in -1, 0, 1")
|
||||
panic("Get Compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
|
@ -450,7 +450,7 @@ func (tree *Tree) Put(key interface{}) {
|
|||
|
||||
cur.size++
|
||||
parent = cur
|
||||
c := tree.compare(key, cur.value)
|
||||
c := tree.Compare(key, cur.value)
|
||||
child = (c + 2) / 2
|
||||
cur = cur.children[child]
|
||||
}
|
||||
|
@ -478,7 +478,7 @@ const (
|
|||
RLD
|
||||
)
|
||||
|
||||
// Traversal 遍历的方法 默认是LDR 从小到大 compare 为 l < r
|
||||
// Traversal 遍历的方法 默认是LDR 从小到大 Compare 为 l < r
|
||||
func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...interface{}) {
|
||||
if tree.root == nil {
|
||||
return
|
||||
|
|
|
@ -27,11 +27,11 @@ func (n *Node) String() string {
|
|||
|
||||
type Tree struct {
|
||||
root *Node
|
||||
compare compare.Compare
|
||||
Compare compare.Compare
|
||||
}
|
||||
|
||||
func New(compare compare.Compare) *Tree {
|
||||
return &Tree{compare: compare}
|
||||
func New(Compare compare.Compare) *Tree {
|
||||
return &Tree{Compare: Compare}
|
||||
}
|
||||
|
||||
func (tree *Tree) String() string {
|
||||
|
@ -243,7 +243,7 @@ func (tree *Tree) Values() []interface{} {
|
|||
}
|
||||
|
||||
func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
||||
c := tree.compare(k2, k1)
|
||||
c := tree.Compare(k2, k1)
|
||||
switch c {
|
||||
case 1:
|
||||
|
||||
|
@ -330,7 +330,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
|
||||
for n := tree.root; n != nil; {
|
||||
last = n
|
||||
c := tree.compare(key, n.value)
|
||||
c := tree.Compare(key, n.value)
|
||||
switch c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
|
@ -342,7 +342,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
iter := NewIterator(n)
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.compare(iter.cur.value, n.value) == 0 {
|
||||
if tree.Compare(iter.cur.value, n.value) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
break
|
||||
|
@ -351,7 +351,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
result[1] = n
|
||||
n = nil
|
||||
default:
|
||||
panic("Get compare only is allowed in -1, 0, 1")
|
||||
panic("Get Compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -391,7 +391,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
|
||||
|
||||
for n := tree.root; n != nil; {
|
||||
switch c := tree.compare(value, n.value); c {
|
||||
switch c := tree.Compare(value, n.value); c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
case 1:
|
||||
|
@ -400,7 +400,7 @@ func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
|
|||
iter := NewIterator(n)
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.compare(iter.cur.value, n.value) == 0 {
|
||||
if tree.Compare(iter.cur.value, n.value) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
break
|
||||
|
@ -408,7 +408,7 @@ func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
|
|||
}
|
||||
return n, true
|
||||
default:
|
||||
panic("Get compare only is allowed in -1, 0, 1")
|
||||
panic("Get Compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
|
@ -451,7 +451,7 @@ func (tree *Tree) Put(key, value interface{}) {
|
|||
|
||||
cur.size++
|
||||
parent = cur
|
||||
c := tree.compare(key, cur.key)
|
||||
c := tree.Compare(key, cur.key)
|
||||
child = (c + 2) / 2
|
||||
cur = cur.children[child]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user