Hierarchy 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Hierarchy --version 1.1.0
NuGet\Install-Package Hierarchy -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hierarchy" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hierarchy --version 1.1.0
#r "nuget: Hierarchy, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Hierarchy as a Cake Addin
#addin nuget:?package=Hierarchy&version=1.1.0

// Install Hierarchy as a Cake Tool
#tool nuget:?package=Hierarchy&version=1.1.0

Hierarchy

A .Net Standard library that can be used for hierarchical data

Nuget

Getting Started

  1. Install the package from NuGet
  2. Transform any flat hierarchical list into a hierarchical tree structure by using the ToHierarchy extension method
  3. Use other extension methods to perform operations on the new Hierarchy list.

Hierarchy

A hierarchy is a structure in which each node can only ever have a single parent node (or no parent in the case of a root node)

Hierarchy Example

using Hierarchy;

public class Person
{
    public int Id { get; set; }
    public int ParentId { get; set; }
    public string Name { get; set; } = "";

    public override string ToString()
    {
        return $"Person: {Id} '{Name}'";
    }
}

List<Person> flatList = new()
{
    new() { Id = 1, ParentId = 0, Name = "CEO" },
    new() { Id = 2, ParentId = 1, Name = "North America Operaror" },
    new() { Id = 3, ParentId = 1, Name = "South America Operator" },
    new() { Id = 12, ParentId = 3, Name = "Brazil Operator" },
    new() { Id = 10, ParentId = 1, Name = "Europe Operator" },
    new() { Id = 11, ParentId = 1, Name = "Africa Operator" },
    new() { Id = 4, ParentId = 0, Name = "CFO" },
    new() { Id = 5, ParentId = 4, Name = "Financial Manager" },
    new() { Id = 6, ParentId = 4, Name = "Financial Designer" },
    new() { Id = 7, ParentId = 4, Name = "Financial Senior Team Lead" },
    new() { Id = 14, ParentId = 7, Name = "Financial Project 2: Lead" },
    new() { Id = 13, ParentId = 7, Name = "Financial Project 1: Lead" },
    new() { Id = 15, ParentId = 13, Name = "Financial Project 1: Member 1" },
    new() { Id = 16, ParentId = 13, Name = "Financial Project 1: Member 2" },
    new() { Id = 17, ParentId = 13, Name = "Financial Project 1: Member 3" },
    new() { Id = 18, ParentId = 13, Name = "Financial Project 1: Member 4" },
    new() { Id = 19, ParentId = 14, Name = "Financial Project 2: Member 1" },
    new() { Id = 20, ParentId = 14, Name = "Financial Project 2: Member 2" },
    new() { Id = 21, ParentId = 14, Name = "Financial Project 2: Member 3" },
    new() { Id = 22, ParentId = 14, Name = "Financial Project 2: Member 4" },
    new() { Id = 23, ParentId = 22, Name = "Financial Project 2: Grunt 1" },
    new() { Id = 24, ParentId = 22, Name = "Financial Project 2: Grunt 2" },
    new() { Id = 25, ParentId = 22, Name = "Financial Project 2: Grunt 3" },
    new() { Id = 26, ParentId = 22, Name = "Financial Project 2: Grunt 4" },
    new() { Id = 27, ParentId = 26, Name = "Financial Project 2: Leaf 1" },
    new() { Id = 30, ParentId = 26, Name = "Financial Project 2: Leaf 4" },
    new() { Id = 28, ParentId = 26, Name = "Financial Project 2: Leaf 2" },
    new() { Id = 29, ParentId = 26, Name = "Financial Project 2: Leaf 3" },
    new() { Id = 8, ParentId = 4, Name = "Marketing Manager" },
    new() { Id = 9, ParentId = 0, Name = "COO" },
};

// NOTE: You can order the incoming list before building the hierarchy to make sure that your hierarchy is ordered
//       the way you want it to be before presenting and traversing it
var hierarchyList = flatList.OrderBy(f => f.Id).ToHierarchy(t => t.Id, t => t.ParentId);
Console.WriteLine("We convert the flat list to a hierarchy");
Console.WriteLine(hierarchyList.PrintTree());

