title: Algorithm to find the max width of a tree, not using node structures
body: <p>I'm trying to figure out how to calculate the max width of a tree. Instead of using a typical leaf/node structure, I am basing it on data from a DB. I will find all the children of a particular "node" (Person) to determine the max width of a peer line:</p>
  
  <pre><code>     1
      /  \
     2    3
   / | \     \
  4  5  6     7 
            /  \
           8    9
  </code></pre>
  
  <p>So the max of that tree above is 4. Since I am not using a traditional left/right approach AND the number of children can be greater than 2, how would I do this? </p>
  
  <p>Couple things:</p>
  
  <ul>
  <li>This is NOT homework</li>
  <li>The code I have below is generate a max width of roughly 3200 (the max I calculated for the example I have handy is 22)</li>
  </ul>
  
  <p>Here is my code as of now:</p>
  
  <pre><code>private int calculateWidth(def org, int h) {
  
      def allContacts = Contact.findAllByOrganization(org)
      List&lt;String&gt; headNodes = findHighestNode(org.id, allContacts )
  
      Contact contact = Contact.get(Long.parseLong(headNodes.get(0)))
      Person parent = new Person(contact.id, contact.fullName)
  
      int maxWidth = 0;
      int width;
      int heightOfChart = h;
      int i;
  
      for(i = 1; i &lt;= heightOfChart; i++)
      {
        width = getWidth(parent, i);
  
        if(width &gt; maxWidth)
          maxWidth = width;
      }
  
      System.out.println("The max width is = " + maxWidth)
      return ((NODE_HEIGHT + NODE_OFFSET) * (maxWidth))
  }
  
  private int getWidth(Person parent, int level)
  {
  
    List&lt;Person&gt; allChildren = getChildren(parent)
  
    if(allChildren.size() == 0)
      return 0;
  
    if(level == 1)
      return 1;
  
    else if (level &gt; 1) {
      int count = 0
      for(Person child : allChildren) {
          count = count + getWidth(parent, level-1)
      }
      return count
    }
  }
  </code></pre>