UP | HOME

Chapter 10.4

Table of Contents

ch10.4

10.4-1

ch10-4-1.png

10.4-2

PRINT-BINARY-TREE-KEYS(T)
    PRINT-NODE-KEYS(T.root)

PRINT-NODE-KEYS(x)
    if x != NIL
        PRINT(x.key)
        PRINT-NODE-KEYS(x.left)
        PRINT-NODE-KEYS(x.right)

10.4-3

PRINT-BINARY-TREE-KEYS(T)
    let s to be an empty stack
    PUSH(s, T.root)
    while not EMPTY?(s):
        x = POP(s)
        if x != NIL:
            PRINT(x.key)
            PUSH(x.left)
            PUSH(x.right)

10.4-4

PRINT-LCRS-TREE-KEYS(T)
    PRINT-LCRS-NODE-KEYS(T.root)

PRINT-LCRS-NODE-KEYS(x)
    if x != NIL
        PRINT(x.key)
        PRINT-LCRS-NODE-KEYS(x.left-child)
        PRINT-LCRS-NODE-KEYS(x.right-sibling)

10.4-5

PRINT-BINARY-TREE-KEYS(T)
    // in-order binary tree traversal
    x = T.root
    left_done = FALSE
    while x != NIL
        if not left_done:
            while x.left != NIL
                x = x.left
            left_done = TRUE
        print(x.key)
        if x.right != NIL
            x = x.right
            left_done = FALSE
        else while x.p != NIL and x == x.p.right
                x = x.p
            x = x.p

10.4-6

We can use two pointers left-child and right-sibling and one boolean value is-last-child to represent an arbitrary rooted tree. If the node is the rightmost child of its parent, then the is-last-child value of the node is true, and the right-sibling pointer of the node points to its parent.

Author: pedh

Created: 2023-08-17 Thu 10:36

Validate