Tree

The next necessary class is named Tree, and it represents the whole tree. Its code is even simpler than that presented in the preceding section, and is as follows:

public class Tree<T> 
{ 
    public TreeNode<T> Root { get; set; } 
} 

The class contains only one property, Root. You can use this property to get access to the root node, and then you can use its Children property to obtain data of other nodes located in the tree.

It is worth noting that both TreeNode and Tree classes are generic and the same type is used in the case of these classes. For instance, if tree nodes should store string values, the string type should be used in the case of instances of Tree and TreeNode classes.

Get C# Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.