BinaryTreeTNode Class |
Namespace: Accord.Collections
[SerializableAttribute] public class BinaryTree<TNode> : IEnumerable where TNode : BinaryNode<TNode>
The BinaryTreeTNode type exposes the following members.
Name | Description | |
---|---|---|
BinaryTreeTNode | Initializes a new instance of the BinaryTreeTNode class |
Name | Description | |
---|---|---|
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetEnumerator |
Returns an enumerator that iterates through the tree.
| |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
ToString | Returns a string that represents the current object. (Inherited from Object.) | |
Traverse |
Traverse the tree using a tree traversal
method. Can be iterated with a foreach loop.
|
Name | Description | |
---|---|---|
HasMethod |
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.) | |
IsEqual |
Compares two objects for equality, performing an elementwise
comparison if the elements are vectors or matrices.
(Defined by Matrix.) | |
SetEqualsTNode |
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.) | |
To(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.) | |
ToT | 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.) |
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"