The German mathematician Ernst Paul Heinz Prüfer discovered that any labelled tree (T) with n vertices can be uniquely represented by a list (S) of n-2 integers. Your assignment is to implement his algorithm.
To find the sequence, S, given a tree, T:
You may assume that the input trees are valid trees. (But do check that the labels the user enters are integers, rather than characters, for example.)
In this assignment, since the trees' vertices are numbered from 1 to n, we'll have one extra row and column Here's a representation for the following tree with 5 vertices (all the trees in this assignment will have no more than 5 vertices).
0 1 2 3 4 5
0 - - - - - -
1 - F T F F F Tree: 1-2-3-4-5
2 - T F T F F
3 - F T F T F
4 - F F T F T
5 - F F F T F
(Note that the dashes in the 0 row and column would actually be set to false; I put them there so it would be easier to see the important parts of the matrix.)
The Prüfer Code of a tree, S, with n vertices can easily be represented as an integer array containing n-2 elements.
This program will prompt you for a description of a 5-vertex tree,
display the tree, and calculate its Pruefer Code.
Please enter the 4 edges of your tree, one at a time.
Edge #1
First Vertex: 1
Second Vertex: 2
Edge #2
First Vertex: 2
Second Vertex: 3
Edge #3
First Vertex: 3
Second Vertex: 4
Edge #4
First Vertex: 2
Second Vertex: 5
The tree is given by the following matrix:
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 1 0 0 0
2 0 1 0 1 0 1
3 0 0 1 0 1 0
4 0 0 0 1 0 0
5 0 0 1 0 0 0
The Pruefer Code is: 0 2 3 2 0
Press any key to continue