blob: f9dbc98bb9c082c1797762508681fa2c4ec69c55 (
plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Complete binary trees %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The macro \b@nary{<number>} expands to the description of a complete
% binary tree with <number> many internal nodes, where each level is filled with
% the maximal number of internal nodes, and the last level of internal nodes
% is filled from left to right.
\newcount\b@nno % number of nodes
\newcount\b@nlv % number of complete levels
\newcount\b@ndl % number of nodes on incomplete level
\def\ld(#1,#2,#3){% #1, #2, and #3 must be counter registers.
% #1 is the input, #1 must be >= 1.
% \ld makes the following assignments:
% #2:=|_log_2(#1)_|, #3:=2^#2.
% The contents of #1 is destroyed during the computation.
#2=0 #3=1
\loop\ifnum #1>\@ne\relax
\divide #1 by\tw@ % this is integer division
\advance #2 by\@ne
\multiply #3 by\tw@
\repeat}
\def\b@nary#1{% draws a complete binary tree with #1 internal nodes,
% a complete binary tree with N internal nodes has
% lv:=|_log_2(N+1)_| many
% complete level of binary nodes and dl:=N-2^{lv}+1 many internal
% nodes on an incomplete level.
\b@nno=#1\relax\advance\b@nno by \@ne
\ld(\b@nno,\b@nlv,\b@ndl)%
\b@ndl=-\b@ndl\advance\b@ndl by #1\advance\b@ndl by\@ne
\b@n}
\def\b@n{%
\ifnum\b@nlv>\@ne
\advance\b@nlv by-\@ne
\b@n
\b@n
\advance\b@nlv by\@ne
\node{}
\else\ifnum\b@ndl>\@ne
\advance\b@ndl by-\tw@
\node{\le@f\external}\node{\le@f\external}\node{}%
\node{\le@f\external}\node{\le@f\external}\node{}%
\node{}%
\else\ifnum\b@ndl=\@ne
\advance\b@ndl by-\@ne
\node{\le@f\external}\node{\le@f\external}\node{}%
\node{\le@f\external}%
\node{}%
\else\node{\le@f\external}\node{\le@f\external}\node{}%
\fi
\fi
\fi}
\def\circleleaves{\def\le@f{\type{circle}}}
\def\squareleaves{\def\le@f{\type{square}}}
\newcount\no@
\def\no#1{\no@=#1\relax}
\def\binary#1{%
\no{1}\circleleaves
#1%
\b@nary{\no@}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Fibonacci trees %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \f@b expands to the description of a Fibonacci tree
% of height \f@bht.
\newcount\f@bht
\def\f@b{% draws a Fibonacci tree of depth #1
\ifnum\f@bht>1
\advance\f@bht by-\@ne\f@b\advance\f@bht by\@ne
\advance\f@bht by-\tw@\f@b\advance\f@bht by\tw@
\ifunn@des\node{\unary}
\fi
\node{\lefttop}
\else\ifnum\f@bht=1
\node{\external\le@f}
\node{\external\le@f}
\node{}
\else\node{\external\le@f}
\fi
\fi}
\newif\ifunn@des
\let\unarynodes\unn@destrue
\def\hght#1{\f@bht=#1\relax}
\def\fibonacci#1{%
\hght{0}\unn@desfalse\circleleaves
#1%
\f@b}
%
|