Short Answers Section 9.2 Tree Representations
|
Draw a complete binary tree with exactly six nodes. Put a different
value in each node. Then draw an array with six components and show
where each of the six node values would be placed in the array (using the
usual array representation of a complete binary tree).
Write the instance variables for a new class that could be used for a node in a tree
where: (1) Each node contains int data, (2) Each node has up to four
children, and (3) Each node also has a reference to its parent. Store the
references to the children in an array of four components.
Short Answers Section 9.3 A Toolkit for Binary Tree Nodes
|
Draw a binary taxonomy tree that can be used for these four animals:
Rabbit, Horse, Whale, Snake.
Using the BTNode class from Chapter 9,
write a new static method of the BTNode class to meet the following specification.
No recursion is needed.
public static void subswap(BTNode root)
// Precondition: Root is the root reference of a binary tree.
// Postcondition: If root is non-null, then the original left subtree below
// this root has been moved and is now the right subtree, and the original
// right subtree is now the left subtree.
// Example original tree: Example new tree:
// 1 1
// / \ / \
// 2 3 3 2
// / \ / \
// 4 5 4 5
Redo the previous problem as a new non-static BTNode method.
Using the BTNode class from Chapter 9,
write a new static method of the BTNode class to meet the following specification.
public static void flip(BTNode root)
// Precondition: Root is the root reference of a binary tree.
// Postcondition: If the root is non-null, then the tree below this node is
// now the mirror image of its original value.
// Example original tree: Example new tree:
// 1 1
// / \ / \
// 2 3 3 2
// / \ / \
// 4 5 5 4
Redo the previous problem as a new non-static BTNode method.
Short Answers Section 9.4 Tree Traversals
|
Here is a small binary tree:
14
/ \
2 11
/ \ / \
1 3 10 30
/ /
7 40
Write the order of the nodes visited in:
A. An in-order traversal:
B. A pre-order traversal:
C. A post-order traversal:
Suppose IntBTNode is a BTNode (from Chapter 9) with integer data.
Write a new static method of the IntBTNode class to meet the following specification.
public static void increase(IntBTNode root)
// Precondition: root is the root reference of a binary tree.
// Postcondition: Every node of the tree has had its data
// increased by one.
Redo the previous problem as a new non-static BTNode method.
Using the BTNode class from Chapter 9,
write a new static method of the BTNode class to meet the following specification.
public static int manyNodes(BTNode root)
// Precondition: root_ptr is the root reference of a binary tree.
// Postcondition: The return value is the number of nodes in the tree.
// NOTES: The empty tree has 0 nodes, and a tree with just a root has
// 1 node.
Using the BTNode class from Chapter 9,
write a new static method of the BTNode class to meet the following specification.
public static int treeDepth(BTNode root)
// Precondition: root_ptr is the root reference of a binary tree.
// Postcondition: The return value is the depth of the binary tree.
// NOTES: The empty tree has a depth of -1 and a tree with just a root
// has a depth of 0.
Suppose IntBTNode is a BTNode (from Chapter 9) with integer data.
Write a new static method of the IntBTNode class to meet the following specification.
public static int count42(IntBTNode root)
// Precondition: root is the root reference of a binary tree (but
// NOT NECESSARILY a search tree).
// Postcondition: The return value indicates how many times 42 appears
// in the tree. NOTE: If the tree is empty, the method returns zero.
Suppose IntBTNode is a BTNode (from Chapter 9) with integer data.
Write a new static method of the IntBTNode class to meet the following specification.
public static boolean has42(IntBTNode root)
// Precondition: root is the root reference of a binary tree (but
// NOT NECESSARILY a search tree).
// Postcondition: The return value indicates whether 42 appears somewhere
// in the tree. NOTE: If the tree is empty, the method returns false.
Suppose IntBTNode is a BTNode (from Chapter 9) with integer data.
Write a new static method of the IntBTNode class to meet the following specification.
public static boolean all42(IntBTNode root)
// Precondition: root is the root reference of a binary tree (but
// NOT NECESSARILY a search tree).
// Postcondition: The return value is true if every node in the tree
// contains 42. NOTE: If the tree is empty, the method returns true.
Suppose IntBTNode is a BTNode (from Chapter 9) with integer data.
Write a new static method of the IntBTNode class to meet the following specification.
public static int sum(IntBTNode root)
// Precondition: root is the root reference of a binary tree.
// Postcondition: The return value is the sum of all the data in all the nodes.
// NOTES: The return value for the empty tree is zero.
Short Answers Section 9.5 Binary Search Trees
|
Suppose that we want to create a binary search tree where each node contains
information of some data type.
What additional factor is required for the data type?
Suppose that a binary search tree contains the number 42 at a node with
two children. Write two or three clear sentences to describe the process
required to delete the 42 from the tree.
Suppose IntBTNode is a BTNode (from Chapter 9) with integer data.
Write a new static method of the IntBTNode class to meet the following specification.
Make the method as efficient as
possible (do not visit nodes unnecessarily).
public static int count42(BTNode root)
// Precondition: root is the root reference of a binary SEARCH tree.
// Postcondition: The return value indicates how many times 42 appears
// in the tree.
Suppose IntBTNode is a BTNode (from Chapter 9) with integer data.
Write a new static method of the IntBTNode class to meet the following specification.
Make the method as efficient as
possible (do not visit nodes unnecessarily).
public static int max(BTNode root)
// Precondition: root is the root reference of a nonempty binary SEARCH
// tree.
// Postcondition: The return value is the largest value in the tree.
Suppose IntBTNode is a BTNode (from Chapter 9) with integer data.
Write a new static method of the IntBTNode class to meet the following specification.
Make the method as efficient as
possible (do not visit nodes unnecessarily).
void insert42(BTNode root)
// Precondition: root is the root reference of a binary SEARCH tree.
// Postcondition: One copy of the number 42 has been added to the binary
// search tree.