Algorithm for computing the depths of all the nodes of tree

Click For Summary
SUMMARY

The discussion focuses on developing an algorithm to compute the depths of all nodes in a binary tree T, where n represents the number of nodes. The proposed algorithm, named nodeHeight, calculates the height of a specific node N by traversing left and right child nodes recursively. However, participants highlight that this approach only computes the depth for one node at a time, which is inefficient for the overall task. A more optimal solution would involve a single traversal of the tree to compute the depths of all nodes simultaneously, achieving a time complexity of O(n).

PREREQUISITES
  • Understanding of binary tree structures
  • Familiarity with recursion in algorithms
  • Knowledge of Big-O notation for algorithm complexity
  • Experience with depth-first search (DFS) techniques
NEXT STEPS
  • Research efficient algorithms for tree traversal, specifically depth-first search (DFS)
  • Learn about breadth-first search (BFS) and its application in tree depth calculations
  • Study the implementation of depth calculation algorithms in programming languages such as Python or Java
  • Explore optimization techniques for recursive algorithms to improve performance
USEFUL FOR

Computer science students, software developers, and algorithm enthusiasts looking to enhance their understanding of tree data structures and depth computation techniques.

s3a
Messages
828
Reaction score
8

Homework Statement


Give an algorithm for computing the depths of all the nodes of a tree T, where n is the number of nodes of T.

a) What is the complexity of your algorithm in terms of Big-O?

b) What is the best possible complexity that you believe can be achieved when solving such
problem? Explain why.

c) If your algorithm does not achieve this best complexity, can you design a better algorithm to
achieve such complexity? If so, give this algorithm as a second solution.

Homework Equations


The height of a node is the number of edges connecting it to the root node.

The Attempt at a Solution


I'm currently at the part before part a.

Is the following a complete (and 100% correct) algorithm, or is something missing?:

Code:
Algorithm nodeHeight(T,N,H)
Input: A binary tree T and a node N and height H of the given node
Output: The height of the given node in the given binary tree

   if root node and node N are the same thing
       return H

   if there is a left node of N   
       nodeHeight(T,left node of N,H+1)

   if there is a right node of N
       nodeHeight(T,right node of N,H+1)

   return 0

It feels "light" to me. Would this be accepted as a solution on an exam?

Any input in helping me fully understand how to answer this question would be GREATLY appreciated!

P.S.
Am I supposed to assume that the tree is a binary tree?
 
Physics news on Phys.org
I'm afraid there are a couple of things I do not understand about your algorithm.
First, the task is to find the depth of all nodes, not just one. If your idea is to then run this algorithm for each node in the tree tvat seems very inefficient.
Secondly, it seems to be running the wrong way. If you want the depth of a node, don't you need to work towards the root, and that would involve running the parent line, not the two child lines, no?
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K