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:
	def __init__(self, data):
		self.data = data
		self.left = None
		self.right = None
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
end
-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;
    }
}
type
  generic BinTree<T> = class
    Value: T;
    Left, Right: BinTree;
  end;   
Node = Struct.new(:left, :right)
parent = Node.new(Node.new, Node.new)
struct BinTree<T> {
    value: T,
    left: Option<Box<BinTree<T>>>,
    right: Option<Box<BinTree<T>>>,
}

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

Idiom created by

programming-idioms.org

Related idioms