Logo

Programming-Idioms

History of Idiom 17 > diff from v28 to v29

Edit summary for version 29 by programming-idioms.org:
[Go] Slightly better demo

Version 28

2016-12-04, 21:17:10

Version 29

2016-12-04, 21:20:29

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