DEV Community

Jennifer Fadriquela
Jennifer Fadriquela

Posted on • Edited on

1

Generic Interface for Binary Tree

A node in a binary tree can only have left and right child nodes. This structure can go deeper. As an effort to strongly-type this kind of data, I came up with below implementation of generic interface and class.

public interface INode<T>
{
    T LeftNode { get; set; }
    T RightNode { get; set; }
    string Path { get; set; }
}

public class Node: INode<Node>
{
    public Node LeftNode { get; set; }
    public Node RightNode { get; set; }
    public string Path { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Node class seems a little quirky because it passes its own type to INode.

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay