如果要保存最大值, 必须要Node交换不能值指针交换

This commit is contained in:
2019-03-25 01:40:12 +08:00
parent 51acc649a4
commit dd4c76f144
11 changed files with 1447 additions and 149 deletions

View File

@@ -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]
}