Click or drag to resize
Accord.NET (logo)

BinaryTreeTNode Class

Base class for binary trees. This class does not implement a binary search tree, but can used to implement one. For binary search trees, please refer to RedBlackTreeT, KDTree and VPTree.
Inheritance Hierarchy

Namespace:  Accord.Collections
Assembly:  Accord (in Accord.dll) Version: 3.8.0
Syntax
[SerializableAttribute]
public class BinaryTree<TNode> : IEnumerable
where TNode : BinaryNode<TNode>
Request Example View Source

Type Parameters

TNode
The class type for the nodes of the tree.

The BinaryTreeTNode type exposes the following members.

Constructors
  NameDescription
Public methodBinaryTreeTNode
Initializes a new instance of the BinaryTreeTNode class
Top
Properties
  NameDescription
Public propertyRoot
Gets the root node of this tree.
Top
Methods
  NameDescription
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetEnumerator
Returns an enumerator that iterates through the tree.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodTraverse
Traverse the tree using a tree traversal method. Can be iterated with a foreach loop.
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodSetEqualsTNode
Compares two enumerables for set equality. Two enumerables are set equal if they contain the same elements, but not necessarily in the same order.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Examples

The BinaryTreeTNode class is a base class for other tree classes such as RedBlackTreeT, KDTree and VPTree. For examples on how to use those classes, please see their respective documentation pages.

If you would like to implement your own binary tree that inherits from this class, then you can do so as shown in the following example. First, make sure your custom node class inherits from BinaryNodeTNode and passes itself as the generic argument of BinaryNodeTNode:

/// <summary>
///   Implement this class as you need. There are no fundamental properties
///   or methods that should be overriden by your class, except for any data
///   value that you might want your node to carry.
/// </summary>
/// 
class MyTreeNode : BinaryNode<MyTreeNode>
{
    /// <summary>
    ///   Gets or sets a custom value that you would like your nodes to have.
    /// </summary>
    /// 
    public string Value { get; set; }
}

Now, once the tree node has been implemented, we can create a new BinaryTreeTNode and explore the tree in different ways as shown below:

// Let's start by creating an empty tree 
var tree = new BinaryTree<MyTreeNode>();

// Now, we can proceed by placing elements on different positions of the tree. Note that this class 
// does not  implement a search  tree, so it is not possible to place elements automatically using 
// an ".Add()" method. Instead, this class offers functionality that is common to all Binary Trees,
// such as traversing the tree breadth-first, depth-first, in order, in pre-order, and in post-order.

// Let's start populating the tree:
tree.Root = new MyTreeNode()
{
    Left = new MyTreeNode()
    {
        Left = new MyTreeNode()
        {
            Value = "a"
        },

        Value = "b",

        Right = new MyTreeNode()
        {
            Value = "c"
        },
    },

    Value = "d",

    Right = new MyTreeNode()
    {
        Left = new MyTreeNode()
        {
            Value = "e"
        },

        Value = "f",

        Right = new MyTreeNode()
        {
            Value = "g"
        },
    }
};

// Now, let's traverse the tree in order:
List<string> breadthFirst = tree.Traverse(TreeTraversal.BreadthFirst).Select(x => x.Value).ToList();
// should return: "d", "b", "f", "a", "c", "e", "g"

List<string> depthFirst = tree.Traverse(TreeTraversal.DepthFirst).Select(x => x.Value).ToList();
// should return: "d", "f", "g", "e", "b", "c", "a"

List<string> inOrder = tree.Traverse(TreeTraversal.InOrder).Select(x => x.Value).ToList();
// should return: "a", "b", "c", "d", "e", "f", "g"

List<string> postOrder = tree.Traverse(TreeTraversal.PostOrder).Select(x => x.Value).ToList();
// should return: "a", "c", "b", "e", "g", "f", "d"

List<string> preOrder = tree.Traverse(TreeTraversal.PreOrder).Select(x => x.Value).ToList();
// should return: "d", "b", "a", "c", "f", "e", "g"
See Also