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.

Node =, :right)
parent =,
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;
class Node<T> {
  final T value;
  Node<T> left, right;
  Node(this.value, {this.left, this.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 = Ø | BT (BT x) x (BT x)
class BinTree<T extends Comparable<T>>{
   T value;
   BinTree<T> left;
   BinTree<T> right;
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
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