Menu

In the slide 11 of Lecture 8, there is a Java code that calculates the depth of a node recursively. Please help me with Java code that calculates the depth of a node iteratively (i.e., without using…

Question

Answered step-by-step

In the slide 11 of Lecture 8, there is a Java code that calculates…

 In the slide 11 of Lecture 8, there is a Java code that calculates the depth of a node recursively. Please help me with Java code that calculates the depth of a node iteratively (i.e., without using recursion). The signature should be: public int depth (Position<E> p)

 

The slide 11 of lecture 8 is shown in the image below

Image transcription text

General Trees Depth and Height . Depth – If p is the root, the depth of p is 0. – Otherwise, the depth of p is one
plus the depth of its parent. 1 public int depth(Position<E> p) throws IllegalArgumentException 2 if
(isRoot(p)) 3 return 0; 4 else 5 return 1 + depth(parent(p)); Running time = O(dp + 1) do is the depth of p
Show more

 

OR

Leave a Reply

Your email address will not be published. Required fields are marked *