Giter Club home page Giter Club logo

easytree's Introduction

EasyTree

EasyTree is a project to create a base class for tree-like objects in C#. Inheriting from the Node class provides APIs for creating, managing, and navigating a tree.

Usage

Creating a Tree

The basis of the tree is an instance of the Node class in the EasyTree namespace. Every node in the tree can be used to create its own subtree, with corresponding methods and properties.

The Node constructor has two overloads:

new Node();
new Node(Node parent);

You can construct a tree by creating each node and passing in its parent as parameter. The class also features the following parent/child management methods:

// Adds a child node to an existing node
void AddChild(Node child) {...}

// Removes a child node
void RemoveChild(Node child) {...}

// Adds a parent to an existing node
void AddParent(Node parent) {...}

// Removes a parent and makes the node a root
void RemoveParent(Node parent) {...}

Properties

The Node class features the following convenient properties.

// The node's parent
Node Parent { get; }

// The node's root
Node Root { get; }

// The full path from the node back to the root
IReadOnlyList<Node> Path { get; }

// List of the node's children in chronological order (the order you added them)
IReadOnlyList<Node> Children { get; }

// True if the node is a leaf
bool IsLeaf { get; }

// True if the node is a root
bool IsRoot { get; }

Searching your Tree

You can iterate through the descendants of any node by using any of the following methods, which return IEnumerator.

// Iterate through the tree with root myNode using a depth-first pre-order search
foreach (Node element in myNode.GetPreOrderIterator())
{
    // Insert code
}

// Iterate through the tree with root myNode using a depth-first post-order search
foreach (Node element in myNode.GetPostOrderIterator())
{
    // Insert code
}

// Iterate through the tree with root myNode using a breadth-first level-order search
foreach (Node element in myNode.GetLevelOrderIterator())
{
    // Insert code
}

Other Methods

In addition to the methods above for creating and iterating through a tree, the Node class has a number of methods to make navigating the tree easier.

// Gets a collection of the descendants of the current node.
IReadOnlyCollection<Node> GetDescendants() {...}

// Gets a collection of the tree's leaves, with the current node as root.
IReadOnlyCollection<Node> GetLeaves() {...}

Usage

Creating a custom class

You can either use the Node class directly or inherit from it to create your own custom class of tree-like objects that makes use of the above APIs.

You can find a minimum working example of a custom class here:

using EasyTree;

namespace CustomNameSpace
{
    public class SampleClass : Node
    {
        // Insert custom properties here

        public SampleClass(...) : base()
        {
            // Insert custom constructor here
        }

        public SampleClass(..., SampleClass parent) : base(parent)
        {
            // Insert custom constructor overload here
        }
    }
}

Contributing

Feel free to contribute by creating an issue or submitting a PR.

Author

Kier von Konigslow

License

This project is licensed under the MIT License - see the LICENSE file for details

easytree's People

Contributors

kvonkoni avatar

Stargazers

Chad avatar Dave Stone avatar  avatar Azadeh Bagheri avatar Gustav Wengel avatar

Watchers

 avatar

easytree's Issues

Improve performance for large trees

Each time a parent is added to a node, several lists are updated.

To improve performance, make the following changes:

  • Maintain only the list of children for a given node
  • Replace the Descendants, Leaves, with methods to avoid maintaining lists
  • Initialize the children list with an estimated maximum (say 10,000 items)

Make list-like properties read-only or remove them

Currently, Children, leaves, descendants, etc are properties that are exposed through getters. However, it's possible to modify them inappropriately.
It might be worthwhile to put them behind IReadOnlyList interface.

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.