Table of Contents

Class TreeNodeExtensions

Namespace
LfrlAnvil.Collections.Extensions
Assembly
LfrlAnvil.Collections.dll

Contains ITreeNode<T> extension methods.

public static class TreeNodeExtensions
Inheritance
TreeNodeExtensions
Inherited Members

Methods

GetChildIndex<T>(ITreeNode<T>, ITreeNode<T>)

Finds a 0-based index of the provided node in the Children collection of the specified parent.

[Pure]
public static int GetChildIndex<T>(this ITreeNode<T> parent, ITreeNode<T> node)

Parameters

parent ITreeNode<T>

Source parent node.

node ITreeNode<T>

Node to check.

Returns

int

0-based index of the provided node if it exists, otherwise -1.

Type Parameters

T

Value type.

GetLevel<T>(ITreeNode<T>)

Calculates the depth of the provided node.

[Pure]
public static int GetLevel<T>(this ITreeNode<T> node)

Parameters

node ITreeNode<T>

Node to check.

Returns

int

Node's depth. 0 means that the node is a root node.

Type Parameters

T

Value type.

GetLevel<T>(ITreeNode<T>, ITreeNode<T>)

Calculates the depth of the provided node relative to the specified root.

[Pure]
public static int GetLevel<T>(this ITreeNode<T> node, ITreeNode<T> root)

Parameters

node ITreeNode<T>

Node to check.

root ITreeNode<T>

Explicit root node.

Returns

int

Node's depth relative to the root node. 0 means that the node is the root node. -1 means that the node is not a descendant of the root node.

Type Parameters

T

Value type.

GetRoot<T>(ITreeNode<T>)

Finds the root node.

[Pure]
public static ITreeNode<T> GetRoot<T>(this ITreeNode<T> node)

Parameters

node ITreeNode<T>

Source node.

Returns

ITreeNode<T>

Root node or the provided node if it is a root node.

Type Parameters

T

Value type.

IsAncestorOf<T>(ITreeNode<T>, ITreeNode<T>)

Checks whether or not the provided ancestor is an ancestor node of the specified node.

[Pure]
public static bool IsAncestorOf<T>(this ITreeNode<T> ancestor, ITreeNode<T> node)

Parameters

ancestor ITreeNode<T>

Ancestor node.

node ITreeNode<T>

Node to check.

Returns

bool

true when the provided ancestor is an ancestor node of node, otherwise false.

Type Parameters

T

Value type.

IsChildOf<T>(ITreeNode<T>, ITreeNode<T>)

Checks whether or not the provided node is a direct child node of the specified parent.

[Pure]
public static bool IsChildOf<T>(this ITreeNode<T> node, ITreeNode<T> parent)

Parameters

node ITreeNode<T>

Node to check.

parent ITreeNode<T>

Parent node.

Returns

bool

true when the provided node is a child node of parent, otherwise false.

Type Parameters

T

Value type.

IsDescendantOf<T>(ITreeNode<T>, ITreeNode<T>)

Checks whether or not the provided node is a descendant node of the specified ancestor.

[Pure]
public static bool IsDescendantOf<T>(this ITreeNode<T> node, ITreeNode<T> ancestor)

Parameters

node ITreeNode<T>

Node to check.

ancestor ITreeNode<T>

Ancestor node.

Returns

bool

true when the provided node is a descendant node of ancestor, otherwise false.

Type Parameters

T

Value type.

IsLeaf<T>(ITreeNode<T>)

Checks whether or not the provided node is a leaf node.

[Pure]
public static bool IsLeaf<T>(this ITreeNode<T> node)

Parameters

node ITreeNode<T>

Node to check.

Returns

bool

true when the node is a leaf node (does not have any children nodes), otherwise false.

Type Parameters

T

Value type.

IsParentOf<T>(ITreeNode<T>, ITreeNode<T>)

Checks whether or not the provided parent is a parent node of the specified node.

[Pure]
public static bool IsParentOf<T>(this ITreeNode<T> parent, ITreeNode<T> node)

Parameters

parent ITreeNode<T>

Parent node.

node ITreeNode<T>

Node to check.

Returns

bool

true when the provided parent is a parent node of node, otherwise false.

Type Parameters

T

Value type.

IsRoot<T>(ITreeNode<T>)

Checks whether or not the provided node is a root node.

[Pure]
public static bool IsRoot<T>(this ITreeNode<T> node)

Parameters

node ITreeNode<T>

Node to check.

Returns

bool

true when the node is a root node (does not have a parent node), otherwise false.

Type Parameters

T

Value type.

VisitAncestors<T>(ITreeNode<T>)

Creates a new IEnumerable<T> instance that contains all ancestors of the provided node.

[Pure]
public static IEnumerable<ITreeNode<T>> VisitAncestors<T>(this ITreeNode<T> node)

Parameters

node ITreeNode<T>

Source node.

Returns

IEnumerable<ITreeNode<T>>

New IEnumerable<T> instance.

Type Parameters

T

Value type.

VisitDescendants<T>(ITreeNode<T>)

Creates a new IEnumerable<T> instance that contains all descendants of the provided node.

[Pure]
public static IEnumerable<ITreeNode<T>> VisitDescendants<T>(this ITreeNode<T> node)

Parameters

node ITreeNode<T>

Source node.

Returns

IEnumerable<ITreeNode<T>>

New IEnumerable<T> instance.

Type Parameters

T

Value type.

VisitDescendants<T>(ITreeNode<T>, Func<ITreeNode<T>, bool>)

Creates a new IEnumerable<T> instance that contains all descendants of the provided node with a stopPredicate.

[Pure]
public static IEnumerable<ITreeNode<T>> VisitDescendants<T>(this ITreeNode<T> node, Func<ITreeNode<T>, bool> stopPredicate)

Parameters

node ITreeNode<T>

Source node.

stopPredicate Func<ITreeNode<T>, bool>

Predicate that stops the traversal for the given sub-tree, when it returns true.

Returns

IEnumerable<ITreeNode<T>>

New IEnumerable<T> instance.

Type Parameters

T

Value type.