Logo

Programming-Idioms

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

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 its children nodes, but not to its parent.

import java.util.List;
import java.util.ArrayList;
class Tree<K,V> {
  K key;
  V deco;
  List<Tree<K,V>> children = new ArrayList<>();
}

Then you will want to add constructors, getters, setters.
typedef struct node_s
{
    int value;
    struct node_s *nextSibling;
    struct node_s *firstChild;
} node_t;

New implementation...