CS5302 Tutorial6 (Week 6) Boolean isAncestor(T, u, v):

advertisement
CS5302 Tutorial6 (Week 6)
Question 1: Write a program to include isAncestor (T, u, v) as a method of the class
TreePost. (Modify the program TreePost.java in week 5)
Boolean isAncestor(T, u, v):
Input: the tree T, node u and node v.
Output: true if u is an ancestor of v. False if u is NOT an ancestor of v.
Extra Question. The idea is interesting. If you do not have time to write the program,
you should at least know the algorithm.
Question 2. Given an O(n)-time algorithm for computing the depth of all the nodes of
a binary tree, where n is the number of nodes of T. Can you write a program to do
that? (To write the program you can combine TreePost.java and TreeDepth.java given
in week 5.)
Download