예제 #1
0
 /// <summary>
 /// Checks if the given iteration over this tree contains the provided tree.
 /// </summary>
 /// <param name="tree">The potentially contained tree.</param>
 /// <param name="iter">The iteration applied.</param>
 /// <returns></returns>
 public bool Has(OrderedTree <T> tree, Func <OrderedTree <T>, IEnumerable <OrderedTree <T> > > iter = null)
 {
     if (iter == null)
     {
         iter = TreeIter.BreadthFirst;
     }
     return(iter(this).Contains(tree));
 }
예제 #2
0
 /// <summary>
 /// Removes the child element.
 /// </summary>
 /// <param name="child">The child element to be removed.</param>
 /// <returns>True if the child parameter was in fact a child of this node, false otherwise.</returns>
 public bool RemoveChild(OrderedTree <T> child)
 {
     if (children.Contains(child))
     {
         children.Remove(child); child.Parent = null; return(true);
     }
     return(false);
 }
예제 #3
0
        /// <summary>
        /// Depth-first iteration of an ordered tree.
        /// </summary>
        /// <typeparam name="T">The value type for the tree.</typeparam>
        /// <param name="tree">The ordered tree to be iterated over.</param>
        /// <returns>An IEnumerable to iterate over the tree, depth-first.</returns>
        public static IEnumerable <T> DepthFirstByValue <T>(OrderedTree <T> tree) where T : IEquatable <T>
        {
            yield return(tree.Value);

            foreach (OrderedTree <T> child in tree.children)
            {
                foreach (OrderedTree <T> ch in DepthFirst(child))
                {
                    yield return(ch.Value);
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Breadth-first iteration of an ordered tree by value.
        /// </summary>
        /// <typeparam name="T">The value type for the tree.</typeparam>
        /// <param name="tree">The ordered tree to be iterated over.</param>
        /// <returns>An IEnumerable to iterate over the values of the  tree, breadth-first.</returns>
        public static IEnumerable <T> BreadthFirstByValue <T>(OrderedTree <T> tree) where T : IEquatable <T>
        {
            List <OrderedTree <T> > trees = new List <OrderedTree <T> > {
                tree
            }; List <T> values = new List <T>();

            foreach (var child in trees)
            {
                values.Add(child.Value); trees.AddRange(child.children);
            }

            return(values);
        }
예제 #5
0
        /// <summary>
        /// Breadth-first iteration of an ordered tree.
        /// </summary>
        /// <typeparam name="T">The value type for the tree.</typeparam>
        /// <param name="tree">The ordered tree to be iterated over.</param>
        /// <returns>An IEnumerable to iterate over the tree, breadth-first.</returns>
        public static IEnumerable <OrderedTree <T> > BreadthFirst <T>(OrderedTree <T> tree) where T : IEquatable <T>
        {
            List <OrderedTree <T> > trees = new List <OrderedTree <T> > {
                tree
            };

            foreach (var child in trees)
            {
                trees.AddRange(child.children);
            }

            return(trees);
        }
예제 #6
0
 /// <summary>
 /// Checks if the child parameter is a child of this tree node.
 /// </summary>
 /// <param name="child">The potential child node.</param>
 /// <returns></returns>
 public bool HasChild(OrderedTree <T> child) => children.Contains(child);
예제 #7
0
 private OrderedTree(T val, OrderedTree <T> parent)
 {
     Value = val; Parent = parent; parent.children.Add(this);
 }
예제 #8
0
 /// <summary>
 /// Creates a new tree with this instance at its root. Each instance is a node of the tree.
 /// </summary>
 /// <param name="val">The value of the node associated with the tree.</param>
 public OrderedTree(T val)
 {
     Value = val; Parent = null; children = new List <OrderedTree <T> >();
 }