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

Idiom #17 Create a Tree data structure

The structure must be recursive. A node may have zero or more children. A node has access to children nodes, but not to its parent.

type Tree struct {
	Key keyType
	Deco valueType
	Children []*Tree
}
typedef struct node_s
{
    int value;
    struct node_s *nextSibling;
    struct node_s *firstChild;
} node_t;
#include <vector>
template<typename T>
struct Node{
  T value;
  std::vector<Node<T>> children;
};
using System.Collections.Generic
class Node<T>
{
 T value;
 List<Node<T>> childNodes;
}
struct Node(T){
    Node[] children;
    T data;
}

alias TreeOfIntegers = Node!(int);
class Node<T> {
  final T value;
  final List<Node<T>> children;
  Node(this.value, this.children);
}
  
data Tree a = Node {
    value :: a,
    children :: [Tree a]
}
import java.util.List;
import java.util.ArrayList;
class Tree<K,V> {
  K key;
  V deco;
  List<Tree<K,V>> children = new ArrayList<>();
}
type
generic
  TTree<_T> = class(TObject)
    Children: array of TObject;
    Data: _T;
  end;

type
  TStringTree = specialize TTree<String>;
type
  TTree = class
    Children: array of TTree;
    Data: TObject;
  end;     
class Node(object):
    def __init__(self, value, *children):
        self.value = value
        self.children = list(children)
Node = Struct.new(:children)
parent = Node.new([])
parent.children << Node.new([])
struct Node<T> {
  value: T,
  children: Vec<Node<T>>,
}
(define (make-tree value children)
  (cons value children))

(define (tree-value t) (car t))
(define (tree-first-child t) (cadr t))
(define (tree-rest-children t) (cddr t))

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