Code Monkey home page Code Monkey logo

treelib's Introduction

Build status Release NuGet

TreeLib

This project provides a:

  • .Net Standard Library (1.4, 1.6, 2.0) or a
  • .Net framework 4.0 Library

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:

  • a project reference,
  • a LINQ statement to find each set of children in the tree,
  • and a simple for each loop to implement the operation on each tree node:
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:

  • the traversal algorithm and
  • the operations performed on each tree node (e.g.: Console.WriteLine(item.GetPath());).

The project in this repository contains a demo console project to demo its usage in more detail.

Supported Generic Traversal Methods

Breadth First

Level Order

See TreeLib.BreadthFirst.Traverse.LevelOrder implementation for:

Depth First

PreOrder

See TreeLib.BreadthFirst.Traverse.PreOrder implementation for:

Postorder

See TreeLib.BreadthFirst.Traverse.Postorder implementation for:

Tip

treelib's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

treelib's Issues

Missing nodes on when calling Depthfirst.Traverse.PostOrder

Traverse PostOrder does not return all nodes of the tree when the first child of a node has even number of children.For example if you have a tree with A as root node and it has 3 children B,C and D and if the first one of the previous children (B) has even number of child nodes then you will not get back all the nodes.More specific it will return only A ,B node and the children of B node but not C and D with their childs.I believe that there is a need for an additional code in the lines below.

if (topEnumerator.MoveNext())
{
// if it has an element, use it
current = topEnumerator.Current;
//break;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.