Click here to Skip to main content
Click here to Skip to main content

Tree structure generator

, 3 Jun 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A small utility program that can help in visualizing the existing directory structure in terms of graphs.

Introduction

This article introduces a small utility program that can help in visualizing the existing directory structure in terms of graphs. This utility is being developed to create a tree like structure based on the directory structure provided as input.

Background

If you've ever designed a build setup for any existing project, you've encountered the daunting task of visualizing the project structure. Sometimes, you may have created a sketch to represent the same. During my experience with build terminologies, I researched for a simple tool to generate a visual representation of the existing project layout. Luckily, I found some utilities, but most of them were commercial utilities. Or I can say, I was not able to find such a free utility. This was the inception of this small utility. I had an idea of recursively processing the directory tree, but the problem was the rendering in a graphical manner. After toilsome efforts, I found a free toolkit called GraphWiz. This toolkit was composed of many utilities to render many kinds of graphs. Here, I was only interested in a directed graph creation utility. GraphWiz has such a utility, namely Dot. Dot requires a simple input file that is written using a Dot specific syntax (described later), and renders the output in various formats including GIF, JPG, and PNG.

The Dot Syntax

Dot uses a very simple syntax to create graph like structures. The basic tokens of the syntax are Node and Connector Edges. I would like to provide a very simple example for the Dot syntax. Suppose we want to display the following directory structure in a graph:

tree.gif

The Dot syntax for the above structure is as shown here:

digraph G {
    Root [shape=box];

    DirA [shape=box];
    DirB [shape=box];
    DirC [shape=box];

    Root -> DirA;
    Root -> DirB;

    DirA-> DirC;
    DirA-> File1;

    DirB-> File2;
    DirB-> File3;

    DirC-> File4;
    DirC-> File5;
}

Using the following command, the output can be rendered in GIF format:

dot -Tgif tree.dt -o tree.gif

The algorithm for generating graphs

The basic algorithm I used here for generating the graphs for the specified tree structure is:

  1. Start from the root directory provided by the input.
  2. For every directory, create a node of inverted triangle shapes.
  3. For every directory being traversed, process each side by creating the node for every file and linking those nodes to their respective parents (the file nodes are of elliptical shape).
  4. For every directory being traversed, start the traversal of the subdirectory by specifying it as the root and making a recursive call to itself.
  5. When every file and directory has been traversed, finish the writing of the Dot syntax and call the Dot utility to generate the output file.

The implementation of the above algorithm (in C#):

void ProcessDirectory(DirectoryInfo dInfo,string ParentNode)
{
    //Processing each subdirectory
    foreach (DirectoryInfo tempDir in dInfo.GetDirectories())
    {
        nodeCtr++;
        dotWriter.WriteLine("node{0} [shape=box,label=\"{1}\"]; ", 
                            nodeCtr,tempDir.Name);
        dotWriter.WriteLine("{0} -> node{1};", ParentNode, nodeCtr);

        //Recursive call: Increment depth and call recursively
        depth++;
        ProcessDirectory(tempDir,"node" + nodeCtr.ToString());
    }

    //Processing each files
    foreach(FileInfo fInfo in dInfo.GetFiles())
    {
        nodeCtr++;
        dotWriter.WriteLine("node{0} [shape=oval,label=\"{1}\"];", 
                            nodeCtr, fInfo.Name);
        dotWriter.WriteLine("{0} -> node{1};", ParentNode, nodeCtr);
    }

    depth--;
    if (depth == 0)
    {
        //Traversal has been completed, finish writing
        //the dot file and call dot to generate the output
    }
}

I hope the implementation and basic concepts have been explained in enough details. Now, I would like to do a walkthrough of the utility.

How to use the utility

After downloading and extracting the utility, you will find an executable TreeStructure.exe. Start the utility, and the initial screen will be as shown:

Clipboard01.jpg

Select the root for the directory structure for which the tree structure needs to be created.

Clipboard02.jpg

Clipboard03.jpg

Now, click Generate button. The utility will perform the traversal and create a Dot syntax file.

Clipboard05.jpg

When the traversal is complete, it will show a file save dialog box for specifying the output file and format. Specify the output file name and the format, e.g., GIF or PNG, and click Save to generate the tree structure.

Clipboard04.jpg

Once the tree structure is generated, the utility will get a prompt to preview the generated file.

Clipboard06.jpg

After successful completion (hope so), you will get an image file containing the graphical representation of the directory structure, starting from the directory you specified. A sample is shown here:

Clipboard07.jpg

Note: Please do not use it for very huge directory structures because it will create a very huge graphic that might not be visible clearly at 16x zoom; trust me I have tried this.

Where to find GraphWiz

The Graphviz software is freely available under an Open Source license. It is available at www.graphviz.org and at www.research.att.com. In addition to software, the latter site also provides documentation, sample layouts, and links to various sites describing libraries or packages incorporating the uses of Graphviz.

History

  • Initial revision: 04 June 2008.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ashutosh Phoujdar
Architect Infosys Technologies Ltd.
India India
An Software Architect having work experience of 8+ years in Architecture, Design and Development of Enterprise class application for Analytic s and Business Intelligence. Having hands-on experience primarily in Microsoft Technology Stack including(but not limited to) .NET, SQL Server, SharePoint Server, Biztalk, Withal know to program in Java, Perl and Shell. Currently working as Product Technical Architect in Infosys Technologies Ltd. Products R&D division. He has worked on development of best in class Supply Chain Optimization System and Spend Classification Software for optimizing indirect spend. An avid programmer by heart and passionate for solving any challenging and complex business problems. He is advocate of Open source and love to share solutions with open source communities like
1.Stack Overflow
2. nHibernate
.
 
Award's :
Prize winner in Competition "Best article of May 2008"
 
Articles :
Click to see my CodeProject Articles
 
Open Source Contributions:
Click to see my Open Source Project
 
Blog :
Share a solution | Explore the .NET world
 
Link'd :
LinkedIn Profile
 
His Favorite(s) :
nHibernate - The best ORM.
nHibernate Contributed Tools
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMember 373887911-Oct-14 11:51 
QuestionPure Genius PinmemberAreal Person19-May-14 20:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 4 Jun 2008
Article Copyright 2008 by Ashutosh Phoujdar
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid