summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/asymptote/binarytree.asy
blob: 31e0e3aab61a1b0e0c8646316909fa72d86470e1 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* **********************************************************************
 * binarytree: An Asymptote module to draw binary trees                 *
 *                                                                      *
 * Copyright(C) 2006                                                    *
 * Tobias Langner tobias[at]langner[dot]nightlabs[dot]de                *
 *                                                                      *
 * Modified by John Bowman                                              *
 *                                                                      *
 * Condensed mode:                                                      *
 * Copyright(C) 2012                                                    *
 * Gerasimos Dimitriadis dimeg [at] intracom [dot] gr                   *
 *                                                                      *
 ************************************************************************
 *                                                                      *
 * This library is free software; you can redistribute it and/or        *
 * modify it under the terms of the GNU Lesser General Public           *
 * License as published by the Free Software Foundation; either         *
 * version 3 of the License, or(at your option) any later version.      *
 *                                                                      *
 * This library is distributed in the hope that it will be useful,      *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    *
 * Lesser General Public License for more details.                      *
 *                                                                      *
 * You should have received a copy of the GNU Lesser General Public     *
 * License along with this library; if not, write to the                *
 *     Free Software Foundation, Inc.,                                  *
 *     51 Franklin St, Fifth Floor,                                     *
 *     Boston, MA  02110-1301  USA                                      *
 *                                                                      *
 * Or get it online:                                                    *
 *     http: //www.gnu.org/copyleft/lesser.html                         *
 *                                                                      *
 ***********************************************************************/

// default values
real minDistDefault=0.2cm;
real nodeMarginDefault=0.1cm;

// structure to represent nodes in a binary tree
struct binarytreeNode {
  int key;
  binarytreeNode left;
  binarytreeNode right;
  binarytreeNode parent;
  bool spans_calculated=false;
  int left_span,total_left_span;
  int right_span,total_right_span;
  void update_spans();

  // Get the horizontal span of the tree consisting of the current 
  // node plus the whole subtree that is rooted at the right child 
  // (condensed mode)
  int getTotalRightSpan() {
    if(spans_calculated == false) {
      update_spans();
    }

    return total_right_span;
  }

  // Get the horizontal span of the tree consisting of the current 
  // node plus the whole subtree that is rooted at the left child 
  // (condensed mode)
  int getTotalLeftSpan() {
    if(spans_calculated == false) {
      update_spans();
    }
    return total_left_span;
  }

  // Get the horizontal distance between this node and its right child
  // (condensed mode)
  int getRightSpan() {
    if(spans_calculated == false) {
      update_spans();
    }
    return right_span;
  }

  // Get the horizontal distance between this node and its left child
  // (condensed mode)
  int getLeftSpan() {
    if(spans_calculated == false) {
      update_spans();
    }
    return left_span;
  }

  // Update all span figures for this node. 
  // condensed mode)
  update_spans=new void() {
    if(spans_calculated == true)
      return;

    left_span=0;
    total_left_span=0;
    right_span=0;
    total_right_span=0;

    if(left != null) {
      left_span=left.getTotalRightSpan()+1;
      total_left_span=left_span+left.getTotalLeftSpan();
    }

    if(right != null) {
      right_span=right.getTotalLeftSpan()+1;
      total_right_span=right_span+right.getTotalRightSpan();
    }
    spans_calculated=true;
  };

  // set the left child of this node
  void setLeft(binarytreeNode left) {
    this.left=left;
    this.left.parent=this;
  }

  // set the right child of this node
  void setRight(binarytreeNode right) {
    this.right=right;
    this.right.parent=this;
  }

  // return a boolean indicating whether this node is the root
  bool isRoot() {
    return parent == null;
  }

  // return the level of the subtree rooted at this node.
  int getLevel() {
    if(isRoot())
      return 1;
    else
      return parent.getLevel()+1;
  }
        
  // set the children of this binarytreeNode
  void setChildren(binarytreeNode left, binarytreeNode right) {
    setLeft(left);
    setRight(right);
  }
        
  // create a new binarytreeNode with key <key> 
  static binarytreeNode binarytreeNode(int key) {
    binarytreeNode toReturn=new binarytreeNode;
    toReturn.key=key;
    return toReturn;
  }
        
  // returns the height of the subtree rooted at this node.
  int getHeight() {
    if(left == null && right == null)
      return 1;
    if(left == null)
      return right.getHeight()+1;
    if(right == null)
      return left.getHeight()+1;
                
    return max(left.getHeight(),right.getHeight())+1;
  }
}

binarytreeNode operator init() {return null;}

// "constructor" for binarytreeNode
binarytreeNode binarytreeNode(int key)=binarytreeNode.binarytreeNode;

