April 2018
Intermediate to advanced
292 pages
6h 44m
English
A node in a binary tree is represented by an instance of BinaryTreeNode, which inherits from the TreeNode generic class with the following code:
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;
}
}
In the BinaryTreeNode class, it is necessary to declare two properties, Left and Right, which represent both possible children of a node. The relevant part of code is as follows:
public class BinaryTreeNode<T> : TreeNode<T> { public BinaryTreeNode() => Children = new List<TreeNode<T>>() { null, ...