This project provides a:
with Generic methods to traverse k-ary trees in different orders (Post-Order, Pre-Order, Level-Order) of traversal. This implementation includes scalable algorithms that return IEnumerable<T>
to make parsing large tree structures a piece of cake, as well, as Generic Exception handling to ensure that traversal algorithms complete despite unexpected errors on some nodes.
Review demo projects:
Implementing something as complicated as a Post-Order traversal algorithm requires just:
Console.WriteLine("(Depth First) PostOrder Tree Traversal V3");
items = TreeLib.Depthfirst.Traverse.PostOrder(root, i => i.Children);
foreach (var item in items)
{
Console.WriteLine(item.GetPath());
}
This pattern leads to a clear-cut separation of:
Console.WriteLine(item.GetPath());
).The project in this repository contains a demo console project to demo its usage in more detail.
See TreeLib.BreadthFirst.Traverse.LevelOrder implementation for:
See TreeLib.BreadthFirst.Traverse.PreOrder implementation for:
See TreeLib.BreadthFirst.Traverse.Postorder implementation for:
Read about Generic Tree and Linked List Traversal in C# to understand the usefulness of Generic traversal methods.
Watch the Binary tree traversal: Preorder, Inorder, Postorder video to better understand what is what (and why these Traversal Order Names make some sense):
Look into data structure books online Introduction to Trees, Binary Search Trees or offline Algorithms by Robert Sedgewick and Kevin Wayne, if you still need more background on tree structures