Logo

Programming-Idioms

History of Idiom 17 > diff from v21 to v22

Edit summary for version 22 by programming-idioms.org:
[Go] +Demo

Version 21

2016-05-02, 12:05:01

Version 22

2016-05-02, 12:12:38

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.

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.

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.
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).
Demo URL
https://play.golang.org/p/ZUKAs8z-FX