File: gb_trees.html

package info (click to toggle)
erlang-doc-html 1%3A11.b.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 23,284 kB
  • ctags: 10,724
  • sloc: erlang: 505; ansic: 323; makefile: 62; perl: 61; sh: 45
file content (496 lines) | stat: -rw-r--r-- 12,937 bytes parent folder | download
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
  <TITLE>gb_trees</TITLE>
  <SCRIPT type="text/javascript" src="../../../../doc/erlresolvelinks.js">
</SCRIPT>
  <STYLE TYPE="text/css">
<!--
    .REFBODY     { margin-left: 13mm }
    .REFTYPES    { margin-left: 8mm }
-->
  </STYLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
      ALINK="#FF0000">
<!-- refpage -->
<CENTER>
<A HREF="http://www.erlang.se">
  <IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif">
</A>
<H1>gb_trees</H1>
</CENTER>

<H3>MODULE</H3>
<DIV CLASS=REFBODY>
gb_trees
</DIV>

<H3>MODULE SUMMARY</H3>
<DIV CLASS=REFBODY>
General Balanced Trees
</DIV>

<H3>DESCRIPTION</H3>
<DIV CLASS=REFBODY>

<P>An efficient implementation of Prof. Arne Andersson's General
Balanced Trees. These have no storage overhead compared to
unbalaced binary trees, and their performance is in general
better than AVL trees.
</DIV>

<H3>Data structure</H3>
<DIV CLASS=REFBODY>

<P> Data structure:

<PRE>
      
