Possible edges of a tree for given diameter, height and vertices
Find a tree with the given values and print the edges of the tree. Print “-1”, if the tree is not possible.
Given three integers n, d and h.
n -> Number of vertices. [1, n]
d -> Diameter of the tree (largest
distance between two vertices).
h -> Height of the tree (longest distance
between vertex 1 and another vertex)
Examples :
Input : n = 5, d = 3, h = 2
Output : 1 2
2 3
1 4
1 5
Explanation :
We can see that the height of the tree is 2 (1 ->
2 --> 5) and diameter is 3 ( 3 -> 2 -> 1 -> 5).
So our conditions are satisfied.
Input : n = 8, d = 4, h = 2
Output : 1 2
2 3
1 4
4 5
1 6
1 7
1 8
Explanation :
- Observe that when d = 1, we cannot construct a tree (if tree has more than 2 vertices). Also when d > 2*h, we cannot construct a tree.
- As we know that height is the longest path from vertex 1 to another vertex. So build that path from vertex 1 by adding edges up to h. Now, if d > h, we should add another path to satisfy diameter from vertex 1, with a length of d – h.
- Our conditions for height and diameter are satisfied. But still some vertices may be left. Add the remaining vertices at any vertex other than the end points. This step will not alter our diameter and height. Choose vertex 1 to add the remaining vertices (you can choose any).
- But when d == h, choose vertex 2 to add the remaining vertices.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void constructTree( int n, int d, int h)
{
if (d == 1) {
if (n == 2 && h == 1) {
cout << "1 2" << endl;
return ;
}
cout << "-1" << endl;
return ;
}
if (d > 2 * h) {
cout << "-1" << endl;
return ;
}
for ( int i = 1; i <= h; i++)
cout << i << " " << i + 1 << endl;
if (d > h) {
cout << "1"
<< " " << h + 2 << endl;
for ( int i = h + 2; i <= d; i++) {
cout << i << " " << i + 1 << endl;
}
}
for ( int i = d + 1; i < n; i++)
{
int k = 1;
if (d == h)
k = 2;
cout << k << " " << i + 1 << endl;
}
}
int main()
{
int n = 5, d = 3, h = 2;
constructTree(n, d, h);
return 0;
}
|
Java
class GfG {
static void constructTree( int n, int d, int h)
{
if (d == 1 ) {
if (n == 2 && h == 1 ) {
System.out.println( "1 2" );
return ;
}
System.out.println( "-1" );
return ;
}
if (d > 2 * h) {
System.out.println( "-1" );
return ;
}
for ( int i = 1 ; i <= h; i++)
System.out.println(i + " " + (i + 1 ));
if (d > h) {
System.out.println( "1" + " " + (h + 2 ));
for ( int i = h + 2 ; i <= d; i++) {
System.out.println(i + " " + (i + 1 ));
}
}
for ( int i = d + 1 ; i < n; i++)
{
int k = 1 ;
if (d == h)
k = 2 ;
System.out.println(k + " " + (i + 1 ));
}
}
public static void main(String[] args)
{
int n = 5 , d = 3 , h = 2 ;
constructTree(n, d, h);
}
}
|
Python3
def constructTree(n, d, h):
if d = = 1 :
if n = = 2 and h = = 1 :
print ( "1 2" )
return 0
print ( "-1" )
return 0
if d > 2 * h:
print ( "-1" )
return 0
for i in range ( 1 , h + 1 ):
print (i, " " , i + 1 )
if d > h:
print ( 1 , " " , h + 2 )
for i in range (h + 2 , d + 1 ):
print (i, " " , i + 1 )
for i in range (d + 1 , n):
k = 1
if d = = h:
k = 2
print (k , " " , i + 1 )
n = 5
d = 3
h = 2
constructTree(n, d, h)
|
C#
using System;
class GfG
{
static void constructTree( int n, int d, int h)
{
if (d == 1)
{
if (n == 2 && h == 1)
{
Console.WriteLine( "1 2" );
return ;
}
Console.WriteLine( "-1" );
return ;
}
if (d > 2 * h)
{
Console.WriteLine( "-1" );
return ;
}
for ( int i = 1; i <= h; i++)
Console.WriteLine(i + " " + (i + 1));
if (d > h)
{
Console.WriteLine( "1" + " " + (h + 2));
for ( int i = h + 2; i <= d; i++)
{
Console.WriteLine(i + " " + (i + 1));
}
}
for ( int i = d + 1; i < n; i++)
{
int k = 1;
if (d == h)
k = 2;
Console.WriteLine(k + " " + (i + 1));
}
}
public static void Main(String[] args)
{
int n = 5, d = 3, h = 2;
constructTree(n, d, h);
}
}
|
Javascript
<script>
function constructTree(n, d, h)
{
if (d == 1)
{
if (n == 2 && h == 1)
{
document.write( "1 2" , "<br>" );
return ;
}
document.write( "-1" , "<br>" );
return ;
}
if (d > 2 * h)
{
document.write( "-1" , "<br>" );
return ;
}
for ( var i = 1; i <= h; i++)
document.write(i + " " + (i + 1), "<br>" );
if (d > h)
{
document.write( "1" + " " + (h + 2), "<br>" );
for ( var i = h + 2; i <= d; i++)
{
document.write(i + " " + (i + 1), "<br>" );
}
}
for ( var i = d + 1; i < n; i++)
{
var k = 1;
if (d == h)
k = 2;
document.write(k + " " + (i + 1), "<br>" );
}
}
var n = 5, d = 3, h = 2;
constructTree(n, d, h);
</script>
|
Time Complexity: O(n), n is the number of vertices of the given tree.
Auxiliary Space: O(1)
Last Updated :
28 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...