修复top为nil

This commit is contained in:
eson 2019-03-23 05:08:34 +08:00
parent b9fa01abdb
commit 6a6596cee5
3 changed files with 6 additions and 4 deletions

View File

@ -80,7 +80,7 @@ func TestIterator(t *testing.T) {
// └── 1`
iter := avl.Iterator() // root start point
t.Error(iter.cur, spew.Sdump(iter.tstack))
l := []int{14, 15, 20, 21, 30}
for i := 0; iter.Prev(); i++ {
@ -95,7 +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)
t.Error(iter.cur, spew.Sdump(iter.tstack))
for i := 0; iter.Next(); i++ { // cur is 30 next is 21
if iter.Value().(int) != l[i] {
t.Error(iter.Value())

View File

@ -3,6 +3,8 @@ package avl
import (
"log"
"github.com/davecgh/go-spew/spew"
"474420502.top/eson/structure/lastack"
)
@ -186,7 +188,7 @@ func (iter *Iterator) curPushNextStack(cur *Node) {
next := cur.children[0] // 当前的左然后向右找, 找到最大, 就是最接近cur 并且小于cur的值
if next != nil {
log.Println(iter.tstack)
log.Println(spew.Sdump(iter.tstack))
iter.tstack.Push(next)
for next.children[1] != nil {
next = next.children[1]

View File

@ -62,7 +62,7 @@ func New() *Stack {
func (as *Stack) Clear() {
as.size = 0
as.top = nil
as.top = &Node{elements: make([]interface{}, 8, 8), cur: -1}
as.cache = nil
}