Logo

Programming-Idioms

History of Idiom 9 > diff from v12 to v13

Edit summary for version 13 by :

Version 12

2015-09-13, 17:54:20

Version 13

2015-10-29, 14:05:11

Idiom #9 Create a Binary Tree data structure

The structure must be recursive because left child and right child are binary trees too. A node has access to children nodes, but not to its parent.

Idiom #9 Create a Binary Tree data structure

The structure must be recursive because left child and right child are binary trees too. A node has access to children nodes, but not to its parent.

Code
type BinTree struct {
	Key keyType
	Deco valueType
	Left *BinTree
	Right *BinTree
}
Code
type BinTree struct {
	Key keyType
	Deco valueType
	Left *BinTree
	Right *BinTree
}
Comments bubble
keyType should be easily comparable.
valueType is a type of value associated with current node, distinct from the key.

Note that in Go you can call methods of pointer type *BinTree 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.

Note that in Go you can call methods of pointer type *BinTree even on a nil receiver (an empty tree).
Demo URL
http://play.golang.org/p/MK3qoaVPol
Demo URL
http://play.golang.org/p/MK3qoaVPol
Code
class BinTree<T extends Comparable<T>>{
   T value;
   BinTree<T> left;
   BinTree<T> right;
}
Code
class BinTree<T extends Comparable<T>>{
   T value;
   BinTree<T> left;
   BinTree<T> right;
}
Comments bubble
Note that with this design an empty tree is null, and thus is not an instance of BinTree.
Comments bubble
Note that with this design an empty tree is null, and thus is not an instance of BinTree.