Click here to Skip to main content
15,897,891 members
Articles / Database Development / SQL Server

Hierarchical Tree Represented by Modified Preorder Tree Traversal Technique using C# 3.0 and SQL 2005

Rate me:
Please Sign up or sign in to vote.
4.93/5 (29 votes)
10 Dec 2008CPOL6 min read 113.7K   3.3K   86  
Gathering of various algorithms into one library to transform Hierarchical trees between various formats, and allows them to be represented into SQL 2005, the formats supported are TreeView, Textual, Tabular, Modified Preorder Tree Traversal and Graphical.
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using MPTT.DAL;
using MPTT.BLL;
using System.Data.SqlClient;

/// <summary>
/// Tree functionalities Add, Insert, Delete and Modify
/// Hirarchical tree representation into SQL table
/// Modified Preorder Tree Traversal (MPTT) schema to represent Hajj data into DB
/// </summary>
namespace MPTT.DAL
{
    public abstract class TreeProvider : DataAccess
    {
        public string[] Fields = {"ID","parentID","lft","rgt","leafID","isLeaf","Title"};

        public TreeProvider()
            : base()
        {
        }
        public TreeProvider(string conString) :
            base(conString)
        {
        }
        public abstract int AddNode(string title, int parentID,bool rebuiled);
        public abstract void DeleteNode(int id);
        public abstract List<MpttNode> GetLevelZeroNodes();
        public abstract List<MpttNode> GetChildren(int parentID);
        public abstract List<MpttNode> GetSubTree(int parentID, bool includeParent);
        public abstract MpttNode GetNodeByLeafID(int leafID);

        public abstract List<MpttNode> GetSiblings(int nodeID);
        public abstract int GetParentID(int nodeID);
        public abstract MpttNode GetNode(int nodeID);
        
        public abstract void TruncateTable();
        public abstract int RebuildTree(int parentID, int lft);
        public abstract void IdentifyAllLeafIDs(int startLeafID);
        public abstract void IdentifyAllNodes(int startID);
        public abstract void IdentifySingleLeafID(int ID, int leafID); 
        public abstract int DescendantsCount(int parentID);
        
        

        /// <summary>
        /// Returns a new MpttTreeNode instance filled with the DataReader's current record data
        /// </summary>
        protected virtual MpttNode GetNodeFromReader(IDataReader reader)
        {
            return new MpttNode(
               (int)reader[Fields[0]],
               (int)reader[Fields[1]],
               (int)reader[Fields[2]],
               (int)reader[Fields[3]],
               (int)reader[Fields[4]],
               ((bool)reader[Fields[5]]) ? 1 : 0,
               reader[Fields[6]].ToString());
        }

        /// <summary>
        /// Returns a collection of MpttTreeNode objects with the data read from the input DataReader
        /// </summary>
        protected virtual List<MpttNode> GetNodeCollectionFromReader(IDataReader reader)
        {
            List<MpttNode> nodes = new List<MpttNode>();
            while (reader.Read())
                nodes.Add(GetNodeFromReader(reader));
            reader.Close();
            return nodes;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Architect Government
Qatar Qatar
Programmer since 1990 with Pascal, VC++, C#, ASP.NET, jQuery, J2EE and Android.
PMP Certified since 2009.
PSP Certified since 2005.
Business & System analyst since 2004.
Led teams in between 8 to 30 members.
Worked for www.beinsports.net, www.harf.com, www.islamweb.net, islam.gov.qa, islamonline.net.

Comments and Discussions