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. Chose vertex 1 to add the remaining vertices (you can chose any).
- But when d == h, choose vertex 2 to add the remaining vertices.
CPP
// CPP program to construct tree for given count // width and height. #include <bits/stdc++.h> using namespace std; // Function to construct the tree void constructTree(int n, int d, int h) { if (d == 1) { // Special case when d == 2, only one edge if (n == 2 && h == 1) { cout << "1 2" << endl; return; } cout << "-1" << endl; // Tree is not possible return; } if (d > 2 * h) { cout << "-1" << endl; return; } // Satisfy the height condition by add // edges up to h for (int i = 1; i <= h; i++) cout << i << " " << i + 1 << endl; if (d > h) { // Add d - h edges from 1 to // satisfy diameter condition cout << "1" << " " << h + 2 << endl; for (int i = h + 2; i <= d; i++) { cout << i << " " << i + 1 << endl; } } // Remaining edges at vertex 1 or 2(d == h) for (int i = d + 1; i < n; i++) { int k = 1; if (d == h) k = 2; cout << k << " " << i + 1 << endl; } } // Driver Code int main() { int n = 5, d = 3, h = 2; constructTree(n, d, h); return 0; } |
chevron_right
filter_none
Java
// Java program to construct tree for given count // width and height. class GfG { // Function to construct the tree static void constructTree(int n, int d, int h) { if (d == 1) { // Special case when d == 2, only one edge if (n == 2 && h == 1) { System.out.println("1 2"); return; } System.out.println("-1"); // Tree is not possible return; } if (d > 2 * h) { System.out.println("-1"); return; } // Satisfy the height condition by add // edges up to h for (int i = 1; i <= h; i++) System.out.println(i + " " + (i + 1)); if (d > h) { // Add d - h edges from 1 to // satisfy diameter condition System.out.println("1" + " " + (h + 2)); for (int i = h + 2; i <= d; i++) { System.out.println(i + " " + (i + 1)); } } // Remaining edges at vertex 1 or 2(d == h) for (int i = d + 1; i < n; i++) { int k = 1; if (d == h) k = 2; System.out.println(k + " " + (i + 1)); } } // Driver Code public static void main(String[] args) { int n = 5, d = 3, h = 2; constructTree(n, d, h); } } |
chevron_right
filter_none
Python3
# Python3 code to construct tree for given count # width and height. # Function to construct the tree def constructTree(n, d, h): if d == 1: # Special case when d == 2, only one edge if n == 2 and h == 1: print("1 2") return 0 print("-1") # Tree is not possible return 0 if d > 2 * h: print("-1") return 0 # Satisfy the height condition by add # edges up to h for i in range(1, h+1): print(i," " , i + 1) if d > h: # Add d - h edges from 1 to # satisfy diameter condition print(1," ", h + 2) for i in range(h+2, d+1): print(i, " " , i + 1) # Remaining edges at vertex 1 or 2(d == h) for i in range(d+1, n): k = 1 if d == h: k = 2 print(k ," " , i + 1) # Driver Code n = 5d = 3h = 2constructTree(n, d, h) # This code is contributed by "Sharad_Bhardwaj". |
chevron_right
filter_none
C#
// C# program to construct tree for // given count width and height. using System; class GfG { // Function to construct the tree static void constructTree(int n, int d, int h) { if (d == 1) { // Special case when d == 2, // only one edge if (n == 2 && h == 1) { Console.WriteLine("1 2"); return; } // Tree is not possible Console.WriteLine("-1"); return; } if (d > 2 * h) { Console.WriteLine("-1"); return; } // Satisfy the height condition // by add edges up to h for (int i = 1; i <= h; i++) Console.WriteLine(i + " " + (i + 1)); if (d > h) { // Add d - h edges from 1 to // satisfy diameter condition Console.WriteLine("1" + " " + (h + 2)); for (int i = h + 2; i <= d; i++) { Console.WriteLine(i + " " + (i + 1)); } } // Remaining edges at vertex 1 or 2(d == h) for (int i = d + 1; i < n; i++) { int k = 1; if (d == h) k = 2; Console.WriteLine(k + " " + (i + 1)); } } // Driver Code public static void Main(String[] args) { int n = 5, d = 3, h = 2; constructTree(n, d, h); } } // This code is contributed by 29AjayKumar |
chevron_right
filter_none
Output :
1 2 2 3 1 4 1 5
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Make a tree with n vertices , d diameter and at most vertex degree k
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Diameter of a Binary Tree
- Diameter of an N-ary tree
- Diameter of a tree using DFS
- Diameter of a Binary Tree in O(n) [A new method]
- Diameter of n-ary tree using BFS
- DP on Trees | Set-3 ( Diameter of N-ary Tree )
- Diameter of a Binary Indexed Tree with N nodes
- Finding the lexicographically smallest diameter in a binary tree
- Right sibling of each node in a tree given as array of edges
- Height of n-ary tree if parent array is given
- Check if a path exists in a tree with K vertices present or are at most at a distance D
- Minimum Operations to make value of all vertices of the tree Zero
- Maximize the sum of products of the degrees between any two vertices of the tree
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree
- Minimum cost to colour a Tree with no 3 adjacent vertices having same colour
- Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal
- Number of edges in mirror image of Complete binary tree
- Number of edges in a perfect binary tree with N levels
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

