Logo

Programming-Idioms

History of Idiom 17 > diff from v13 to v14

Edit summary for version 14 by :

Version 13

2015-09-13, 17:55:55

Version 14

2015-10-29, 14:05:12

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 his parent.

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 his parent.

Code
class Node<T> {
  final T value;
  final List<Node<T>> children;
  Node(this.value, this.children);
}
  
Code
class Node<T> {
  final T value;
  final List<Node<T>> children;
  Node(this.value, this.children);
}
  
Code
struct Node(T){
    Node[] children;
    T data;
}

alias TreeOfIntegers = Node!(int);
Code
struct Node(T){
    Node[] children;
    T data;
}

alias TreeOfIntegers = Node!(int);
Comments bubble
Inside template Node(T), Node is an alias for the instantiated type.
Comments bubble
Inside template Node(T), Node is an alias for the instantiated type.
Code
type Tree struct {
	Key keyType
	Deco valueType
	Children []*Tree
}
Code
type Tree struct {
	Key keyType
	Deco valueType
	Children []*Tree
}
Comments bubble
keyType should be easily comparable.
valueType is a type of value associated with current node, distinct from the key.
Children is a slice of pointers.


Note that in Go you can call methods of pointer type *Tree even on a nil receiver (an empty tree).
Comments bubble
keyType should be easily comparable.
valueType is a type of value associated with current node, distinct from the key.
Children is a slice of pointers.


Note that in Go you can call methods of pointer type *Tree even on a nil receiver (an empty tree).