Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Generics » Downloads
 
Add your own
alternative version

Simple Generic Tree

, 6 Sep 2012
Simple Generic Tree in c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NodeTree
{
    //------------------------------------------------------------------------------
    /// <summary>
    /// Generic Tree Node base class
    /// </summary>
    /// <typeparam name="T"></typeparam>
    //------------------------------------------------------------------------------
    public abstract class TreeNodeBase<T> : ITreeNode<T> where T : class, ITreeNode<T>
    {

        //------------------------------------------------------------------------------
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        //------------------------------------------------------------------------------
        protected TreeNodeBase(string name)
        {
            Name = name;
            ChildNodes = new List<T>();
        }

        //------------------------------------------------------------------------------
        /// <summary>
        /// Name
        /// </summary>
        //------------------------------------------------------------------------------
        public string Name
        {
            get;
            private set;
        }

        //------------------------------------------------------------------------------
        /// <summary>
        /// Parent
        /// </summary>
        //------------------------------------------------------------------------------
        public T Parent
        {
            get;
            set;
        }

        //------------------------------------------------------------------------------
        /// <summary>
        /// Children
        /// </summary>
        //------------------------------------------------------------------------------
        public List<T> ChildNodes
        {
            get;
            private set;
        }

        //------------------------------------------------------------------------------
        /// <summary>
        /// this
        /// </summary>
        //------------------------------------------------------------------------------
        protected abstract T MySelf
        {
            get;
        }

        //------------------------------------------------------------------------------
        /// <summary>
        /// True if a Leaf Node
        /// </summary>
        //------------------------------------------------------------------------------
        public bool IsLeaf
        {
            get { return ChildNodes.Count == 0; }
        }

        //------------------------------------------------------------------------------
        /// <summary>
        /// True if the Root of the Tree
        /// </summary>
        //------------------------------------------------------------------------------
        public bool IsRoot
        {
            get { return Parent == null; }
        }

        //------------------------------------------------------------------------------
        /// <summary>
        /// List of Leaf Nodes
        /// </summary>
        //------------------------------------------------------------------------------
        public List<T> GetLeafNodes()
        {
            return ChildNodes.Where(x => x.IsLeaf).ToList();
        }

        //------------------------------------------------------------------------------
        /// <summary>
        /// List of Non Leaf Nodes
        /// </summary>
        //------------------------------------------------------------------------------
        public List<T> GetNonLeafNodes()
        {
            return ChildNodes.Where(x => !x.IsLeaf).ToList();
        }

        //------------------------------------------------------------------------------
        /// <summary>
        /// Get the Root Node of the Tree
        /// </summary>
        //------------------------------------------------------------------------------
        public T GetRootNode()
        {
            if (Parent == null)
                return MySelf;

            return Parent.GetRootNode();
        }

        //------------------------------------------------------------------------------
        /// <summary>
        /// Dot separated name from the Root to this Tree Node
        /// </summary>
        //------------------------------------------------------------------------------
        public string GetFullyQualifiedName()
        {
            if (Parent == null)
                return Name;

            return string.Format("{0}.{1}", Parent.GetFullyQualifiedName(), Name);
        }

        //------------------------------------------------------------------------------
        /// <summary>
        /// Add a Child Tree Node
        /// </summary>
        /// <param name="child"></param>
        //------------------------------------------------------------------------------
        public void AddChild(T child)
        {
            child.Parent = MySelf;
            ChildNodes.Add(child);
        }

        //------------------------------------------------------------------------------
        /// <summary>
        /// Add a collection of child Tree Nodes
        /// </summary>
        /// <param name="children"></param>
        //------------------------------------------------------------------------------
        public void AddChildren(IEnumerable<T> children)
        {
            foreach (T child in children)
                AddChild(child);
        }

    }
}

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)

Share

About the Author

alc_aardvark

United Kingdom United Kingdom
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 6 Sep 2012
Article Copyright 2012 by alc_aardvark
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid