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