Tree.cs :  » Game » SokoSolve-Sokoban » SokoSolve » Common » Structures » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » SokoSolve Sokoban 
SokoSolve Sokoban » SokoSolve » Common » Structures » Tree.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace SokoSolve.Common.Structures{

  /// <summary>
  /// Encapsulate a Tree structure for parent / children
  /// </summary>
  /// <typeparam name="T">Node's data</typeparam>
    public class Tree<T> : ITree<T> 
  {
    private TreeNode<T> root;

    /// <summary>
    /// Default Constructor
    /// </summary>
    public Tree()
    {
      this.root = new TreeNode<T>(this);
      this.root.Tree = this;
    }


        /// <summary>
        /// Allow centralised controll of the children collections.
        /// </summary>
        /// <param name="notification"></param>
        /// <returns></returns>
        protected internal ManagedCollection<TreeNode<T>> ManagedCollectionFactory(ManagedCollectionNotification<TreeNode<T>> notification)
        {
            return new ManagedCollection<TreeNode<T>>(notification);
        }

    
    /// <summary>
    /// The top node (essentially this represents the entire tree)
    /// </summary>
    public TreeNode<T> Root
    {
      get { return root; }
    }

    /// <summary>
    /// A collection of leaf nodes (nodes without children)
    /// </summary>
    public ICollection<TreeNode<T>> Leaves
    {
      get
      {
        return Root.FindAll(delegate(TreeNode<T> item) { return item.IsLeaf; }, int.MaxValue);
      }
    }

        /// <summary>
        /// A collection of ALL nodes
        /// </summary>
        public List<T> Nodes
        {
            get
            {
                return Root.ToList();
            }
        }

    /// <summary>
    /// Convert a list of nodes to a list of data payload T
    /// </summary>
    /// <param name="source"></param>
    /// <returns></returns>
    public static List<T> Convert(ICollection<TreeNode<T>> source)
    {
      List<T> results = new List<T>();
      foreach(TreeNode<T> node in source)
      {
        results.Add(node.Data);
      }
      return results;
    }

        /// <summary>
        /// Move a node to another parent
        /// </summary>
        /// <param name="Node"></param>
        /// <param name="NewParent">null means make it the new root node</param>
        public void Move(TreeNode<T> Node, TreeNode<T> NewParent)
        {
            // Remove it from tree
            if (!Node.IsRoot)
            {
                Node.Parent.RemoveChild(Node);
            }
            else
            {
                throw new NotSupportedException("Cannot move root node to sub-node");
            }

            // Add it to new parent
            if (NewParent == null)
            {
                TreeNode<T> oldRoot = root;
                root = Node;
                Node.Add(oldRoot);
            }
            else
            {
                NewParent.Add(Node);    
            }
        }


    #region ITree<T> Members

    ITreeNode<T> ITree<T>.Root
    {
      get { return root; }
    }

    ICollection<ITreeNode<T>> ITree<T>.Leaves
    {
      get { return (ICollection<ITreeNode<T>>)Leaves; }
    }

    #endregion

        public override string ToString()
        {
            return string.Format("Tree Size:{0}", Leaves.Count);
        }

        /// <summary>
        /// Clear/Empty the tree
        /// </summary>
      public void Clear()
      {
          if (root != null)
          {
              
              root.ForEach(ClearNode, 0);
              root = null;
          }
      }

      private void ClearNode(TreeNode<T> obj)
      {
          obj.Data = default(T);
          obj.Clear();
      }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.