// NOTE: When you want to search through the entire tree, you must start with the **AllNodes()** extension method 
//       this will make sure you aren't performing linq operations just on the nodes at the top level
var node = hierarchyList.AllNodes().First(n => n.Data.Id == 14);
Console.WriteLine("We search through all nodes in the hierarchy for the one with this id");
Console.WriteLine(node.Data);
Console.WriteLine();

var siblingNodes = node.SiblingNodes();
Console.WriteLine("We get all other nodes that are at the same level as this node");
Console.WriteLine(siblingNodes.PrintNodes());

var childNodesBreadthFirst = node.DescendantNodes(TraversalType.BreadthFirst);
Console.WriteLine("We get all descendant nodes of this node (Breadth First)");
Console.WriteLine(childNodesBreadthFirst.PrintNodes());

var childNodesDepthFirst = node.DescendantNodes(TraversalType.DepthFirst);
Console.WriteLine("We get all descendant nodes of this node (Depth First)");
Console.WriteLine(childNodesDepthFirst.PrintNodes());

var parentNodes = node.AncestorNodes();
Console.WriteLine("We get all ancestor nodes of this node");
Console.WriteLine(parentNodes.PrintNodes());

var leafNodesBreadthFirst = node.LeafNodes(TraversalType.BreadthFirst);
Console.WriteLine("We get all leaf nodes (descendant nodes that do not have childen) of this node (Breadth First)");
Console.WriteLine(leafNodesBreadthFirst.PrintNodes());

var leafNodesDepthFirst = node.LeafNodes(TraversalType.DepthFirst);
Console.WriteLine("We get all leaf nodes (descendant nodes that do not have childen) of this node (Depth First)");
Console.WriteLine(leafNodesDepthFirst.PrintNodes());

var rootNode = node.RootNode();
Console.WriteLine("We get the top level node of this branch (the highest level ancestor)");
Console.WriteLine(rootNode.Data);

Console.ReadLine();

The output of the code above will look like this

We convert the flat list to a hierarchy
Person: 1 'CEO'
├─ Person: 2 'North America Operaror'
├─ Person: 3 'South America Operator'
│   └─ Person: 12 'Brazil Operator'
├─ Person: 10 'Europe Operator'
└─ Person: 11 'Africa Operator'
Person: 4 'CFO'
├─ Person: 5 'Financial Manager'
├─ Person: 6 'Financial Designer'
├─ Person: 7 'Financial Senior Team Lead'
│   ├─ Person: 14 'Financial Project 2: Lead'
│   │   ├─ Person: 19 'Financial Project 2: Member 1'
│   │   ├─ Person: 20 'Financial Project 2: Member 2'
│   │   ├─ Person: 21 'Financial Project 2: Member 3'
│   │   └─ Person: 22 'Financial Project 2: Member 4'
│   │       ├─ Person: 23 'Financial Project 2: Grunt 1'
│   │       ├─ Person: 24 'Financial Project 2: Grunt 2'
│   │       ├─ Person: 25 'Financial Project 2: Grunt 3'
│   │       └─ Person: 26 'Financial Project 2: Grunt 4'
│   │           ├─ Person: 27 'Financial Project 2: Leaf 1'
│   │           ├─ Person: 28 'Financial Project 2: Leaf 2'
│   │           ├─ Person: 29 'Financial Project 2: Leaf 3'
│   │           └─ Person: 30 'Financial Project 2: Leaf 4'
│   └─ Person: 13 'Financial Project 1: Lead'
│       ├─ Person: 15 'Financial Project 1: Member 1'
│       ├─ Person: 16 'Financial Project 1: Member 2'
│       ├─ Person: 17 'Financial Project 1: Member 3'
│       └─ Person: 18 'Financial Project 1: Member 4'
└─ Person: 8 'Marketing Manager'
Person: 9 'COO'

We search through all nodes in the hierarchy for the one with this id
Person: 14 'Financial Project 2: Lead'

We get all other nodes that are at the same level as this node
Person: 13 'Financial Project 1: Lead'

We get all descendant nodes of this node (Breadth First)
Person: 19 'Financial Project 2: Member 1'
Person: 20 'Financial Project 2: Member 2'
Person: 21 'Financial Project 2: Member 3'
Person: 22 'Financial Project 2: Member 4'
Person: 23 'Financial Project 2: Grunt 1'
Person: 24 'Financial Project 2: Grunt 2'
Person: 25 'Financial Project 2: Grunt 3'
Person: 26 'Financial Project 2: Grunt 4'
Person: 27 'Financial Project 2: Leaf 1'
Person: 28 'Financial Project 2: Leaf 2'
Person: 29 'Financial Project 2: Leaf 3'
Person: 30 'Financial Project 2: Leaf 4'

