1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
|
/// In a height-balanced binary tree, the following property holds for every node: The
/// height of its left subtree and the height of its right subtree are almost equal,
/// which means their difference is not greater than one.
///
/// Example:
/// ?- hbal_tree(3,T).
/// T = t(x, t(x, t(x, nil, nil), t(x, nil, nil)), t(x, t(x, nil, nil), t(x, nil, nil))) ;
/// T = t(x, t(x, t(x, nil, nil), t(x, nil, nil)), t(x, t(x, nil, nil), nil)) ;
/// etc......No
///
/// Example in F#:
///
/// > hbalTree 'x' 3 |> Seq.take 4;;
/// val it : seq<char Tree> =
/// seq
/// [Branch
/// ('x',Branch ('x',Branch ('x',Empty,Empty),Branch ('x',Empty,Empty)),
/// Branch ('x',Branch ('x',Empty,Empty),Branch ('x',Empty,Empty)));
/// Branch
/// ('x',Branch ('x',Branch ('x',Empty,Empty),Branch ('x',Empty,Empty)),
/// Branch ('x',Branch ('x',Empty,Empty),Empty));
/// Branch
/// ('x',Branch ('x',Branch ('x',Empty,Empty),Branch ('x',Empty,Empty)),
/// Branch ('x',Empty,Branch ('x',Empty,Empty)));
/// Branch
/// ('x',Branch ('x',Branch ('x',Empty,Empty),Branch ('x',Empty,Empty)),
/// Branch ('x',Empty,Empty))]
(Solution)
|