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

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

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