1 .\" Id: tree.mdoc,v 1.3 2004/03/09 06:30:09 marka Exp
3 .\" Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
4 .\" Copyright (c) 1995-1999 by Internet Software Consortium
6 .\" Permission to use, copy, modify, and distribute this software for any
7 .\" purpose with or without fee is hereby granted, provided that the above
8 .\" copyright notice and this permission notice appear in all copies.
10 .\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
11 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
13 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 .\" OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 .Nd balanced binary tree routines
31 .Fn tree_init "void **tree"
33 .Fn tree_srch "void **tree" "int (*compare)()" "void *data"
35 .Fn tree_add "void **tree" "int (*compare)()" \
36 "void *data" "void (*del_uar)()"
38 .Fn tree_delete "void **tree" "int (*compare)()" \
39 "void *data" "void (*del_uar)()"
41 .Fn tree_trav "void **tree" "int (*trav_uar)()"
43 .Fn tree_mung "void **tree" "void (*del_uar)()"
45 These functions create and manipulate a balanced binary (AVL) tree. Each node
46 of the tree contains the expected left & right subtree pointers, a short int
47 balance indicator, and a pointer to the user data. On a 32 bit system, this
48 means an overhead of 4+4+2+4 bytes per node (or, on a RISC or otherwise
49 alignment constrained system with implied padding, 4+4+4+4 bytes per node).
50 There is no key data type enforced by this package; a caller supplied
51 compare routine is used to compare user data blocks.
53 Balanced binary trees are very fast on searches and replacements, but have a
54 moderately high cost for additions and deletions. If your application does a
55 lot more searches and replacements than it does additions and deletions, the
56 balanced (AVL) binary tree is a good choice for a data structure.
59 creates an empty tree and binds it to
61 (which for this and all other routines in this package should be declared as
62 a pointer to void or int, and passed by reference), which can then be used by
63 other routines in this package. Note that more than one
65 variable can exist at once; thus multiple trees can be manipulated
69 searches a tree for a specific node and returns either
71 if no node was found, or the value of the user data pointer if the node
74 is the address of a function to compare two user data blocks. This routine
75 should work much the way
79 could be used if the user data was a \s-2NUL\s+2 terminated string.
81 is the address of a user data block to be used by
83 as the search criteria. The tree is searched for a node where
88 inserts or replaces a node in the specified tree. The tree specified by
92 and if a node is found to match
96 function, if non\-\s-2NULL\s+2, is called with the address of the user data
97 block for the node (this routine should deallocate any dynamic memory which
98 is referenced exclusively by the node); the user data pointer for the node
99 is then replaced by the value of
101 If no node is found to match, a new node is added (which may or may not
102 cause a transparent rebalance operation), with a user data pointer equal to
104 A rebalance may or may not occur, depending on where the node is added
105 and what the rest of the tree looks like.
109 pointer unless catastrophe occurs in which case it will return \s-2NULL\s+2.
114 A rebalance may or may not occur, depending on where the node is removed from
115 and what the rest of the tree looks like.
117 returns TRUE if a node was deleted, FALSE otherwise.
124 with the address of each user data block. If
126 returns FALSE at any time,
128 will immediately return FALSE to its caller. Otherwise all nodes will be
134 deletes every node in
138 (if it is not \s-2NULL\s+2) with the user data address from each node (see
142 above). The tree is left in the same state that
144 leaves it in \- i.e., empty.
146 Should have a way for the caller to specify application-specific
150 functions to be used internally when allocating meta data.
152 Paul Vixie, converted and augumented from Modula\-2 examples in
153 .Dq Algorithms & Data Structures ,
154 Niklaus Wirth, Prentice\-Hall, ISBN 0\-13\-022005\-1.