~rdoering/ubuntu/karmic/erlang/fix-535090

« back to all changes in this revision

Viewing changes to lib/stdlib/doc/src/gb_trees.xml

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2009-02-15 16:42:52 UTC
  • mfrom: (3.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090215164252-q5x4rcf8a5pbesb1
Tags: 1:12.b.5-dfsg-2
Upload to unstable after lenny is released.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="latin1" ?>
 
2
<!DOCTYPE erlref SYSTEM "erlref.dtd">
 
3
 
 
4
<erlref>
 
5
  <header>
 
6
    <copyright>
 
7
      <year>2001</year>
 
8
      <year>2007</year>
 
9
      <holder>Ericsson AB, All Rights Reserved</holder>
 
10
    </copyright>
 
11
    <legalnotice>
 
12
  The contents of this file are subject to the Erlang Public License,
 
13
  Version 1.1, (the "License"); you may not use this file except in
 
14
  compliance with the License. You should have received a copy of the
 
15
  Erlang Public License along with this software. If not, it can be
 
16
  retrieved online at http://www.erlang.org/.
 
17
 
 
18
  Software distributed under the License is distributed on an "AS IS"
 
19
  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 
20
  the License for the specific language governing rights and limitations
 
21
  under the License.
 
22
 
 
23
  The Initial Developer of the Original Code is Ericsson AB.
 
24
    </legalnotice>
 
25
 
 
26
    <title>gb_trees</title>
 
27
    <prepared></prepared>
 
28
    <docno></docno>
 
29
    <date></date>
 
30
    <rev></rev>
 
31
  </header>
 
32
  <module>gb_trees</module>
 
33
  <modulesummary>General Balanced Trees</modulesummary>
 
34
  <description>
 
35
    <p>An efficient implementation of Prof. Arne Andersson's General
 
36
      Balanced Trees. These have no storage overhead compared to
 
37
      unbalaced binary trees, and their performance is in general
 
38
      better than AVL trees.</p>
 
39
  </description>
 
40
 
 
41
  <section>
 
42
    <title>Data structure</title>
 
43
    <p>Data structure:</p>
 
44
    <code type="none">
 
45
      
 
46
- {Size, Tree}, where `Tree' is composed of nodes of the form:
 
47
  - {Key, Value, Smaller, Bigger}, and the "empty tree" node:
 
48
  - nil.</code>
 
49
    <p>There is no attempt to balance trees after deletions. Since
 
50
      deletions do not increase the height of a tree, this should be OK.</p>
 
51
    <p>Original balance condition <em>h(T) &lt;= ceil(c * log(|T|))</em>
 
52
      has been changed to the similar (but not quite equivalent)
 
53
      condition <em>2 ^ h(T) &lt;= |T| ^ c</em>. This should also be OK.</p>
 
54
    <p>Performance is comparable to the AVL trees in the Erlang book
 
55
      (and faster in general due to less overhead); the difference is
 
56
      that deletion works for these trees, but not for the book's
 
57
      trees. Behaviour is logaritmic (as it should be).</p>
 
58
  </section>
 
59
 
 
60
  <section>
 
61
    <title>DATA TYPES</title>
 
62
    <code type="none">
 
63
gb_tree() = a GB tree</code>
 
64
  </section>
 
65
  <funcs>
 
66
    <func>
 
67
      <name>balance(Tree1) -> Tree2</name>
 
68
      <fsummary>Rebalance a tree</fsummary>
 
69
      <type>
 
70
        <v>Tree1 = Tree2 = gb_tree()</v>
 
71
      </type>
 
72
      <desc>
 
73
        <p>Rebalances <c>Tree1</c>. Note that this is rarely necessary,
 
74
          but may be motivated when a large number of nodes have been
 
75
          deleted from the tree without further insertions. Rebalancing
 
76
          could then be forced in order to minimise lookup times, since
 
77
          deletion only does not rebalance the tree.</p>
 
78
      </desc>
 
79
    </func>
 
80
    <func>
 
81
      <name>delete(Key, Tree1) -> Tree2</name>
 
82
      <fsummary>Remove a node from a tree</fsummary>
 
83
      <type>
 
84
        <v>Key = term()</v>
 
85
        <v>Tree1 = Tree2 = gb_tree()</v>
 
86
      </type>
 
87
      <desc>
 
88
        <p>Removes the node with key <c>Key</c> from <c>Tree1</c>;
 
89
          returns new tree. Assumes that the key is present in the tree,
 
90
          crashes otherwise.</p>
 
91
      </desc>
 
92
    </func>
 
93
    <func>
 
94
      <name>delete_any(Key, Tree1) -> Tree2</name>
 
95
      <fsummary>Remove a (possibly non-existing) node from a tree</fsummary>
 
96
      <type>
 
97
        <v>Key = term()</v>
 
98
        <v>Tree1 = Tree2 = gb_tree()</v>
 
99
      </type>
 
100
      <desc>
 
101
        <p>Removes the node with key <c>Key</c> from <c>Tree1</c> if
 
102
          the key is present in the tree, otherwise does nothing;
 
103
          returns new tree.</p>
 
104
      </desc>
 
105
    </func>
 
106
    <func>
 
107
      <name>empty() -> Tree</name>
 
108
      <fsummary>Return an empty tree</fsummary>
 
109
      <type>
 
110
        <v>Tree = gb_tree()</v>
 
111
      </type>
 
112
      <desc>
 
113
        <p>Returns a new empty tree</p>
 
114
      </desc>
 
115
    </func>
 
116
    <func>
 
117
      <name>enter(Key, Val, Tree1) -> Tree2</name>
 
118
      <fsummary>Insert or update key with value in a tree</fsummary>
 
119
      <type>
 
120
        <v>Key = Val = term()</v>
 
121
        <v>Tree1 = Tree2 = gb_tree()</v>
 
122
      </type>
 
123
      <desc>
 
124
        <p>Inserts <c>Key</c> with value <c>Val</c> into <c>Tree1</c> if
 
125
          the key is not present in the tree, otherwise updates
 
126
          <c>Key</c> to value <c>Val</c> in <c>Tree1</c>. Returns the
 
127
          new tree.</p>
 
128
      </desc>
 
129
    </func>
 
130
    <func>
 
131
      <name>from_orddict(List) -> Tree</name>
 
132
      <fsummary>Make a tree from an orddict</fsummary>
 
133
      <type>
 
134
        <v>List = [{Key, Val}]</v>
 
135
        <v>&nbsp;Key = Val = term()</v>
 
136
        <v>Tree = gb_tree()</v>
 
137
      </type>
 
138
      <desc>
 
139
        <p>Turns an ordered list <c>List</c> of key-value tuples into a
 
140
          tree. The list must not contain duplicate keys.</p>
 
141
      </desc>
 
142
    </func>
 
143
    <func>
 
144
      <name>get(Key, Tree) -> Val</name>
 
145
      <fsummary>Look up a key in a tree, if present</fsummary>
 
146
      <type>
 
147
        <v>Key = Val = term()</v>
 
148
        <v>Tree = gb_tree()</v>
 
149
      </type>
 
150
      <desc>
 
151
        <p>Retrieves the value stored with <c>Key</c> in <c>Tree</c>.
 
152
          Assumes that the key is present in the tree, crashes
 
153
          otherwise.</p>
 
154
      </desc>
 
155
    </func>
 
156
    <func>
 
157
      <name>lookup(Key, Tree) -> {value, Val} | none</name>
 
158
      <fsummary>Look up a key in a tree</fsummary>
 
159
      <type>
 
160
        <v>Key = Val = term()</v>
 
161
        <v>Tree = gb_tree()</v>
 
162
      </type>
 
163
      <desc>
 
164
        <p>Looks up <c>Key</c> in <c>Tree</c>; returns
 
165
          <c>{value, Val}</c>, or <c>none</c> if <c>Key</c> is not
 
166
          present.</p>
 
167
      </desc>
 
168
    </func>
 
169
    <func>
 
170
      <name>insert(Key, Val, Tree1) -> Tree2</name>
 
171
      <fsummary>Insert a new key and value in a tree</fsummary>
 
172
      <type>
 
173
        <v>Key = Val = term()</v>
 
174
        <v>Tree1 = Tree2 = gb_tree()</v>
 
175
      </type>
 
176
      <desc>
 
177
        <p>Inserts <c>Key</c> with value <c>Val</c> into <c>Tree1</c>;
 
178
          returns the new tree. Assumes that the key is not present in
 
179
          the tree, crashes otherwise.</p>
 
180
      </desc>
 
181
    </func>
 
182
    <func>
 
183
      <name>is_defined(Key, Tree) -> bool()</name>
 
184
      <fsummary>Test for membership of a tree</fsummary>
 
185
      <type>
 
186
        <v>Tree = gb_tree()</v>
 
187
      </type>
 
188
      <desc>
 
189
        <p>Returns <c>true</c> if <c>Key</c> is present in <c>Tree</c>,
 
190
          otherwise <c>false</c>.</p>
 
191
      </desc>
 
192
    </func>
 
193
    <func>
 
194
      <name>is_empty(Tree) -> bool()</name>
 
195
      <fsummary>Test for empty tree</fsummary>
 
196
      <type>
 
197
        <v>Tree = gb_tree()</v>
 
198
      </type>
 
199
      <desc>
 
200
        <p>Returns <c>true</c> if <c>Tree</c> is an empty tree, and
 
201
          <c>false</c> otherwise.</p>
 
202
      </desc>
 
203
    </func>
 
204
    <func>
 
205
      <name>iterator(Tree) -> Iter</name>
 
206
      <fsummary>Return an iterator for a tree</fsummary>
 
207
      <type>
 
208
        <v>Tree = gb_tree()</v>
 
209
        <v>Iter = term()</v>
 
210
      </type>
 
211
      <desc>
 
212
        <p>Returns an iterator that can be used for traversing the
 
213
          entries of <c>Tree</c>; see <c>next/1</c>. The implementation
 
214
          of this is very efficient; traversing the whole tree using
 
215
          <c>next/1</c> is only slightly slower than getting the list
 
216
          of all elements using <c>to_list/1</c> and traversing that.
 
217
          The main advantage of the iterator approach is that it does
 
218
          not require the complete list of all elements to be built in
 
219
          memory at one time.</p>
 
220
      </desc>
 
221
    </func>
 
222
    <func>
 
223
      <name>keys(Tree) -> [Key]</name>
 
224
      <fsummary>Return a list of the keys in a tree</fsummary>
 
225
      <type>
 
226
        <v>Tree = gb_tree()</v>
 
227
        <v>Key = term()</v>
 
228
      </type>
 
229
      <desc>
 
230
        <p>Returns the keys in <c>Tree</c> as an ordered list.</p>
 
231
      </desc>
 
232
    </func>
 
233
    <func>
 
234
      <name>largest(Tree) -> {Key, Val}</name>
 
235
      <fsummary>Return largest key and value</fsummary>
 
236
      <type>
 
237
        <v>Tree = gb_tree()</v>
 
238
        <v>Key = Val = term()</v>
 
239
      </type>
 
240
      <desc>
 
241
        <p>Returns <c>{Key, Val}</c>, where <c>Key</c> is the largest
 
242
          key in <c>Tree</c>, and <c>Val</c> is the value associated
 
243
          with this key. Assumes that the tree is nonempty.</p>
 
244
      </desc>
 
245
    </func>
 
246
    <func>
 
247
      <name>next(Iter1) -> {Key, Val, Iter2} | none</name>
 
248
      <fsummary>Traverse a tree with an iterator</fsummary>
 
249
      <type>
 
250
        <v>Iter1 = Iter2 = Key = Val = term()</v>
 
251
      </type>
 
252
      <desc>
 
253
        <p>Returns <c>{Key, Val, Iter2}</c> where <c>Key</c> is the
 
254
          smallest key referred to by the iterator <c>Iter1</c>, and
 
255
          <c>Iter2</c> is the new iterator to be used for
 
256
          traversing the remaining nodes, or the atom <c>none</c> if no
 
257
          nodes remain.</p>
 
258
      </desc>
 
259
    </func>
 
260
    <func>
 
261
      <name>size(Tree) -> int()</name>
 
262
      <fsummary>Return the number of nodes in a tree</fsummary>
 
263
      <type>
 
264
        <v>Tree = gb_tree()</v>
 
265
      </type>
 
266
      <desc>
 
267
        <p>Returns the number of nodes in <c>Tree</c>.</p>
 
268
      </desc>
 
269
    </func>
 
270
    <func>
 
271
      <name>smallest(Tree) -> {Key, Val}</name>
 
272
      <fsummary>Return smallest key and value</fsummary>
 
273
      <type>
 
274
        <v>Tree = gb_tree()</v>
 
275
        <v>Key = Val = term()</v>
 
276
      </type>
 
277
      <desc>
 
278
        <p>Returns <c>{Key, Val}</c>, where <c>Key</c> is the smallest
 
279
          key in <c>Tree</c>, and <c>Val</c> is the value associated
 
280
          with this key. Assumes that the tree is nonempty.</p>
 
281
      </desc>
 
282
    </func>
 
283
    <func>
 
284
      <name>take_largest(Tree1) -> {Key, Val, Tree2}</name>
 
285
      <fsummary>Extract largest key and value</fsummary>
 
286
      <type>
 
287
        <v>Tree1 = Tree2 = gb_tree()</v>
 
288
        <v>Key = Val = term()</v>
 
289
      </type>
 
290
      <desc>
 
291
        <p>Returns <c>{Key, Val, Tree2}</c>, where <c>Key</c> is the
 
292
          largest key in <c>Tree1</c>, <c>Val</c> is the value
 
293
          associated with this key, and <c>Tree2</c> is this tree with
 
294
          the corresponding node deleted. Assumes that the tree is
 
295
          nonempty.</p>
 
296
      </desc>
 
297
    </func>
 
298
    <func>
 
299
      <name>take_smallest(Tree1) -> {Key, Val, Tree2}</name>
 
300
      <fsummary>Extract smallest key and value</fsummary>
 
301
      <type>
 
302
        <v>Tree1 = Tree2 = gb_tree()</v>
 
303
        <v>Key = Val = term()</v>
 
304
      </type>
 
305
      <desc>
 
306
        <p>Returns <c>{Key, Val, Tree2}</c>, where <c>Key</c> is the
 
307
          smallest key in <c>Tree1</c>, <c>Val</c> is the value
 
308
          associated with this key, and <c>Tree2</c> is this tree with
 
309
          the corresponding node deleted. Assumes that the tree is
 
310
          nonempty.</p>
 
311
      </desc>
 
312
    </func>
 
313
    <func>
 
314
      <name>to_list(Tree) -> [{Key, Val}]</name>
 
315
      <fsummary>Convert a tree into a list</fsummary>
 
316
      <type>
 
317
        <v>Tree = gb_tree()</v>
 
318
        <v>Key = Val = term()</v>
 
319
      </type>
 
320
      <desc>
 
321
        <p>Converts a tree into an ordered list of key-value tuples.</p>
 
322
      </desc>
 
323
    </func>
 
324
    <func>
 
325
      <name>update(Key, Val, Tree1) -> Tree2</name>
 
326
      <fsummary>Update a key to new value in a tree</fsummary>
 
327
      <type>
 
328
        <v>Key = Val = term()</v>
 
329
        <v>Tree1 = Tree2 = gb_tree()</v>
 
330
      </type>
 
331
      <desc>
 
332
        <p>Updates <c>Key</c> to value <c>Val</c> in <c>Tree1</c>;
 
333
          returns the new tree. Assumes that the key is present in the
 
334
          tree.</p>
 
335
      </desc>
 
336
    </func>
 
337
    <func>
 
338
      <name>values(Tree) -> [Val]</name>
 
339
      <fsummary>Return a list of the values in a tree</fsummary>
 
340
      <type>
 
341
        <v>Tree = gb_tree()</v>
 
342
        <v>Val = term()</v>
 
343
      </type>
 
344
      <desc>
 
345
        <p>Returns the values in <c>Tree</c> as an ordered list, sorted
 
346
          by their corresponding keys. Duplicates are not removed.</p>
 
347
      </desc>
 
348
    </func>
 
349
  </funcs>
 
350
 
 
351
  <section>
 
352
    <title>SEE ALSO</title>
 
353
    <p><seealso marker="gb_sets">gb_sets(3)</seealso>, 
 
354
      <seealso marker="dict">dict(3)</seealso></p>
 
355
  </section>
 
356
</erlref>
 
357