[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2520
  • Last Modified:

Recursive Get Tree Size

I need a small recursive method able to return a given multigrade tree size (the number of items contained in it), every node has several children. This other method returns the tree height, I place it as an example so you can figure out the structure of the tree.
public int getHeight(int height, Element element, int maxHeight){
      if(height > maxHeight)
        maxHeight = height;
      int i=0;
      for(i = 0; i < element.getNumChildren(); i++)
        return getHeight(height+1,element.getChild(i),maxHeight);
    return maxHeight;

Open in new window

1 Solution
This counts all nodes (not only leaves):

public int getSize(Element element) {
  int i, size=1;
  for(i = 0; i < element.getNumChildren(); i++)
  return size;
axturAuthor Commented:
perfect solution

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now