This language bar is your friend. Select your favorite languages!

Idiom #9 Create a Binary Tree data structure

The structure must be recursive because left child and right child are binary trees too. A node has access to children nodes, but not to its parent.

class Node<T> {
  final T value;
  Node<T> left, right;
  Node(this.value, {this.left, this.right});
struct treenode{
  int value;
  struct treenode* left;
  struct treenode* right;
class BinaryTree<T>:IComparable<T>
 T value;
 BinaryTree<T> leftChild;
 BinaryTree<T> rightChild;
struct BinaryTree(T)
	T data;
	BinaryTree* left;
	BinaryTree* right;
defmodule BinaryTree do
	defstruct data: nil, left: nil, right: nil
-type binary_tree(T) ::
	#{ data := T
	 , left := binary_tree(T)
	 , right := binary_tree(T)
type BinTree struct {
	Key keyType
	Deco valueType
	Left *BinTree
	Right *BinTree
data BT x = BEnd | BNode (BT x) x (BT x)
class Node {
  constructor (data) { = data
    this.left = null
    this.right = null
class Node {
  constructor (data) { = data
    this.left = null
    this.right = null
class BinTree<T extends Comparable<T>>{
   T value;
   BinTree<T> left;
   BinTree<T> right;
local function BinTree(v, l, r)
	return {
		val = v,
		left = l,
		right = r
class BNode
    public $data;
    public $lNode;
    public $rNode;

    public function __construct($item) {
        $this->data = $item;
        $this->lNode = null;
        $this->rNode = null;
  generic BinTree<T> = class
    Value: T;
    Left, Right: BinTree;
class Node:
	def __init__(self, data): = data
		self.left = None
		self.right = None
Node =, :right)
parent =,
struct BinTree<T> {
    value: T,
    left: Option<Box<BinTree<T>>>,
    right: Option<Box<BinTree<T>>>,
(define (make-btree value left right)
  (cons value (cons left right)))

(define (btree-value bt) (car bt))
(define (btree-left bt) (cadr bt))
(define (btree-right bt) (cddr bt))

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

Related idioms