Nem_HierarchyTree 2.0.0
See the version list below for details.
dotnet add package Nem_HierarchyTree --version 2.0.0
NuGet\Install-Package Nem_HierarchyTree -Version 2.0.0
<PackageReference Include="Nem_HierarchyTree" Version="2.0.0" />
<PackageVersion Include="Nem_HierarchyTree" Version="2.0.0" />
<PackageReference Include="Nem_HierarchyTree" />
paket add Nem_HierarchyTree --version 2.0.0
#r "nuget: Nem_HierarchyTree, 2.0.0"
#:package Nem_HierarchyTree@2.0.0
#addin nuget:?package=Nem_HierarchyTree&version=2.0.0
#tool nuget:?package=Nem_HierarchyTree&version=2.0.0
Nem_HierarchyTree
Nem_HierarchyTree is a C# library for representing and manipulating hierarchical tree structures using bitwise operations for efficient node management. It is designed for scenarios where unique node identification, fast containment checks, and compact representation are required.
Features
- Hierarchical tree structure with support for parent and child nodes
- Unique node identification using GUIDs and bit flags
- Efficient node addition, removal, and lookup
- Flat dictionary for fast access to any node
- Support for cleaning orphaned and false parent nodes
- Serialization support via custom JSON converter
- .NET 8 and C# 12 compatible
Known Limitations
- Niether the tree nor nodes are thread-safe.
- Performance degrades with very large trees. The default size limit is 2,000 nodes.
- Nodes must have unique contents (no duplicates allowed).
Getting Started
Add the NuGet package or reference the project in your solution. All types are in the Nem_HierarchyTree namespace.
Basic Usage
Creating a Tree and Adding Nodes
using Nem_HierarchyTree;
HierarchyTree<string> tree = new HierarchyTree<string>();
Node<string> parent = new Node<string>("Parent");
tree.Add(parent);
Node<string> child = new Node<string>("Child") { ParentId = parent.Id };
tree.Add(child);
Adding Nodes with TryAdd
Node<string> another = new Node<string>("Another");
Node<string> addedNode;
if (tree.TryAdd(another, out addedNode)) {
// addedNode is the node added to the tree
}
Removing Nodes
// Remove a node and all its children
tree.Remove(parent); // returns a list of removed nodes
// Or use TryRemove
List<Node<string>> removedNodes;
if (tree.TryRemove(child, out removedNodes)) {
// removedNodes contains the nodes that were removed
}
Enumerating Nodes (Pre-order Traversal)
foreach (Node<string> node in tree) {
Console.WriteLine($"Node: {node.Contents}, Id: {node.Id}");
}
Containment Checks
// By node instance
bool exists = tree.Contains(parent);
// By node Id
Guid id = parent.Id;
bool existsById = tree.Contains(id);
// By node contents (string)
bool existsByName = tree.Contains("Parent");
// Check if a node is a descendant of another
bool isDescendant = parent.Contains(child); // true
Accessing Nodes
// By Id
Node<string> foundById = tree[parent.Id];
// By contents
Node<string> foundByContents = tree["Child"];
Cleaning and Clearing the Tree
// Remove false parents and orphaned nodes
tree.CleanTree();
// Remove all nodes and reset the tree
tree.Clear();
Serialization and Deserialization
using System.Text.Json;
// Serialize
tree.Add(new Node<string>("Root"));
string json = HierarchyTree<string>.SerializeJson(tree);
// Deserialize
HierarchyTree<string> deserializedTree = HierarchyTree<string>.DeserializeJson(json);
Node<T> API Highlights
Id: Unique identifier (Guid)Contents: The value stored in the nodeParentId: Guid of the parent node (Guid.Empty for root)Children: Read-only list of child nodesChildCount: Number of childrenContains(Node<T>): Checks if this node contains another node using bitwise operations
HierarchyTree<T> API Highlights
Add(Node<T>): Add a node (throws on error)TryAdd(Node<T>, out Node<T>): Try to add a nodeRemove(Node<T>): Remove a node and its childrenTryRemove(Node<T>, out List<Node<T>>)Contains(Node<T> | Guid | string): Check for node existenceGetNode(Guid | T): Retrieve node by Id or contentsRoots: List of root nodesCleanTree(): Remove false parents and orphansClear(): Remove all nodesSerializeJson,DeserializeJson: JSON (de)serialization
License
This project is licensed under the MIT License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Nem_HierarchyTree:
| Package | Downloads |
|---|---|
|
Nem_LanguageInfo
A library for finding ISO 639, ISO 3166, ISO 15924, UN M.49, and IETF BCP 47 information |
GitHub repositories
This package is not used by any popular GitHub repositories.