Skip to content

Cross platform Binary Tree C# implementation as NuGet package

License

Notifications You must be signed in to change notification settings

AbhiAgarwal192/BinaryTree

 
 

Repository files navigation

HttpPatch

BinaryTree

C# Binary tree

This little project contains a cross platform Binary Tree implementation

AppVeyor Travis build status GitHub release NuGet package NuGet License contributions welcome

How to Install

You can directly install this library from Nuget. There is package:

BinaryTree

PM> Install-Package BinaryTree

How to use

Create a new instanse: var binaryTree = new BinaryTree<int>(); and add elements:

binaryTree.Add(8);
binaryTree.Add(5);
binaryTree.Add(12)

or use collection initializer like : var binaryTree = new BinaryTree<int>() { 8, 5, 12, 3, 7, 10, 15 };

Another way is constructs a BinaryTree, copying the contents of the given collection

IList<int> numbers = new List<int>();
var binaryTree = new BinaryTree<int>(numbers);

Also you can initialize a new instance of the BinaryTree class that is empty and has the specified initial capacity: var binaryTree = new BinaryTree<int>(10);

By default traversal is set to In-order
You can set the type of traversal in constructor var binaryTree = new BinaryTree<int>(new PostOrderTraversal<int>()); or use property TraversalStrategy:

var inOrder = new InOrderTraversal<int>();
var preOrder = new PreOrderTraversal<int>();
var postOrder = new PostOrderTraversal<int>();

binaryTree.TraversalStrategy = preOrder;

Available operations:

  • void Add(T value) - adds a new element to the tree
  • void AddRange(IEnumerable<T> collection) - copying the contents of the given collection to the tree
  • ITraversalStrategy<T> TraversalStrategy -gets or sets type of traversal(Pre-order, In-order, Post-order)
  • int Count - returns count of elements in tree
  • int Capacity - gets the number of elements that the BinaryTree can contain
  • bool IsReadOnly - always return false
  • bool IsFixedSize - gets a value indicating whether the BinaryTree has a fixed size
  • bool Contains(T value) - checks if the tree contains the element
  • bool Remove(T value) - remove element from the tree. Returns true if element was removed.
  • void Clear() - clears tree
  • void CopyTo(T[] array, int arrayIndex) - copies all the elements of the tree to the specified one-dimensional array starting at the specified destination array index.
  • IEnumerator<T> GetEnumerator() - returns numerator of tree

To display all elements of tree, use:

foreach (var item in binaryTree)
{
   Console.Write(item + " ");
}

or use extension method:

binaryTree.PrintToConsole();

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

License

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

About

Cross platform Binary Tree C# implementation as NuGet package

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 75.2%
  • PowerShell 14.2%
  • Shell 9.6%
  • Batchfile 1.0%