// draw the tree rooted at the given <node> at the given position <pos>, with
// <height>=the height of the containing tree,
// <minDist>=the minimal horizontal distance of two nodes at the lowest level,
// <levelDist>=the vertical distance between two levels,
// <nodeDiameter>=the diameter of one node.
object draw(picture pic=currentpicture, binarytreeNode node, pair pos,
            int height, real minDist, real levelDist, real nodeDiameter,
            pen p=currentpen, bool condensed=false) {
  Label label=Label(math((string) node.key),pos);
        
  binarytreeNode left=node.left;        
  binarytreeNode right=node.right;

  // return the distance for two nodes at the given <level> when the
  // containing tree has height <height> 
  // and the minimal distance between two nodes is <minDist> .
  real getDistance(int level, int height, real minDist) {
    return(nodeDiameter+minDist)*2^(height-level);
  }

  // return the horiontal distance between node <n> and its left child
  // (condensed mode)
  real getLeftDistance(binarytreeNode n) {
    return(nodeDiameter+minDist) *(real)n.getLeftSpan() * 0.5;
  }

  // return the horiontal distance between node <n> and its right child
  // (condensed mode)
  real getRightDistance(binarytreeNode n) {
    return(nodeDiameter+minDist) *(real)n.getRightSpan() * 0.5;
  }

  real dist=getDistance(node.getLevel(),height,minDist)/2;

  // draw the connection between the two nodes at the given positions
  // by calculating the connection points and drawing the corresponding
  // arrow.
  void deferredDrawNodeConnection(pair parentPos, pair childPos) {
    pic.add(new void(frame f, transform t) {
        pair start,end; 
        // calculate connection path 
        transform T=shift(nodeDiameter/2*unit(t*childPos-t*parentPos));  
        path arr=(T*t*parentPos)--(inverse(T)*t*childPos);  
        draw(f,PenMargin(arr,p).g,p,Arrow(5));  
      }); 
    pic.addPoint(parentPos);
    pic.addPoint(childPos);
  } 

  if(left != null) {
    pair childPos;
    if(condensed == false) {
      childPos=pos-(0,levelDist)-(dist/2,0);
    }
    else {
      childPos=pos-(0,levelDist)-((real)getLeftDistance(node),0);
    }
    draw(pic,left,childPos,height,minDist,levelDist,nodeDiameter,p,condensed);
    deferredDrawNodeConnection(pos,childPos);
  }

  if(right != null) {
    pair childPos;
    if(condensed == false) {
      childPos=pos-(0,levelDist)+(dist/2,0);
    }
    else {
      childPos=pos-(0,levelDist)+((real)getRightDistance(node),0);
    }
    draw(pic,right,childPos,height,minDist,levelDist,nodeDiameter,p,condensed);
    deferredDrawNodeConnection(pos,childPos);
  }
        
  picture obj;
  draw(obj,circle((0,0),nodeDiameter/2),p);
  label(obj,label,(0,0),p);
        
  add(pic,obj,pos);
        
  return label;
}

struct key {
  int n;
  bool active;
}

key key(int n, bool active=true) {key k; k.n=n; k.active=active; return k;}

key operator cast(int n) {return key(n);}
int operator cast(key k) {return k.n;}
int[] operator cast(key[] k) {
  int[] I;
  for(int i=0; i < k.length; ++i)
    I[i]=k[i].n;
  return I;
}

key nil=key(0,false);

// structure to represent a binary tree.
struct binarytree {
  binarytreeNode root;
  int[] keys;
        
  // add the given <key> to the tree by searching for its place and
  // inserting it there.
  void addKey(int key) {
    binarytreeNode newNode=binarytreeNode(key);
                
    if(root == null) {
      root=newNode;
      keys.push(key);
      return; 
    }
                
    binarytreeNode n=root;
    while(n != null) {
      if(key < n.key) {
        if(n.left != null)
          n=n.left;
        else {
          n.setLeft(newNode);
          keys.push(key);
          return;
        }
      } else if(key > n.key) {
        if(n.right != null)
          n=n.right;
        else {
          n.setRight(newNode);
          keys.push(key);
          return;
        }
      }
    }
  }
        
  // return the height of the tree
  int getHeight() {
    if(root == null)
      return 0;
    else
      return root.getHeight();
  }
        
  // add all given keys to the tree sequentially
  void addSearchKeys(int[] keys) {
    for(int i=0; i < keys.length; ++i) {
      int key=keys[i];
      // Ignore duplicate keys
      if(find(this.keys == key) == -1)
        addKey(key);
    }
  }
        
  binarytreeNode build(key[] keys, int[] ind) {
    if(ind[0] >= keys.length) return null;
    key k=keys[ind[0]];
    ++ind[0];
    if(!k.active) return null;
    binarytreeNode bt=binarytreeNode(k);
    binarytreeNode left=build(keys,ind);
    binarytreeNode right=build(keys,ind);
    bt.left=left; bt.right=right;
    if(left != null) left.parent=bt;
    if(right != null) right.parent=bt;
    return bt;
  }

  void addKeys(key[] keys) {
    int[] ind={0};
    root=build(keys,ind);
    this.keys=keys;
  }


  // return all key in the tree
  int[] getKeys() {
    return keys;
  }
}

binarytree searchtree(...int[] keys)
{
  binarytree bt;
  bt.addSearchKeys(keys);
  return bt;
}

binarytree binarytree(...key[] keys)
{
  binarytree bt;
  bt.addKeys(keys);
  return bt;
}

// draw the given binary tree.
void draw(picture pic=currentpicture, binarytree tree,
          real minDist=minDistDefault, real nodeMargin=nodeMarginDefault,
          pen p=currentpen, bool condensed=false)
{
  int[] keys=tree.getKeys();
        
  // calculate the node diameter so that all keys fit into it
  frame f; 
  for(int i=0; i < keys.length; ++i)
    label(f,math(string(keys[i])),p);

  real nodeDiameter=abs(max(f)-min(f))+2*nodeMargin;
  real levelDist=nodeDiameter*1.8;

  draw(pic,tree.root,(0,0),tree.getHeight(),minDist,levelDist,nodeDiameter,p,
       condensed);
}