
Look at the subtree with root Elizabeth II. Which of the following is true?
public class Tree
{
private Node root;
class Node
{
public Object data;
public List<Node> children;
}
public Tree(Object rootData)
{
root = new Node();
root.data = rootData;
root.children = new ArrayList<Node>();
}
public void addSubtree(Tree subtree)
{
root.children.add(subtree.root);
}
. . .
}
What are the leaves of the tree r?
Tree u = new Tree("Fred");
Tree t = new Tree("Barney");
Tree s = new Tree("Dino");
Tree r = new Tree("Wilma");
r.addSubtree(s);
s.addSubtree(t);
s.addSubtree(u);
class Tree
{
. . .
class Node
{
. . .
public int size()
{
int sum = 0;
for (Node child : children) { sum = sum + child.size(); }
return 1 + sum;
}
}
public int size()
{
if (root == null) { return 0; }
else { return root.size(); }
}
}
What is correct about this implementation of the height method of a Tree class?
public class Tree
{
. . .
public int height() { return root.height(); }
class Node
{
int height()
{
int h = 0;
for (Node child : children) { h = Math.max(h, child.height()); }
return h + 1;
}
}
. . .
}
What is true about this implementation of the height methodof a Tree class?
public class Tree
{
. . .
public int height() { if (root == null) { return 1; } else { return root.height(); } }
class Node
{
int height()
{
int h = 0;
for (Node child : children) { h = Math.max(h, child.height()); }
return h;
}
}
. . .
}
What is the big-oh efficiency of size() and height() in terms of n, the number of nodes in the tree?