2013-08-01

C# Tree

This is the construction of the Tree class presented at this URL:
http://dvanderboom.wordpress.com/2008/03/15/treet-implementing-a-non-binary-tree-in-c/ 
 
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace UserDefined.Structures
{
    // basic multinode tree template
 
    public enum TreeTraversalType
    {
        TopDown,
        BottomUp
    }// of enum TreeTraversalType
 
    public class TreeBasic001 <T> : TreeBasic001Node <T>
    {
        public TreeBasic001() {}// Default constructor
 
        public TreeBasic001(T rootNode)
        {
            Value = rootNode;
        }// of constructor TreeBasic001(T)
 
        // since this is derived from TreeBasic001Node, it shares the copy constructor of that class
    }// of class TreeBasic
 
    public class TreeBasic001NodeList<T> : List<TreeBasic001Node<T>>
    {
        public TreeBasic001Node<T> Parent;
 
        public TreeBasic001NodeList(TreeBasic001Node<T> Parent)
        {
            this.Parent = Parent;
        }// of constructor TreeBasic001NodeList(TreeBasic001Node<T>)
 
        public TreeBasic001NodeList(TreeBasic001Node<T> newParent, TreeBasic001NodeList<T> otherList)
        {
            this.Parent = newParent;
            foreach (TreeBasic001Node<T> otherChild in otherList)
            {
                this.Add(new TreeBasic001Node<T>(otherChild));
            }
        }// of copy constructor TreeBasic001NodeList(TreeBasic001NodeList<T>)
 
        public new TreeBasic001Node<T> Add(TreeBasic001Node<T> Node)
        {
            base.Add(Node);
            Node.Parent = Parent;
            return Node;
        }// of method Add(TreeBasic001Node<T>)
 
        public TreeBasic001Node<T> Add(T Value)
        {
            return Add(new TreeBasic001Node<T>(Value));
        }// of method Add(T)
 
        public override string ToString()
        {
            return "Count=" + Count.ToString();
        }// of method ToString()
    }// of class TreeBasic001NodeList<T>
 
    public class TreeBasic001Node<T> : IDisposable
    {
 
        private TreeBasic001Node<T> _Parent;
        public TreeBasic001Node<T> Parent
        {
            get { return _Parent; }
            set
            {
                if (value == _Parent)
                {
                    return;
                }
                
                if (_Parent != null)
                {
                    _Parent.Children.Remove(this);
                }
 
                if (value != null && !value.Children.Contains(this))
                {
                    value.Children.Add(this);
                }
 
                _Parent = value;
            }
        }// of Property TreeBasic001Node<T> Parent
 
        public TreeBasic001Node<T> Root
        {
            get
            {
                //return (Parent == null) ? this : Parent.Root;
                TreeBasic001Node<T> node = this;
 
                while (node.Parent != null)
                {
                    node = node.Parent;
                }
    
                return node;
            }
        }// of Property TreeBasic001Node<T> Root
          
        private TreeBasic001NodeList<T> _Children;
 
        public TreeBasic001NodeList<T> Children
        {
            get { return _Children; }
            private set { _Children = value; }
        }// of Property TreeBasic001NodeList<T> Children
 
        private T _Value;
 
        public T Value
        {
            get { return _Value; }
            set
            {
                _Value = value;
                /* more advanced implementation
                if (_Value != null && _Value is ITreeNodeAware<T>)
                {
                    (_Value as ITreeNodeAware<T>).Node = this;
                }*/
            }
        }// of Property Value
 
        public int Depth
        {
            get
            {
                //return (Parent == null ? -1 : Parent.Depth) + 1;
                int depth = 0;
                TreeBasic001Node<T> node = this;
                while (node.Parent != null)
                {
                    node = node.Parent;
                    depth++;
                }
 
                return depth;
            }
        }// of Property Depth
 
        public TreeBasic001Node()
        {
            this.Value = default(T);
            Parent = null;
            Children = new TreeBasic001NodeList<T>(this);
        }// of constructor TreeBasic001Node()
 
        public TreeBasic001Node(T Value)
        {
            this.Value = Value;
            Parent = null;
            Children = new TreeBasic001NodeList<T>(this);
        }// of constructor TreeBasic001Node(T)
 
        public TreeBasic001Node(T Value, TreeBasic001Node<T> Parent)
        {
            this.Value = Value;
            this.Parent = Parent;
            Children = new TreeBasic001NodeList<T>(this);
        }// of constructor TreeBasic001Node(T, TreeBasic001Node<T>)
 
        public TreeBasic001Node(TreeBasic001Node<T> otherNode)
        {
            this.Value = otherNode.Value;
            this.Parent = otherNode.Parent;
            this.Children = new TreeBasic001NodeList<T>(this, otherNode.Children);
        }// of copy constructor TreeBasic001Node(TreeBasic001Node<T>)
 
        public override string ToString()
        {
            string Description = string.Empty;
            if (Value != null)
            {
                Description = "[" + Value.ToString() + "] ";
            }
 
            return Description + "Depth=" + Depth.ToString() + ", Children="
              + Children.Count.ToString();
        }// of method ToString()
 
        private TreeTraversalType _DisposeTraversal = TreeTraversalType.BottomUp;
        public TreeTraversalType DisposeTraversal
        {
            get { return _DisposeTraversal; }
            set { _DisposeTraversal = value; }
        }// of Property DisposeTraversal
 
        private bool _IsDisposed;
 
        public bool IsDisposed
        {
            get { return _IsDisposed; }
        }// of Property IsDisposed
 
        public void Dispose()
        {
            CheckDisposed();
            OnDisposing();
 
            // clean up contained objects (in Value property)
            if (Value is IDisposable)
            {
                if (DisposeTraversal == TreeTraversalType.BottomUp)
                {
                    foreach (TreeBasic001Node<T> node in Children)
                    {
                        node.Dispose();
                    }
                }
 
                (Value as IDisposable).Dispose();
 
                if (DisposeTraversal == TreeTraversalType.TopDown)
                {
                    foreach (TreeBasic001Node<T> node in Children)
                    {
                        node.Dispose();
                    }
                }
            }// of if T implements IDisposable
 
            _IsDisposed = true;
        }// of method Dispose()
 
        public event EventHandler Disposing;
 
        protected void OnDisposing()
        {
            if (Disposing != null)
            {
                Disposing(thisEventArgs.Empty);
            }
        }// of event signal OnDisposing()
 
        public void CheckDisposed()
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }
        }// of method CheckDisposed()
 
    }// of class TreeBasic001Node<T>
 
 
}// of namespace UserDefined.Structures
 

No comments:

Post a Comment