CPS 100 -- Spring 1996
Group Assignment: Trees
This assignment is due at 8:00 am, March 9. Submit one copy of
solutions per group. Submit a file with both code and information about
how long the assignment took and how the group worked together (i.e.,
incorporate the README into the file you submit). Use
submit100 trees trees.html
(the name of the file is trees.html and the name of the
assignment is trees)
Assume the following declarations have been made
struct Tnode
{
int info;
Tnode * left,* right;
Tnode(int val, Tnode * lbranch = 0, Tnode * rbranch = 0);
};
Tnode::Tnode(int val, Tnode * lbranch, Tnode * rbranch)
: info(val),
left(lbranch),
right(rbranch)
{}
The following function returns the height of a tree.
int TreeHeight(Tnode * t)
// postcondition: returns height of tree with root t
{
if (t == 0)
{
return 0;
}
else
{
return 1 + Max(TreeHeight(t->left),TreeHeight(t->right));
}
}
where the function Max returns the largest of its two integer
parameters.
Problem 1: NonLeafCount (2 points)
Write a function NonLeafCount that has a single parameter of
typeTnode *
and that returns the number of non-leaf nodes in the tree whose
root is pointed to by the parameter. You may find it useful to write a
boolean-valued function
IsLeaf that returns true if and only if it's
parameter is a leaf.
Tree2List (4 points)
Write a function Tree2List that converts a binary search tree
into a sorted linked list. You may find it useful to write auxiliary
function(s) called by Tree2List.
struct Node
{
int info;
Node * next;
Node(int val, Node * ptr = 0)
: info(val),
next(ptr)
{}
};
Node * Tree2List(Tnode * t)
// precondition: t is the root of a binary search tree
// postcondition: returns a pointer to the first node of a
// sorted linked list containing same values as t
Path Sum (4 points)
Write the function HasPathSum whose header is given below.
HasPathSum returns 1 if there exists some root-to-leaf path in
the tree whose root is t whose
nodes sum to target and returns 0 otherwise.
For example, in the tree shown below there are exactly four root-to-leaf
paths. The sums of the paths are 27, 22, 26, and 18. Thus the value
of HasPathSum(t,27) should be true and the value of
HasPathSum(t,30) should be false
(assuming t points to the
root of the tree --- the node whose info field has value 5.)
Tree Diameter (4 points)
The diameter of a tree (sometimes called the width) is the number
of nodes on the longest path between two leaves in the tree. The
diagram below shows two trees each with diameter nine, the leaves that
form the ends of a longest path are shaded (note that there is more than
one path in each tree of length nine, but no path longer than nine nodes).
In particular, note that the diameter of a tree T
is the largest of the
following quantities:
- the diameter of T's left subtree
- the diameter of T's right subtree
- the longest path between leaves that goes through the root of T
(this can be computed from the heights of the subtrees of T)
You are to write a function TreeDiameter that returns the
diameter of a binary tree. In writing TreeDiameter you may
call the function TreeHeight and Max discussed above.
int TreeDiameter(Tnode *t)
// postcondition: returns diameter of tree with root t
For full credit you should visit nodes in the tree the minimal number of
times possible, in particular the complexity of your code should be
O(n). This may entail writing an auxiliary function called from
TreeDiameter. In particular, you may need to write a function
that uses a reference parameters to pass back the height. I used a
function whose prototype is given below:
void DoDiameter(Tnode * t, int & height, int & diameter)
// postcondition: sets height to height of tree t,
// sets diameter to diameter of t
General Trees (4 points)
A tree in which each node can have an arbitray number of children, and
in which each node stores an integer, can be
implemented using the following declarations:
struct GTnode
{
int info;
GTnode * child; // first child of node
GTnode * sibling; // sibling of node
GTnode(int val, GTnode * kid = 0, GTnode * sib = 0)
: info(val),
child(kid),
sibling(sib)
{}
};
Write the routine TreeProduct whose header is given below.
TreeProduct returns the product of all the values stored in the
nodes of t. For example, it would return
12! = 479,001,600
for the tree diagrammed below. In the tree below, the solid arrows
represent the connections using the declaration for
GTnode
given above, the dotted arrows represent the ``conceptual''
children for each node of the tree.
By definition, the value returned for an empty tree is zero.
int TreeProduct(GTnode * t)
// postcondition: returns product of all nodes in t
// returns 0 if t is empty
Search/Isomorphism (12 points)
Two binary search trees
s and t are value equal if every value in
s is in t and every value in t is in
s. The shapes of the trees don't matter, the collection of
values stored in s must be the same as the collection stored
in t for trees to be value equal.
Part A (2 points)
Consider the functions below. The running time of
of IsValueEqual in the average case is NOT
O(n log n) (where both
s and t contain N nodes). Explain why
the running time is greater than O(n log n).
You MUST justify your
answer (with a recurrence relation/solution or by
reasoning and explanation) for credit.
bool InTree(const string & s, Tree * t)
// postcondition: returns true if s in t, otherwise returns false
{
if (t != 0)
{
if (t->info == s) return true;
return InTree(s, t->left) || InTree(s, t->right);
}
return false;
}
bool FirstInSecond(Tree * s, Tree * t)
// postcondition: returns true if all values of s are in t
// returns false otherwise
{
if (s != 0)
{
if (InTree(s->info,t))
{
return FirstInSecond(s->left,t) &&
FirstInSecond(s->right,t);
}
return false;
}
return true;
}
bool IsValueEqual(Tree * s, Tree * t)
// postcondition: returns true if s is value equal to t,
// returns false otherwise
{
return FirstInSecond(s,t) && FirstInSecond(t,s);
}
Part B (2 points)
Describe
an O(N) algorithm for determining if two trees are value equal.
Part C (4 points)
Two binary trees s and t are
equivalent if they
have the same shape; the values stored in the nodes do not affect
whether two trees are equivalent. In the diagram
below, the tree in the middle is NOT equivalent
to the other trees, but the tree on the right IS equivalent
to the tree on the left.
Write a function IsEquivalent that returns true if its two tree
parameters are equivalent and false otherwise. You must also give the
big-Oh running time of your function with a justification.
Part D (4 points)
Two trees s and t are isomorphic if
s can be transformed into t by swapping left
and right children of some of the nodes of s. The values in
the nodes are NOT not important in determining
isomorphism, only the shape is important. The trees
below are isomorphic because if the children of the nodes A, B, and G
in the tree on the left are swapped, the tree on the right is obtained.
Write a function IsIsomorphic that returns true if two trees
are isomorphic. You must
give the big-Oh complexity (in the average
case) of your function with a justification.