Path: redb/redb.Core/Utils/TreeExtensions.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Utils/TreeExtensions.cs
----
using System;
using System.Collections.Generic;
using System.Linq;
using redb.Core.Models.Contracts;
using redb.Core.Models.Entities;
namespace redb.Core.Utils
{
///
/// Extensions for working with tree structures.
///
public static class TreeExtensions
{
///
/// Breadth-First Search tree traversal for polymorphic trees.
///
/// Root node for traversal
/// Node sequence in BFS order
public static IEnumerable BreadthFirstTraversal(this ITreeRedbObject root)
{
var queue = new Queue();
queue.Enqueue(root);
while (queue.Count > 0)
{
var current = queue.Dequeue();
yield return current;
foreach (var child in current.Children)
queue.Enqueue(child);
}
}
///
/// Breadth-First Search tree traversal for typed trees.
///
/// Object properties type
/// Root node for traversal
/// Node sequence in BFS order
public static IEnumerable> BreadthFirstTraversal(this ITreeRedbObject root)
where TProps : class, new()
{
var queue = new Queue>();
queue.Enqueue(root);
while (queue.Count > 0)
{
var current = queue.Dequeue();
yield return current;
// Children is untyped, cast to typed version
foreach (var child in current.Children.OfType>())
queue.Enqueue(child);
}
}
///
/// Depth-First Search tree traversal - pre-order for polymorphic trees.
///
/// Root node for traversal
/// Node sequence in DFS pre-order
public static IEnumerable DepthFirstTraversal(this ITreeRedbObject root)
{
yield return root;
foreach (var child in root.Children)
{
foreach (var descendant in child.DepthFirstTraversal())
yield return descendant;
}
}
///
/// Depth-First Search tree traversal - pre-order for typed trees.
///
/// Object properties type
/// Root node for traversal
/// Node sequence in DFS pre-order
public static IEnumerable> DepthFirstTraversal(this ITreeRedbObject root)
where TProps : class, new()
{
yield return root;
// Children is untyped, cast to typed version
foreach (var child in root.Children.OfType>())
{
foreach (var descendant in child.DepthFirstTraversal())
yield return descendant;
}
}
///
/// Depth-First Search tree traversal - post-order for polymorphic trees.
///
/// Root node for traversal
/// Node sequence in DFS post-order
public static IEnumerable PostOrderTraversal(this ITreeRedbObject root)
{
foreach (var child in root.Children)
{
foreach (var descendant in child.PostOrderTraversal())
yield return descendant;
}
yield return root;
}
///
/// Depth-First Search tree traversal - post-order for typed trees.
///
/// Object properties type
/// Root node for traversal
/// Node sequence in DFS post-order
public static IEnumerable> PostOrderTraversal(this ITreeRedbObject root)
where TProps : class, new()
{
// Children is untyped, cast to typed version
foreach (var child in root.Children.OfType>())
{
foreach (var descendant in child.PostOrderTraversal())
yield return descendant;
}
yield return root;
}
///
/// Find node by ID in polymorphic tree.
///
/// Root node for search
/// ID of target node
/// Found node or null
public static ITreeRedbObject? FindById(this ITreeRedbObject root, long id)
{
return root.DepthFirstTraversal().FirstOrDefault(node => node.Id == id);
}
///
/// Find node by ID in typed tree.
///
/// Object properties type
/// Root node for search
/// ID of target node
/// Found node or null
public static ITreeRedbObject? FindById(this ITreeRedbObject root, long id)
where TProps : class, new()
{
return root.DepthFirstTraversal().FirstOrDefault(node => node.Id == id);
}
///
/// Find nodes by predicate in polymorphic tree.
///
/// Root node for search
/// Search predicate
/// Collection of found nodes
public static IEnumerable FindNodes(this ITreeRedbObject root,
Func predicate)
{
return root.DepthFirstTraversal().Where(predicate);
}
///
/// Find nodes by predicate in typed tree.
///
/// Object properties type
/// Root node for search
/// Search predicate
/// Collection of found nodes
public static IEnumerable> FindNodes(this ITreeRedbObject root,
Func, bool> predicate)
where TProps : class, new()
{
return root.DepthFirstTraversal().Where(predicate);
}
///
/// Gets all leaf nodes of polymorphic tree.
///
/// Root node
/// Collection of leaf nodes
public static IEnumerable GetLeaves(this ITreeRedbObject root)
{
return root.DepthFirstTraversal().Where(node => node.IsLeaf);
}
///
/// Gets all leaf nodes of typed tree.
///
/// Object properties type
/// Root node
/// Collection of leaf nodes
public static IEnumerable> GetLeaves(this ITreeRedbObject root)
where TProps : class, new()
{
return root.DepthFirstTraversal().Where(node => node.IsLeaf);
}
///
/// Gets all nodes at specific level in polymorphic tree.
///
/// Root node
/// Level (0 for root)
/// Collection of nodes at specified level
public static IEnumerable GetNodesAtLevel(this ITreeRedbObject root, int level)
{
return root.DepthFirstTraversal().Where(node => node.Level == level);
}
///
/// Gets all nodes at specific level in typed tree.
///
/// Object properties type
/// Root node
/// Level (0 for root)
/// Collection of nodes at specified level
public static IEnumerable> GetNodesAtLevel(this ITreeRedbObject root, int level)
where TProps : class, new()
{
return root.DepthFirstTraversal().Where(node => node.Level == level);
}
///
/// Builds materialized path for polymorphic node.
///
/// Node
/// Path separator
/// Materialized path like "/1/5/23"
public static string GetMaterializedPath(this ITreeRedbObject node, string separator = "/")
{
var pathIds = node.GetPathIds();
return separator + string.Join(separator, pathIds);
}
///
/// Builds materialized path for typed node.
///
/// Object properties type
/// Node
/// Path separator
/// Materialized path like "/1/5/23"
public static string GetMaterializedPath(this ITreeRedbObject node, string separator = "/")
where TProps : class, new()
{
var pathIds = node.GetPathIds();
return separator + string.Join(separator, pathIds);
}
///
/// Flattens polymorphic tree to list with level indicators.
///
/// Root node
/// List of (node, level) pairs
public static IEnumerable<(ITreeRedbObject Node, int Level)> FlattenWithLevels(this ITreeRedbObject root)
{
return root.DepthFirstTraversal().Select(node => (node, node.Level));
}
///
/// Flattens typed tree to list with level indicators.
///
/// Object properties type
/// Root node
/// List of (node, level) pairs
public static IEnumerable<(ITreeRedbObject Node, int Level)> FlattenWithLevels(this ITreeRedbObject root)
where TProps : class, new()
{
return root.DepthFirstTraversal().Select(node => (node, node.Level));
}
///
/// Checks if polymorphic tree is balanced (subtree depth difference does not exceed 1).
///
/// Root node
/// true if tree is balanced
public static bool IsBalanced(this ITreeRedbObject root)
{
return CheckBalance(root) != -1;
}
///
/// Checks if typed tree is balanced (subtree depth difference does not exceed 1).
///
/// Object properties type
/// Root node
/// true if tree is balanced
public static bool IsBalanced(this ITreeRedbObject root)
where TProps : class, new()
{
return CheckBalance(root) != -1;
}
private static int CheckBalance(ITreeRedbObject node)
{
if (!node.Children.Any())
return 0;
var childHeights = new List();
foreach (var child in node.Children)
{
var height = CheckBalance(child);
if (height == -1) return -1; // Not balanced
childHeights.Add(height);
}
var maxHeight = childHeights.Max();
var minHeight = childHeights.Min();
return Math.Abs(maxHeight - minHeight) <= 1 ? maxHeight + 1 : -1;
}
private static int CheckBalance(ITreeRedbObject node)
where TProps : class, new()
{
if (!node.Children.Any())
return 0;
var childHeights = new List();
foreach (var child in node.Children)
{
var height = CheckBalance(child);
if (height == -1) return -1; // Not balanced
childHeights.Add(height);
}
var maxHeight = childHeights.Max();
var minHeight = childHeights.Min();
return Math.Abs(maxHeight - minHeight) <= 1 ? maxHeight + 1 : -1;
}
}
}