O'Reilly logo

C# Data Structures and Algorithms by Marcin Jamro

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required