/// <summary> /// Tries to get a node's parent. /// </summary> /// <typeparam name="T">The type of elements in the tree.</typeparam> /// <param name="walker"> /// The <see cref="ITreeWalker<T>"/> that knows how to find the parent and child /// nodes. /// </param> /// <param name="node"> /// The node whose parent is to be returned. /// </param> /// <param name="parent"> /// A reference to the resulting parent node. This will be a default value of type /// <typeparamref name="T"/> if we fail to get a the parent node. /// </param> /// <returns> /// True if a parent node is found, false, otherwise. /// </returns> public static bool TryGetParent <T>(this ITreeWalker <T> walker, T node, out T parent) { // Validate parameters. if (walker == null) { throw new ArgumentNullException("walker"); } if (node == null) { throw new ArgumentNullException("node"); } // Get the ancestors enumerator and attempt to move to the first node. // If we get the first node output it and return true. Otherwise, output a default // node and return false. using (IEnumerator <T> enumerator = walker.GetAncestors(node).GetEnumerator()) { if (enumerator.MoveNext()) { parent = enumerator.Current; return(true); } else { parent = default(T); return(false); } } }
/// <summary> /// Determines if a node has a parent. /// </summary> /// <typeparam name="T">The type of elements in the tree.</typeparam> /// <param name="walker"> /// The <see cref="ITreeWalker<T>"/> that knows how to find the parent and child nodes. /// </param> /// <param name="node"> /// The node being checked for a parent. /// </param> /// <returns> /// Returns a <see cref="System.Boolean"/> indicating whether or not the node has a parent. /// </returns> public static bool HasParent <T>(this ITreeWalker <T> walker, T node) { // Validate parameters. if (walker == null) { throw new ArgumentNullException("walker"); } if (node == null) { throw new ArgumentNullException("node"); } return(walker.GetAncestors(node).Any()); }
/// <summary> /// Returns a node's parent or a default node if no parent exists. /// </summary> /// <typeparam name="T">The type of elements in the tree.</typeparam> /// <param name="walker"> /// The <see cref="ITreeWalker<T>"/> that knows how to find the parent and child nodes. /// </param> /// <param name="node"> /// The node whose parent is to be returned. /// </param> /// <returns> /// Returns a node's parent or a default node if no parent exists. /// </returns> public static T GetParentOrDefault <T>(this ITreeWalker <T> walker, T node) { // Validate parameters. if (walker == null) { throw new ArgumentNullException("walker"); } if (node == null) { throw new ArgumentNullException("node"); } // Return the node's parent or a default node if the parent does not exist. return(walker.GetAncestors(node).FirstOrDefault()); }
/// <summary> /// Gets the depth of the node. The depth is measured by the number of edges between /// <paramref name="node"/> and the root of the tree. /// </summary> /// <typeparam name="T">The type of elements in the tree.</typeparam> /// <param name="walker"> /// The <see cref="ITreeWalker<T>"/> that knows how to find the parent and child nodes. /// </param> /// <param name="node"> /// The node whose depth is to be returned. /// </param> /// <returns> /// The number of edges between <paramref name="node"/> and the root of the tree. /// </returns> public static int GetDepth <T>(this ITreeWalker <T> walker, T node) { // Validate parameters. if (walker == null) { throw new ArgumentNullException("walker"); } if (node == null) { throw new ArgumentNullException("node"); } // Return the depth of 'node' by counting the nodes ancestors. return(walker.GetAncestors(node).Count()); }
/// <summary> /// Gets a node and the node's ancestors, starting with its parent node and ending with /// the root node. /// </summary> /// <typeparam name="T">The type of elements in the tree.</typeparam> /// <param name="walker"> /// The <see cref="ITreeWalker<T>"/> that knows how to find the parent and child /// nodes. /// </param> /// <param name="node"> /// The node whose ancestors are to be returned. /// </param> /// <returns> /// An <see cref="System.Collections.Generic.IEnumerable<T>"/> that contains the node /// and all of the node's ancestors, up to and including the root. /// </returns> public static IEnumerable <T> GetAncestorsAndSelf <T>(this ITreeWalker <T> walker, T node) { if (walker == null) { throw new ArgumentNullException("walker"); } if (node == null) { throw new ArgumentNullException("node"); } yield return(node); foreach (T item in walker.GetAncestors(node)) { yield return(item); } }