a class Tree . This should have at least the attributes listed below, together w
ID: 3721104 • Letter: A
Question
a class Tree. This should have at least the attributes listed below, together with the appropriate methods for accessing them:
_name or _id: the name of the node. For leaf nodes, this should be the ID of the corresponding organism. For non-leaf nodes, one simple way to indicate that the node is not a leaf is to set this attribute to None.
_left and _right: references to the left and right subtrees of the node.
Additionally, you may want to have an attribute at each node giving the set of IDs of all of the leaf nodes "under" that node: this can be useful when computing the similarity between two trees. This is not a requirement, just a suggestion.
Explanation / Answer
class Leaf
{
int _id;
Leaf _left, _right;
public Leaf(int item)
{
_id = item;
_left = _right = null;
}
public Leaf()
{
_id = none;
_left = _right = null;
}
}
class Tree
{
// Root of the Tree
Leaf root;
Tree()
{
root = null;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.