Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C#
Article

Tree structure generator

Rate me:
Please Sign up or sign in to vote.
3.67/5 (5 votes)
3 Jun 2008CPOL3 min read 50.3K   1.1K   19   2
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#):

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)


Written By
Architect
Canada Canada
Ashutosh is an avid programmer, who “lives to code”. One can always find him busy seeking challenging programming assignments on various complex problems ranging from Data management, Classification, Optimization, Security, Network analysis, Distributed computing.

He started his programming stint with “C”; he has also worked on C++, Visual Basic, JAVA, Perl, FoxPro, PASCAL, Shell Scripting, and Perl. Currently, he is proficient and working on C#.

His area of interest includes Distributed Computing, Analytic and Business Intelligence, Large Enterprise System Architectures, Enterprise Content Management.

He is an advocate of Open source and likes 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

Blog :
Share a solution | Explore the .NET world

Link'd :


His Favorite(s) :
nHibernate - The best ORM.
nHibernate Contributed Tools

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 373887911-Oct-14 10:51
Member 373887911-Oct-14 10:51 
QuestionPure Genius Pin
Areal Person19-May-14 19:35
Areal Person19-May-14 19:35 

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

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