We get all descendant nodes of this node (Depth First)
Person: 22 'Financial Project 2: Member 4'
Person: 26 'Financial Project 2: Grunt 4'
Person: 30 'Financial Project 2: Leaf 4'
Person: 29 'Financial Project 2: Leaf 3'
Person: 28 'Financial Project 2: Leaf 2'
Person: 27 'Financial Project 2: Leaf 1'
Person: 25 'Financial Project 2: Grunt 3'
Person: 24 'Financial Project 2: Grunt 2'
Person: 23 'Financial Project 2: Grunt 1'
Person: 21 'Financial Project 2: Member 3'
Person: 20 'Financial Project 2: Member 2'
Person: 19 'Financial Project 2: Member 1'

We get all ancestor nodes of this node
Person: 7 'Financial Senior Team Lead'
Person: 4 'CFO'

We get all leaf nodes (descendant nodes that do not have childen) of this node (Breadth First)
Person: 19 'Financial Project 2: Member 1'
Person: 20 'Financial Project 2: Member 2'
Person: 21 'Financial Project 2: Member 3'
Person: 23 'Financial Project 2: Grunt 1'
Person: 24 'Financial Project 2: Grunt 2'
Person: 25 'Financial Project 2: Grunt 3'
Person: 27 'Financial Project 2: Leaf 1'
Person: 28 'Financial Project 2: Leaf 2'
Person: 29 'Financial Project 2: Leaf 3'
Person: 30 'Financial Project 2: Leaf 4'

We get all leaf nodes (descendant nodes that do not have childen) of this node (Depth First)
Person: 30 'Financial Project 2: Leaf 4'
Person: 29 'Financial Project 2: Leaf 3'
Person: 28 'Financial Project 2: Leaf 2'
Person: 27 'Financial Project 2: Leaf 1'
Person: 25 'Financial Project 2: Grunt 3'
Person: 24 'Financial Project 2: Grunt 2'
Person: 23 'Financial Project 2: Grunt 1'
Person: 21 'Financial Project 2: Member 3'
Person: 20 'Financial Project 2: Member 2'
Person: 19 'Financial Project 2: Member 1'

We get the top level node of this branch (the highest level ancestor)
Person: 4 'CFO'

Graphs

A graph structure can have multiple parents and children, because of this there are a different set of traversal methods that are used.

Graph Example

using Hierarchy;

public class PersonRelationships
{
    public int Id { get; set; }
    public int[] KnownRelationshipIds { get; set; } = new int[0];
    public string Name { get; set; } = "";

    public override string ToString()
    {
        return $"Person: {Id} '{Name}'";
    }
}

List<PersonRelationships> flatList = new()
{
    new() { Id = 1, KnownRelationshipIds = new int[] { 2, 3, 4 }, Name = "Bob" },
    new() { Id = 2, KnownRelationshipIds = new int[] { 1, 3, 4 }, Name = "Mary" },
    new() { Id = 3, KnownRelationshipIds = new int[] { 1, 2, 4 }, Name = "Jane" },
    new() { Id = 4, KnownRelationshipIds = new int[] { 1, 2, 3 }, Name = "Lion" },
    new() { Id = 5, KnownRelationshipIds = new int[] { 6 }, Name = "Gorilla" },
    new() { Id = 6, KnownRelationshipIds = new int[] { 0 }, Name = "Monkey" },
    new() { Id = 7, KnownRelationshipIds = new int[] { 1, 2, 3, 4, 5 }, Name = "Noodle" },
    new() { Id = 8, KnownRelationshipIds = new int[] { 1, 2, 3 }, Name = "Cake" },
};

var hierarchyList = flatList.OrderBy(f => f.Id).ToGraph(t => t.Id, t => t.KnownRelationshipIds);
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.1 967 4/17/2022
1.1.0 377 4/16/2022
1.0.2 364 4/15/2022
1.0.1 374 4/15/2022
1.0.0-alpha 117 4/14/2022

initial release