Your colleague is incorrect. A child node can't be fixed relative to its siblings until the subtrees rooted at the siblings have been properly formatted; only after the two subtrees have been properly formatted can the two children at the roots be placed relative to each other. This requires a bottom up sweep of the tree.
size_type represents a value able to hold the
maximum number of elements in a vector. Vectors don't provide the type
index_type to represent a value able to hold the largest possible index
for a vector. A colleague of yours speculates that index_type isn't
needed because size_type is good enough. What is your opinion of your
colleague's speculation? Explain.
Your colleague also speculates that vectors could provide only the type
index_type, which could also be used to represent size_type. What is
your opinion of this speculation? Explain.
In the first case, your colleague is correct. If n is a value of type
size_type, then the legal index values are 0 through n - 1, all of
which can be interpreted as values of type size_type.
In the second case, your colleague is incorrect. If n is a value of type
index_type representing the largest legal index for some vector, then the
vector has n + 1 elements, which is a value that may not be representable
as a value of type index_type.
For example, suppose index_type is equivalent to unsigned char, that
is, an eight-bit unsigned value. The largest possible vector index value is
255, and the vector itself would have 256 elements, a value not representable
in eight bits.
Your function should contain the fewest statements possible.
Remembering that a tree is either a node or a node with a list of other trees underneath, the algorithm for counting interior follows directly:
This algorithm leads directly to a simple, recursive function for counting interior nodes:
int count_interior_nodes(tree)
int count = 0
if tree.children.size() > 0
count = 1
for each c in tree.children
count += count_interior_nodes(c)
return count
static void t(int i) {
throw i + 1;
}
int main() {
int i;
try {
i = 1;
}
catch (int j) {
cout << "j = " << j << "\n";
}
t(i);
}
Unfortunately, the program, when run, never prints the message. Explain to your colleague what's going on.
The throw is taking place inside of the call to t(), but the call to
t() is taking place outside the scope of the try-catch statement. Because
the throw is taking place outside the scope of the try, the associated
catch cannot receive the exception because it doesn't know about it.
This page last modified on 11 October 2001.