func main() { d := ds.NewArrayStack() d.Push(1) d.Push(2) d.Push(3) ds.Test_PrintTree() }
func (this *Tree) Inorder_with_stack_2() { n := this.Root stack := ds.NewArrayStack() for stack.Size() > 0 || n != nil { if n != nil { stack.Push(n) n = n.left } else { t, _ := stack.Pop().(*Node) n = t.right } } }
func (this *RedBlackTree) String() string { s := ds.NewArrayStack() v := this.root for v != nil && v != Nil { s.Push(v) v = v.left } str := "[" for s.Size() > 0 { pre := s.Pop() a, _ := pre.(*rbNode) str = fmt.Sprint(str, a.value, ",") rNode := a.right for rNode != nil && rNode != Nil { s.Push(rNode) rNode = rNode.left } } return str }
func (this *Tree) Inorder_with_stack() { stack := ds.NewArrayStack() root := this.Root // 首先把根节点下面的所有左节点全部进栈 for root != nil { stack.Push(root) root = root.left } // 首先打印最小数据,然后打印这个节点的右子树 // 如果这个右子树有左节点时,再进栈 for stack.Size() > 0 { t, ok := (stack.Pop()).(*Node) if ok { fmt.Println(t.Value) t = t.right for t != nil { stack.Push(t) t = t.left } } } }