Javatpoint Logo
Javatpoint Logo

Height of n-ary tree if parent array is given

Problem Statement:

We will be provided an array named parent with n number of elements that form a tree, according to the issue statement. A node in the tree is represented by each index i in the parent array, and the value at the ith index denotes the i index's immediate parent.

The height of this generic tree, which is provided as a parent array, must be determined. A tree in which a node might have N nodes is known as a generic or N-way tree. The distance between the longest root node to the leaf path of a tree is its height.

Input array[] = [-1 0 1 6 6 0 0 2 7]

Result: 5 in height

The tree that is formed is:

  1. Begin at every node and proceed to its parent until we reach -1.
  2. Additionally, note the highest point that may be reached between any two nodes.

Implementation:

Naïve approach:

Java Code:

Output:

Height of the given tree is: 4

Time complexity: O(n^2)

Space complexity: O(1)

Optimized Approach:

Dynamic programming is employed. The height between the root and every node is kept in an array. Thus, by simply adding 1, we can determine the height from the root to the node child if we know the height of the root to the node.

Java Code:

Output:

Height of the given tree is: 4

The Java program's goal is to use a given parent array to calculate the height of a generic N-ary tree. It achieves this by using a recursive process that finds the highest point among all nodes and methodically determines each node's height.

The maxHeight variable keeps track of the highest node height.

In this procedure, the calculateHeight function is essential. It uses parent-child relationships to compute a node's height recursively. The function returns a height of 1 if a node is the root node, which is indicated by a parent value of -1. The function obtains and returns the height of the current node if it has already been calculated and stored in the height array. The function moves up the tree through the parent array, increasing the height for each level to ascertain the height if it still needs to be determined.

The entire computation is handled by the findTreeHeight function. The computed heights for every node are stored in an array named height, which is initialized with all starting values set to -1. After that, the code iterates through each node, calculating each one's height by calling the calculateHeight function. The maxHeight variable keeps track of the highest height of every node.

The entire computation is handled by the findTreeHeight function. The computed heights for every node are stored in an array named height, which is initialized with all starting values set to -1. After that, the code iterates through each node, calculating each one's height by calling the calculateHeight function. The maxHeight variable keeps track of the highest height of every node.

Time Complexity: O(n)

Space Complexity: O(n)







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA