如果要保存最大值, 必须要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
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