Example #1
0
        /// <summary>
        /// Use a Depth-first search to find an instance of
        /// the given node type. Returns null if none are found.
        /// </summary>
        /// <typeparam name="Node">The type of node to search for</typeparam>
        public Node SearchTree <Node>(Models.Node node) where Node : Models.Node
        {
            var result = node.Children
                         .Select(n => (n.GetType() == typeof(Node)) ? n : SearchTree <Node>(n));

            return(result.OfType <Node>().FirstOrDefault());
        }
Example #2
0
        /// <summary>
        /// Iterates over the nodes ancestors until it finds
        /// the first instance of the given node type.
        /// </summary>
        /// <typeparam name="Node">The type of node to search for</typeparam>
        public Node GetAncestor <Node>() where Node : Models.Node
        {
            Models.Node ancestor = Parent;

            while (ancestor.Parent.GetType() != typeof(Node))
            {
                ancestor = ancestor.Parent;
            }

            return((Node)ancestor.Parent);
        }