structure/avl/iterator.go

157 lines
3.2 KiB
Go
Raw Normal View History

2019-03-12 11:18:42 +00:00
package avl
import (
"474420502.top/eson/structure/lastack"
2019-03-12 11:18:42 +00:00
)
type Iterator struct {
op *Tree
2019-03-12 11:18:42 +00:00
2019-03-12 17:59:52 +00:00
dir int
up *Node
cur *Node
tstack *lastack.Stack
2019-03-12 11:18:42 +00:00
// curnext *Node
}
func initIterator(avltree *Tree) *Iterator {
iter := &Iterator{op: avltree, tstack: lastack.New()}
2019-03-12 17:59:52 +00:00
iter.up = avltree.root
2019-03-12 11:18:42 +00:00
return iter
}
2019-03-15 11:57:25 +00:00
func NewIterator(tree *Tree) *Iterator {
return initIterator(tree)
}
2019-03-12 11:18:42 +00:00
func (iter *Iterator) Value() interface{} {
return iter.cur.value
}
2019-03-13 11:09:24 +00:00
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) Prev() (result bool) {
if iter.dir > -1 {
if iter.dir == 1 && iter.cur != nil {
iter.tstack.Clear()
iter.curPushPrevStack(iter.cur)
iter.up = iter.getPrevUp(iter.cur)
}
iter.dir = -1
}
if iter.tstack.Size() == 0 {
if iter.up == nil {
return false
}
iter.tstack.Push(iter.up)
iter.up = iter.getPrevUp(iter.up)
}
if v, ok := iter.tstack.Pop(); ok {
iter.cur = v.(*Node)
iter.curPushPrevStack(iter.cur)
return true
}
return false
}
2019-03-12 11:18:42 +00:00
func (iter *Iterator) Next() (result bool) {
2019-03-13 11:09:24 +00:00
if iter.dir < 1 { // 非 1(next 方向定义 -1 为 prev)
if iter.dir == -1 && iter.cur != nil { // 如果上次为prev方向, 则清空辅助计算的栈
iter.tstack.Clear()
iter.curPushNextStack(iter.cur) // 把当前cur计算的逆向回朔
iter.up = iter.getNextUp(iter.cur) // cur 寻找下个要计算up
2019-03-12 17:59:52 +00:00
}
iter.dir = 1
2019-03-12 11:18:42 +00:00
}
2019-03-13 11:09:24 +00:00
// 如果栈空了, 把up的递归计算入栈, 重新计算 下次的up值
2019-03-12 17:59:52 +00:00
if iter.tstack.Size() == 0 {
2019-03-13 11:09:24 +00:00
if iter.up == nil {
2019-03-12 17:59:52 +00:00
return false
}
2019-03-13 11:09:24 +00:00
iter.tstack.Push(iter.up)
iter.up = iter.getNextUp(iter.up)
2019-03-12 17:59:52 +00:00
}
if v, ok := iter.tstack.Pop(); ok {
2019-03-12 11:18:42 +00:00
iter.cur = v.(*Node)
iter.curPushNextStack(iter.cur)
return true
}
2019-03-13 11:09:24 +00:00
// 如果再次计算的栈为空, 则只能返回false
2019-03-12 11:18:42 +00:00
return false
}
func getRelationship(cur *Node) int {
if cur.parent.children[1] == cur {
return 1
}
return 0
}
2019-03-13 06:34:20 +00:00
func (iter *Iterator) getNextUp(cur *Node) *Node {
for cur.parent != nil {
if getRelationship(cur) == 1 { // next 在 降序 小值. 如果child在右边, parent 比 child 小, parent才有效, 符合降序
2019-03-13 06:34:20 +00:00
return cur.parent
}
cur = cur.parent
}
2019-03-13 06:45:27 +00:00
return nil
2019-03-13 06:34:20 +00:00
}
2019-03-12 11:18:42 +00:00
func (iter *Iterator) curPushNextStack(cur *Node) {
2019-03-13 11:09:24 +00:00
next := cur.children[0] // 当前的左然后向右找, 找到最大, 就是最接近cur 并且小于cur的值
2019-03-12 11:18:42 +00:00
if next != nil {
2019-03-12 17:59:52 +00:00
iter.tstack.Push(next)
2019-03-12 11:18:42 +00:00
for next.children[1] != nil {
next = next.children[1]
2019-03-13 11:09:24 +00:00
iter.tstack.Push(next) // 入栈 用于回溯
2019-03-12 17:59:52 +00:00
}
}
}
2019-03-13 06:34:20 +00:00
func (iter *Iterator) getPrevUp(cur *Node) *Node {
for cur.parent != nil {
if getRelationship(cur) == 0 { // Prev 在 降序 大值. 如果child在左边, parent 比 child 大, parent才有效 , 符合降序
2019-03-13 06:34:20 +00:00
return cur.parent
}
cur = cur.parent
}
2019-03-13 06:45:27 +00:00
return nil
2019-03-13 06:34:20 +00:00
}
2019-03-12 17:59:52 +00:00
func (iter *Iterator) curPushPrevStack(cur *Node) {
prev := cur.children[1]
if prev != nil {
iter.tstack.Push(prev)
for prev.children[0] != nil {
prev = prev.children[0]
iter.tstack.Push(prev)
2019-03-12 11:18:42 +00:00
}
}
}