Applying Recursion To Build Tree Structures

In my last post, the goal was to show how parent/child relationships could be derived from Entity Framework(EF) using recursion, however the code below does not do anything meaningful other than apply recursion for looping though a parent/child relationship for a books table of contents. it cannot be used alone for building a tree structure. Why?

private void DisplayTOCData(TableOfContent TOC)
            if (TOC.TableOfContents.Count > 0)
                foreach (var child in TOC.TableOfContents.OrderByDescending(o => o.ContentOrder))
                    Console.WriteLine(". {0} is the root of {1}",TOC.Title,child.Title);

 The reason why is probably the most important factor for understanding recursion and how it needs to work. The code above simply looks to see if there are any table of contents, orders the table of contents and then checks to see if there are any other table of contents underneath a parent table of contents so they can be dispayed. When thinking about how to build a tree, it is important to think about the characteristics of a tree structure, applied to characteristics of the data, used to shape th tree. For instance,

  1. A tree will have a starting root, therefore the parent/child relationship of data must have a starting root.
  2. An element within the tree may just be a parent element and maynot have content, therfore there will be table of content headers that may or may not have content associated to a page in a book.
  3. Siblings under a parent might have an order (so they are not randomly loaded), therefore each table of content element may have an order so they can be ordered accordingly to other table of contents under the same topic.
  4. There is a very high chance that the data loaded into the tree will not be ordered the way the tree anticipates, therefore comparisons must be made to the next possible element to see if it is another sibling or belongs elsewhere.

The code below will model from the previous post, and use the TableOfContent object referenced earlier.

 private void BuildTOCDetails(TableOfContent toc)
    if(toc.TableOfContents.Count > 0)
       if (!toc.ParentContentId.HasValue)
       {//this is the root

       foreach (var child in toc.TableOfContents.OrderBy(o => o.ContentOrder))
          if (!this.CurrentParentId.Equals(toc.ContentId)
          && child.ContentOrder.Equals(1))
          {//starts the grouping of child topics by checking the first elements order

          if (child.TableOfContents.Count.Equals(0)
          && child.ContentOrder.Equals(toc.TableOfContents.Count)) //last sibling with no children
          {//close the grouping of siblings
             if (child.TableOfContents.Count > 0) //check if the child has children
                 //get the children through recursion


Within each of the conditions code, you can add your own code for building your tree, using either 3rd party controls or HTML markup. Each condition illustrated in code takes into consideration the different concepts that should be applied for building the tree. 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: BayerWhite
Posted on: 2/27/2014 at 2:03 AM
Actions: E-mail | Kick it! | DZone it! |
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed
Comments are closed