PRINT-BINARY-TREE-RECURSIVE(x) if x == NIL return print x.key PRINT-BINARY-TREE-RECURSIVE(x.left) PRINT-BINARY-TREE-RECURSIVE(x.right)
10.3-3
1 2 3 4 5 6 7 8 9 10 11 12
PRINT-BINARY-TREE-NONRECURSIVE(x) let S be a new STACK p = x while not STACK-EMPTY(S) or p != NIL // 一直沿着左子节点走,如果发现走不下去,那么回退父节点,并且开始走右子节点。 while p != NIL print p.key PUSH(S, p) p = p.left if not STACK-EMPTY(S) p = POP(S) p = p.right
10.3-4
1 2 3 4 5 6 7 8
PRINT-TREE(x) if x == NIL return print x.key p = x.left-child while p != NIL PRINT-TREE(p) p = p.right-sibling
PRINT-BINARY-TREE-NONRECURSIVE-CONSTANT-SPACE(x) Let S be a new STACK. pre = NIL q = x while q != NIL if q.p == pre // 说明上一次访问的是q的父节点,说明子树q还没被访问过。 print q.key pre = q // 尝试向下访问 if q.left != NIL q = q.left else if q.right != NIL q = q.right // 这是个叶节点,返回。 else q = q.p else if q.left == pre and q.right != NIL // 如果左子树为空,右子树非空,可以发现不会从这里进入右子树。 pre = q q = q.right else // 左右子树都访问过,返回到父节点。 pre = q q = q.p