Node

Let's start with the class representing a single node in a tree. Fortunately, you can use the implementation of the class already described for the binary tree (BinaryTreeNode) as a base. The modified code is as follows:

public class BinaryTreeNode<T> : TreeNode<T> { public BinaryTreeNode() => Children = new List<TreeNode<T>>() { null, null }; public BinaryTreeNode<T> Parent { get; set; } public BinaryTreeNode<T> Left { get { return (BinaryTreeNode<T>)Children[0]; } set { Children[0] = value; } } public BinaryTreeNode<T> Right { get { return (BinaryTreeNode<T>)Children[1]; } set { Children[1] = value; } } public int GetHeight() { int height = 1; BinaryTreeNode<T> current = this; while (current.Parent != null) { height++; current = current.Parent; ...

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.