10.4-1
Draw the binary tree rooted at index $6$ that is represented by the following attributes:
$$ \begin{array}{cccc} \text{index} & key & left & right \\ \hline 1 & 12 & 7 & 3 \\ 2 & 15 & 8 & \text{NIL} \\ 3 & 4 & 10 & \text{NIL} \\ 4 & 10 & 5 & 9 \\ 5 & 2 & \text{NIL} & \text{NIL} \\ 6 & 18 & 1 & 4 \\ 7 & 7 & \text{NIL} & \text{NIL} \\ 8 & 14 & 6 & 2 \\ 9 & 21 & \text{NIL} & \text{NIL} \\ 10 & 5 & \text{NIL} & \text{NIL} \end{array} $$
10.4-2
Write an $O(n)$-time recursive procedure that, given an $n$-node binary tree, prints out the key of each node in the tree.
1 2 3 4 5 6 |
|
10.4-3
Write an O$(n)$-time nonrecursive procedure that, given an $n$-node binary tree, prints out the key of each node in the tree. Use a stack as an auxiliary data structure.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
10.4-4
Write an $O(n)$-time procedure that prints all the keys of an arbitrary rooted tree with $n$ nodes, where the tree is stored using the left-child, right-sibling representation.
1 2 3 4 5 6 7 8 9 10 11 |
|
10.4-5 $\star$
Write an $O(n)$-time nonrecursive procedure that, given an $n$-node binary tree, prints out the key of each node. Use no more than constant extra space outside of the tree itself and do not modify the tree, even temporarily, during the procedure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
10.4-6 $\star$
The left-child, right-sibling representation of an arbitrary rooted tree uses three pointers in each node: left-child, right-sibling, and parent. From any node, its parent can be reached and identified in constant time and all its children can be reached and identified in time linear in the number of children. Show how to use only two pointers and one boolean value in each node so that the parent of a node or all of its children can be reached and identified in time linear in the number of children.
Use boolean to identify the last sibling, and the last sibling's right-sibling points to the parent.