April 2018
Intermediate to advanced
292 pages
6h 44m
English
The first class is named TreeNode and is declared as the generic class to provide a developer with the ability to specify the type of data stored in each node. Thus, you can create the strongly-typed solution, which eliminates the necessity of casting objects to target types. The code is as follows:
public class TreeNode<T>
{
public T Data { get; set; }
public TreeNode<T> Parent { get; set; }
public List<TreeNode<T>> Children { get; set; }
public int GetHeight()
{
int height = 1;
TreeNode<T> current = this;
while (current.Parent != null)
{
height++;
current = current.Parent;
}
return height;
}
}
The class contains three properties: the data stored in the node (Data) of the type (T) specified while creating an instance of the class, ...