发现已经做了位置处理, 不需要再去改名

This commit is contained in:
2019-07-22 02:02:05 +08:00
parent 9263c03525
commit 76f81b9920
18 changed files with 272 additions and 167 deletions

View File

@@ -22,6 +22,43 @@ func loadTestData() []int {
return l
}
func TestIteratorHeadTail(t *testing.T) {
tree := New(compare.Int)
for _, v := range []int{1, 2, 7, 4, 5, 6, 7, 14, 15, 20, 30, 21, 3} {
tree.Put(v)
}
// ` AVLTree
// │ ┌── 30
// │ │ └── 21
// │ ┌── 20
// │ │ └── 15
// └── 14
// │ ┌── 7
// │ ┌── 7
// │ │ └── 6
// └── 5
// │ ┌── 4
// │ │ └── 3
// └── 2
// └── 1`
iter := tree.Iterator()
iter.Prev()
if iter.Value() != 14 {
t.Error("iter.Value() != ", 14, " value =", iter.Value())
}
iter.ToHead()
if iter.Value() != 1 {
t.Error("iter.Value() != ", 14, " value =", iter.Value())
}
iter.ToTail()
if iter.Value() != 30 {
t.Error("iter.Value() != ", 30, " value =", iter.Value())
}
}
func TestIterator(t *testing.T) {
tree := New(compare.Int)
for _, v := range []int{1, 2, 7, 4, 5, 6, 7, 14, 15, 20, 30, 21, 3} {

View File

@@ -30,6 +30,16 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator {
return iter
}
func (iter *Iterator) ToHead() {
for iter.Prev() {
}
}
func (iter *Iterator) ToTail() {
for iter.Next() {
}
}
func (iter *Iterator) GetNode() *Node {
return iter.cur
}
@@ -44,24 +54,6 @@ func (iter *Iterator) Value() interface{} {
return iter.cur.value
}
func (iter *Iterator) Left() bool {
if iter.cur.children[0] != nil {
iter.dir = 0
iter.cur = iter.cur.children[0]
return true
}
return false
}
func (iter *Iterator) Right() bool {
if iter.cur.children[1] != nil {
iter.dir = 0
iter.cur = iter.cur.children[1]
return true
}
return false
}
func (iter *Iterator) GetNext(cur *Node, idx int) *Node {
// iter := NewIterator(cur)