Answer:
We can compute the diameter of the tree T by a pruning procedure, starting at the leaves (external nodes).
- Remove all leaves of T. Let the remaining tree be T1.
- Remove all leaves of T1. Let the remaining tree be T2.
- Repeat the "remove" operation as follows: Remove all leaves of Ti. Let remaining tree be Ti+1.
- When the remaining tree has only one node or two nodes, stop! Suppose now the remaining tree is Tk.
- If Tk has only one node, that is the center of T. The diameter of T is 2k.
- If Tk has two nodes, either can be the center of T. The diameter of T is 2k+1.
Step-by-step explanation:
We can compute the diameter of the tree T by a pruning procedure, starting at the leaves (external nodes).
- Remove all leaves of T. Let the remaining tree be T1.
- Remove all leaves of T1. Let the remaining tree be T2.
- Repeat the "remove" operation as follows: Remove all leaves of Ti. Let remaining tree be Ti+1.
- When the remaining tree has only one node or two nodes, stop! Suppose now the remaining tree is Tk.
- If Tk has only one node, that is the center of T. The diameter of T is 2k.
- If Tk has two nodes, either can be the center of T. The diameter of T is 2k+1.