- {Size, Tree}, where `Tree' is composed of nodes of the form:
  - {Key, Value, Smaller, Bigger}, and the &#34;empty tree&#34; node:
  - nil.
    
</PRE>

<P>There is no attempt to balance trees after deletions. Since
deletions do not increase the height of a tree, this should be OK.

<P>Original balance condition <STRONG>h(T) &#60;= ceil(c * log(|T|))</STRONG>
has been changed to the similar (but not quite equivalent)
condition <STRONG>2 ^ h(T) &#60;= |T| ^ c</STRONG>. This should also be OK.

<P>Performance is comparable to the AVL trees in the Erlang book
(and faster in general due to less overhead); the difference is
that deletion works for these trees, but not for the book's
trees. Behaviour is logaritmic (as it should be).
</DIV>

<H3>DATA TYPES</H3>
<DIV CLASS=REFBODY>

<PRE>
gb_tree() = a GB tree
    
</PRE>

</DIV>

<H3>EXPORTS</H3>

<P><A NAME="balance/1"><STRONG><CODE>balance(Tree1) -&#62; Tree2</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Tree1 = Tree2 = gb_tree()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Rebalances <CODE>Tree1</CODE>. Note that this is rarely necessary,
         but may be motivated when a large number of nodes have been
         deleted from the tree without further insertions. Rebalancing
         could then be forced in order to minimise lookup times, since
         deletion only does not rebalance the tree.
</DIV>

<P><A NAME="delete/2"><STRONG><CODE>delete(Key, Tree1) -&#62; Tree2</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Key = term()</CODE></STRONG><BR>
<STRONG><CODE>Tree1 = Tree2 = gb_tree()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Removes the node with key <CODE>Key</CODE> from <CODE>Tree1</CODE>;
         returns new tree. Assumes that the key is present in the tree,
         crashes otherwise.
</DIV>

<P><A NAME="delete_any/2"><STRONG><CODE>delete_any(Key, Tree1) -&#62; Tree2</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Key = term()</CODE></STRONG><BR>
<STRONG><CODE>Tree1 = Tree2 = gb_tree()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Removes the node with key <CODE>Key</CODE> from <CODE>Tree1</CODE> if
         the key is present in the tree, otherwise does nothing;
         returns new tree.
</DIV>

<P><A NAME="empty/0"><STRONG><CODE>empty() -&#62; Tree</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Tree = gb_tree()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Returns a new empty tree
</DIV>

<P><A NAME="enter/3"><STRONG><CODE>enter(Key, Val, Tree1) -&#62; Tree2</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Key = Val = term()</CODE></STRONG><BR>
<STRONG><CODE>Tree1 = Tree2 = gb_tree()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Inserts <CODE>Key</CODE> with value <CODE>Val</CODE> into <CODE>Tree1</CODE> if
         the key is not present in the tree, otherwise updates
         <CODE>Key</CODE> to value <CODE>Val</CODE> in <CODE>Tree1</CODE>. Returns the
         new tree.
</DIV>

<P><A NAME="from_orddict/1"><STRONG><CODE>from_orddict(List) -&#62; Tree</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>List = [{Key, Val}]</CODE></STRONG><BR>
<STRONG><CODE>Key = Val = term()</CODE></STRONG><BR>
<STRONG><CODE>Tree = gb_tree()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Turns an ordered list <CODE>List</CODE> of key-value tuples into a
         tree. The list must not contain duplicate keys.
</DIV>

<P><A NAME="get/2"><STRONG><CODE>get(Key, Tree) -&#62; Val</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Key = Val = term()</CODE></STRONG><BR>
<STRONG><CODE>Tree = gb_tree()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Retrieves the value stored with <CODE>Key</CODE> in <CODE>Tree</CODE>.
         Assumes that the key is present in the tree, crashes
         otherwise.
</DIV>

<P><A NAME="lookup/2"><STRONG><CODE>lookup(Key, Tree) -&#62; {value, Val} | none</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Key = Val = term()</CODE></STRONG><BR>
<STRONG><CODE>Tree = gb_tree()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Looks up <CODE>Key</CODE> in <CODE>Tree</CODE>; returns
         <CODE>{value, Val}</CODE>, or <CODE>none</CODE> if <CODE>Key</CODE> is not
         present.
</DIV>

<P><A NAME="insert/3"><STRONG><CODE>insert(Key, Val, Tree1) -&#62; Tree2</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Key = Val = term()</CODE></STRONG><BR>
<STRONG><CODE>Tree1 = Tree2 = gb_tree()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Inserts <CODE>Key</CODE> with value <CODE>Val</CODE> into <CODE>Tree1</CODE>;
         returns the new tree. Assumes that the key is not present in
         the tree, crashes otherwise.
</DIV>

<P><A NAME="is_defined/2"><STRONG><CODE>is_defined(Key, Tree) -&#62; bool()</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Tree = gb_tree()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Returns <CODE>true</CODE> if <CODE>Key</CODE> is present in <CODE>Tree</CODE>,
         otherwise <CODE>false</CODE>.
</DIV>

<P><A NAME="is_empty/1"><STRONG><CODE>is_empty(Tree) -&#62; bool()</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Tree = gb_tree()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Returns <CODE>true</CODE> if <CODE>Tree</CODE> is an empty tree, and
         <CODE>false</CODE> otherwise.
</DIV>

<P><A NAME="iterator/1"><STRONG><CODE>iterator(Tree) -&#62; Iter</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Tree = gb_tree()</CODE></STRONG><BR>
<STRONG><CODE>Iter = term()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Returns an iterator that can be used for traversing the
         entries of <CODE>Tree</CODE>; see <CODE>next/1</CODE>. The implementation
         of this is very efficient; traversing the whole tree using
         <CODE>next/1</CODE> is only slightly slower than getting the list
         of all elements using <CODE>to_list/1</CODE> and traversing that.
         The main advantage of the iterator approach is that it does
         not require the complete list of all elements to be built in
         memory at one time.
</DIV>

<P><A NAME="keys/1"><STRONG><CODE>keys(Tree) -&#62; [Key]</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Tree = gb_tree()</CODE></STRONG><BR>
<STRONG><CODE>Key = term()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Returns the keys in <CODE>Tree</CODE> as an ordered list.
</DIV>

<P><A NAME="largest/1"><STRONG><CODE>largest(Tree) -&#62; {Key, Val}</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Tree = gb_tree()</CODE></STRONG><BR>
<STRONG><CODE>Key = Val = term()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Returns <CODE>{Key, Val}</CODE>, where <CODE>Key</CODE> is the largest
         key in <CODE>Tree</CODE>, and <CODE>Val</CODE> is the value associated
         with this key. Assumes that the tree is nonempty.
</DIV>

<P><A NAME="next/1"><STRONG><CODE>next(Iter1) -&#62; {Key, Val, Iter2</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Iter1 = Iter2 = Key = Val = term()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Returns <CODE>{Key, Val, Iter2}</CODE> where <CODE>Key</CODE> is the
         smallest key referred to by the iterator <CODE>Iter1</CODE>, and
         <CODE>Iter2</CODE> is the new iterator to be used for
         traversing the remaining nodes, or the atom <CODE>none</CODE> if no
         nodes remain.
</DIV>

<P><A NAME="size/1"><STRONG><CODE>size(Tree) -&#62; int()</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Tree = gb_tree()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Returns the number of nodes in <CODE>Tree</CODE>.
</DIV>

<P><A NAME="smallest/1"><STRONG><CODE>smallest(Tree) -&#62; {Key, Val}</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Tree = gb_tree()</CODE></STRONG><BR>
<STRONG><CODE>Key = Val = term()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Returns <CODE>{Key, Val}</CODE>, where <CODE>Key</CODE> is the smallest
         key in <CODE>Tree</CODE>, and <CODE>Val</CODE> is the value associated
         with this key. Assumes that the tree is nonempty.
</DIV>

<P><A NAME="take_largest/1"><STRONG><CODE>take_largest(Tree1) -&#62; {Key, Val, Tree2}</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Tree1 = Tree2 = gb_tree()</CODE></STRONG><BR>
<STRONG><CODE>Key = Val = term()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Returns <CODE>{Key, Val, Tree2}</CODE>, where <CODE>Key</CODE> is the
         largest key in <CODE>Tree1</CODE>, <CODE>Val</CODE> is the value
         associated with this key, and <CODE>Tree2</CODE> is this tree with
         the corresponding node deleted. Assumes that the tree is
         nonempty.
</DIV>

<P><A NAME="take_smallest/1"><STRONG><CODE>take_smallest(Tree1) -&#62; {Key, Val, Tree2}</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Tree1 = Tree2 = gb_tree()</CODE></STRONG><BR>
<STRONG><CODE>Key = Val = term()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Returns <CODE>{Key, Val, Tree2}</CODE>, where <CODE>Key</CODE> is the
         smallest key in <CODE>Tree1</CODE>, <CODE>Val</CODE> is the value
         associated with this key, and <CODE>Tree2</CODE> is this tree with
         the corresponding node deleted. Assumes that the tree is
         nonempty.
</DIV>

<P><A NAME="to_list/1"><STRONG><CODE>to_list(Tree) -&#62; [{Key, Val}]</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Tree = gb_tree()</CODE></STRONG><BR>
<STRONG><CODE>Key = Val = term()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Converts a tree into an ordered list of key-value tuples.
</DIV>

<P><A NAME="update/3"><STRONG><CODE>update(Key, Val, Tree1) -&#62; Tree2</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Key = Val = term()</CODE></STRONG><BR>
<STRONG><CODE>Tree1 = Tree2 = gb_tree()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Updates <CODE>Key</CODE> to value <CODE>Val</CODE> in <CODE>Tree1</CODE>;
         returns the new tree. Assumes that the key is present in the
         tree.
</DIV>

<P><A NAME="values/1"><STRONG><CODE>values(Tree) -&#62; [Val]</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Tree = gb_tree()</CODE></STRONG><BR>
<STRONG><CODE>Val = term()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Returns the values in <CODE>Tree</CODE> as an ordered list, sorted
         by their corresponding keys. Duplicates are not removed.
</DIV>

<H3>SEE ALSO</H3>
<DIV CLASS=REFBODY>

<P> <A HREF="gb_sets.html">gb_sets(3)</A>, 
<A HREF="dict.html">dict(3)</A>

</DIV>

<H3>AUTHORS</H3>
<DIV CLASS=REFBODY>
Sven-Olof Nystrom, Richard Carlsson - support@erlang.ericsson.se<BR>

</DIV>
<CENTER>
<HR>
<SMALL>stdlib 1.14.2<BR>
Copyright &copy; 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>