xref: /netbsd-src/external/bsd/libbind/dist/isc/tree.mdoc (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1*b5677b36Schristos.\" Id: tree.mdoc,v 1.3 2004/03/09 06:30:09 marka Exp
2*b5677b36Schristos.\"
3*b5677b36Schristos.\" Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
4*b5677b36Schristos.\" Copyright (c) 1995-1999 by Internet Software Consortium
5*b5677b36Schristos.\"
6*b5677b36Schristos.\" Permission to use, copy, modify, and distribute this software for any
7*b5677b36Schristos.\" purpose with or without fee is hereby granted, provided that the above
8*b5677b36Schristos.\" copyright notice and this permission notice appear in all copies.
9*b5677b36Schristos.\"
10*b5677b36Schristos.\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
11*b5677b36Schristos.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*b5677b36Schristos.\" MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
13*b5677b36Schristos.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*b5677b36Schristos.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*b5677b36Schristos.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16*b5677b36Schristos.\" OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*b5677b36Schristos.\"
18*b5677b36Schristos.Dd April 5, 1994
19*b5677b36Schristos.Dt TREE 3
20*b5677b36Schristos.Os BSD 4
21*b5677b36Schristos.Sh NAME
22*b5677b36Schristos.Nm tree_init ,
23*b5677b36Schristos.Nm tree_mung ,
24*b5677b36Schristos.Nm tree_srch ,
25*b5677b36Schristos.Nm tree_add ,
26*b5677b36Schristos.Nm tree_delete ,
27*b5677b36Schristos.Nm tree_trav
28*b5677b36Schristos.Nd balanced binary tree routines
29*b5677b36Schristos.Sh SYNOPSIS
30*b5677b36Schristos.Ft void
31*b5677b36Schristos.Fn tree_init "void **tree"
32*b5677b36Schristos.Ft void *
33*b5677b36Schristos.Fn tree_srch "void **tree" "int (*compare)()" "void *data"
34*b5677b36Schristos.Ft void
35*b5677b36Schristos.Fn tree_add "void **tree" "int (*compare)()" \
36*b5677b36Schristos"void *data" "void (*del_uar)()"
37*b5677b36Schristos.Ft int
38*b5677b36Schristos.Fn tree_delete "void **tree" "int (*compare)()" \
39*b5677b36Schristos"void *data" "void (*del_uar)()"
40*b5677b36Schristos.Ft int
41*b5677b36Schristos.Fn tree_trav "void **tree" "int (*trav_uar)()"
42*b5677b36Schristos.Ft void
43*b5677b36Schristos.Fn tree_mung "void **tree" "void (*del_uar)()"
44*b5677b36Schristos.Sh DESCRIPTION
45*b5677b36SchristosThese functions create and manipulate a balanced binary (AVL) tree.  Each node
46*b5677b36Schristosof the tree contains the expected left & right subtree pointers, a short int
47*b5677b36Schristosbalance indicator, and a pointer to the user data.  On a 32 bit system, this
48*b5677b36Schristosmeans an overhead of 4+4+2+4 bytes per node (or, on a RISC or otherwise
49*b5677b36Schristosalignment constrained system with implied padding, 4+4+4+4 bytes per node).
50*b5677b36SchristosThere is no key data type enforced by this package; a caller supplied
51*b5677b36Schristoscompare routine is used to compare user data blocks.
52*b5677b36Schristos.Pp
53*b5677b36SchristosBalanced binary trees are very fast on searches and replacements, but have a
54*b5677b36Schristosmoderately high cost for additions and deletions.  If your application does a
55*b5677b36Schristoslot more searches and replacements than it does additions and deletions, the
56*b5677b36Schristosbalanced (AVL) binary tree is a good choice for a data structure.
57*b5677b36Schristos.Pp
58*b5677b36Schristos.Fn Tree_init
59*b5677b36Schristoscreates an empty tree and binds it to
60*b5677b36Schristos.Dq Fa tree
61*b5677b36Schristos(which for this and all other routines in this package should be declared as
62*b5677b36Schristosa pointer to void or int, and passed by reference), which can then be used by
63*b5677b36Schristosother routines in this package.  Note that more than one
64*b5677b36Schristos.Dq Fa tree
65*b5677b36Schristosvariable can exist at once; thus multiple trees can be manipulated
66*b5677b36Schristossimultaneously.
67*b5677b36Schristos.Pp
68*b5677b36Schristos.Fn Tree_srch
69*b5677b36Schristossearches a tree for a specific node and returns either
70*b5677b36Schristos.Fa NULL
71*b5677b36Schristosif no node was found, or the value of the user data pointer if the node
72*b5677b36Schristoswas found.
73*b5677b36Schristos.Fn compare
74*b5677b36Schristosis the address of a function to compare two user data blocks.  This routine
75*b5677b36Schristosshould work much the way
76*b5677b36Schristos.Xr strcmp 3
77*b5677b36Schristosdoes; in fact,
78*b5677b36Schristos.Xr strcmp
79*b5677b36Schristoscould be used if the user data was a \s-2NUL\s+2 terminated string.
80*b5677b36Schristos.Dq Fa Data
81*b5677b36Schristosis the address of a user data block to be used by
82*b5677b36Schristos.Fn compare
83*b5677b36Schristosas the search criteria.  The tree is searched for a node where
84*b5677b36Schristos.Fn compare
85*b5677b36Schristosreturns 0.
86*b5677b36Schristos.Pp
87*b5677b36Schristos.Fn Tree_add
88*b5677b36Schristosinserts or replaces a node in the specified tree.  The tree specified by
89*b5677b36Schristos.Dq Fa tree
90*b5677b36Schristosis searched as in
91*b5677b36Schristos.Fn tree_srch ,
92*b5677b36Schristosand if a node is found to match
93*b5677b36Schristos.Dq Fa data ,
94*b5677b36Schristosthen the
95*b5677b36Schristos.Fn del_uar
96*b5677b36Schristosfunction, if non\-\s-2NULL\s+2, is called with the address of the user data
97*b5677b36Schristosblock for the node (this routine should deallocate any dynamic memory which
98*b5677b36Schristosis referenced exclusively by the node); the user data pointer for the node
99*b5677b36Schristosis then replaced by the value of
100*b5677b36Schristos.Dq Fa data .
101*b5677b36SchristosIf no node is found to match, a new node is added (which may or may not
102*b5677b36Schristoscause a transparent rebalance operation), with a user data pointer equal to
103*b5677b36Schristos.Dq Fa data .
104*b5677b36SchristosA rebalance may or may not occur, depending on where the node is added
105*b5677b36Schristosand what the rest of the tree looks like.
106*b5677b36Schristos.Fn Tree_add
107*b5677b36Schristoswill return the
108*b5677b36Schristos.Dq Fa data
109*b5677b36Schristospointer unless catastrophe occurs in which case it will return \s-2NULL\s+2.
110*b5677b36Schristos.Pp
111*b5677b36Schristos.Fn Tree_delete
112*b5677b36Schristosdeletes a node from
113*b5677b36Schristos.Dq Fa tree .
114*b5677b36SchristosA rebalance may or may not occur, depending on where the node is removed from
115*b5677b36Schristosand what the rest of the tree looks like.
116*b5677b36Schristos.Fn Tree_delete
117*b5677b36Schristosreturns TRUE if a node was deleted, FALSE otherwise.
118*b5677b36Schristos.Pp
119*b5677b36Schristos.Fn Tree_trav
120*b5677b36Schristostraverses all of
121*b5677b36Schristos.Dq Fa tree ,
122*b5677b36Schristoscalling
123*b5677b36Schristos.Fn trav_uar
124*b5677b36Schristoswith the address of each user data block.  If
125*b5677b36Schristos.Fn trav_uar
126*b5677b36Schristosreturns FALSE at any time,
127*b5677b36Schristos.Fn tree_trav
128*b5677b36Schristoswill immediately return FALSE to its caller.  Otherwise all nodes will be
129*b5677b36Schristosreached and
130*b5677b36Schristos.Fn tree_trav
131*b5677b36Schristoswill return TRUE.
132*b5677b36Schristos.Pp
133*b5677b36Schristos.Fn Tree_mung
134*b5677b36Schristosdeletes every node in
135*b5677b36Schristos.Dq Fa tree ,
136*b5677b36Schristoscalling
137*b5677b36Schristos.Fn del_uar
138*b5677b36Schristos(if it is not \s-2NULL\s+2) with the user data address from each node (see
139*b5677b36Schristos.Fn tree_add
140*b5677b36Schristosand
141*b5677b36Schristos.Fn tree_delete
142*b5677b36Schristosabove).  The tree is left in the same state that
143*b5677b36Schristos.Fn tree_init
144*b5677b36Schristosleaves it in \- i.e., empty.
145*b5677b36Schristos.Sh BUGS
146*b5677b36SchristosShould have a way for the caller to specify application-specific
147*b5677b36Schristos.Xr malloc
148*b5677b36Schristosand
149*b5677b36Schristos.Xr free
150*b5677b36Schristosfunctions to be used internally when allocating meta data.
151*b5677b36Schristos.Sh AUTHOR
152*b5677b36SchristosPaul Vixie, converted and augumented from Modula\-2 examples in
153*b5677b36Schristos.Dq Algorithms & Data Structures ,
154*b5677b36SchristosNiklaus Wirth, Prentice\-Hall, ISBN 0\-13\-022005\-1.
155