Actually i just need to pass the root node to function. Dont really have to compare the left and right subtree, right?

04-08-2004

elad

The initial node evaluated in Salems function will be the root node of the entire tree. However, note that findMaxHt() as posted is recursive. That is, within each call to findMaxHt() there will be two further calls to findMaxHt() (one for left subtree of current node and one for righ subtree of current node) or none, if current node is a leaf. In effect, with each call to findMaxHt() the current node acts as the root node for its own subtree.

04-08-2004

Raison

OK I understand now for the max ht. Thanks alot!

So does anyone know how to solve the first question? I been thinking about it for very long. And i cant think of a way to find the min ht of the tree, which probably need the use of a queue ADT.

04-09-2004

Salem

> So does anyone know how to solve the first question?
Well first you find the min height, using the examples provided above

Then pass that min height to another function which walks the tree. If you reach a leaf node and your path is the same length as the min height, then print it.

04-09-2004

Raison

Quote:

Originally Posted by Salem

Well first you find the min height, using the examples provided above

Then pass that min height to another function which walks the tree. If you reach a leaf node and your path is the same length as the min height, then print it.

Can u give me an rough idea on how to find the min ht and what to do after that?

Cos i figured it out even if i know the min ht. I need to find a way to start traversing from the root to print each shortest path, which i dont know how too.

You always return when you reach a leaf (rt==NULL)
If at the same time, minHt is also 0, then you've also found a shortest path as well, so print it.

04-09-2004

Raison

Take the following tree as example:

Clearly, the min ht of the tree is at 12 and it is the only shortest path. But i will reach the left child of 6 (which is an empty node) first before reaching the path to 12 and they are at the same ht.

SO i should CHECK for the node to be a leaf node and is at min ht.
I amended the code. Correct me if i am wrong. Thanks.

// the transversing may reach an empty node before
// reaching minHt==0
Not if you're printing the shortest path it can't

If it is a shortest path, then minHt == 0 when rt == NULL
If it is a long path, then minHt reaches 0 before rt becomes NULL. At this point, you can stop the recursion (because nothing below this node can be a shortest path either).

04-09-2004

Raison

Code:

10
/ \
6 15
\ \
7 12
/ \
4 30

The min ht of this tree is 3. The left child of 6 is an empty node is at ht 1. Since we transverse left first, we reach the empty left child of 6 before reaching the min ht. We will end pushing an empty node and the program will try to transverse further.