1TSEARCH(3) Linux Programmer's Manual TSEARCH(3)
2
3
4
6 tsearch, tfind, tdelete, twalk, tdestroy - manage a binary tree
7
9 #include <search.h>
10
11 void *tsearch(const void *key, void **rootp,
12 int(*compar)(const void *, const void *));
13
14 void *tfind(const void *key, const void **rootp,
15 int(*compar)(const void *, const void *));
16
17 void *tdelete(const void *key, void **rootp,
18 int(*compar)(const void *, const void *));
19
20 void twalk(const void *root, void(*action)(const void *nodep,
21 const VISIT which,
22 const int depth));
23
24 #define _GNU_SOURCE
25 #include <search.h>
26
27 void tdestroy (void *root, void (*free_node)(void *nodep));
28
30 tsearch(), tfind(), twalk(), and tdelete() manage a binary tree. They
31 are generalized from Knuth (6.2.2) Algorithm T. The first field in
32 each node of the tree is a pointer to the corresponding data item.
33 (The calling program must store the actual data.) compar points to a
34 comparison routine, which takes pointers to two items. It should
35 return an integer which is negative, zero, or positive, depending on
36 whether the first item is less than, equal to, or greater than the sec‐
37 ond.
38
39 tsearch() searches the tree for an item. key points to the item to be
40 searched for. rootp points to a variable which points to the root of
41 the tree. If the tree is empty, then the variable that rootp points to
42 should be set to NULL. If the item is found in the tree, then
43 tsearch() returns a pointer to it. If it is not found, then tsearch()
44 adds it, and returns a pointer to the newly added item.
45
46 tfind() is like tsearch(), except that if the item is not found, then
47 tfind() returns NULL.
48
49 tdelete() deletes an item from the tree. Its arguments are the same as
50 for tsearch().
51
52 twalk() performs depth-first, left-to-right traversal of a binary tree.
53 root points to the starting node for the traversal. If that node is
54 not the root, then only part of the tree will be visited. twalk()
55 calls the user function action each time a node is visited (that is,
56 three times for an internal node, and once for a leaf). action, in
57 turn, takes three arguments. The first is a pointer to the node being
58 visited. The second is an integer which takes on the values preorder,
59 postorder, and endorder depending on whether this is the first, second,
60 or third visit to the internal node, or leaf if it is the single visit
61 to a leaf node. (These symbols are defined in <search.h>.) The third
62 argument is the depth of the node, with zero being the root.
63
64 (More commonly, preorder, postorder, and endorder are known as pre‐
65 order, inorder, and postorder: before visiting the children, after the
66 first and before the second, and after visiting the children. Thus, the
67 choice of name postorder is rather confusing.)
68
69 tdestroy() removes the whole tree pointed to by root, freeing all
70 resources allocated by the tsearch() function. For the data in each
71 tree node the function free_node is called. The pointer to the data is
72 passed as the argument to the function. If no such work is necessary
73 free_node must point to a function doing nothing.
74
76 tsearch() returns a pointer to a matching item in the tree, or to the
77 newly added item, or NULL if there was insufficient memory to add the
78 item. tfind() returns a pointer to the item, or NULL if no match is
79 found. If there are multiple elements that match the key, the element
80 returned is unspecified.
81
82 tdelete() returns a pointer to the parent of the item deleted, or NULL
83 if the item was not found.
84
85 tsearch(), tfind(), and tdelete() also return NULL if rootp was NULL on
86 entry.
87
89 twalk() takes a pointer to the root, while the other functions take a
90 pointer to a variable which points to the root.
91
92 twalk() uses postorder to mean "after the left subtree, but before the
93 right subtree". Some authorities would call this "inorder", and
94 reserve "postorder" to mean "after both subtrees".
95
96 tdelete() frees the memory required for the node in the tree. The user
97 is responsible for freeing the memory for the corresponding data.
98
99 The example program depends on the fact that twalk() makes no further
100 reference to a node after calling the user function with argument
101 "endorder" or "leaf". This works with the GNU library implementation,
102 but is not in the SysV documentation.
103
105 The following program inserts twelve random numbers into a binary tree,
106 where duplicate numbers are collapsed, then prints the numbers in
107 order.
108
109 #include <search.h>
110 #include <stdlib.h>
111 #include <stdio.h>
112 #include <time.h>
113
114 void *root = NULL;
115
116 void *xmalloc(unsigned n) {
117 void *p;
118 p = malloc(n);
119 if (p) return p;
120 fprintf(stderr, "insufficient memory\n");
121 exit(1);
122 }
123
124 int compare(const void *pa, const void *pb) {
125 if (*(int *)pa < *(int *)pb) return -1;
126 if (*(int *)pa > *(int *)pb) return 1;
127 return 0;
128 }
129
130 void action(const void *nodep, const VISIT which, const int depth) {
131 int *datap;
132
133 switch(which) {
134 case preorder:
135 break;
136 case postorder:
137 datap = *(int **)nodep;
138 printf("%6d\n", *datap);
139 break;
140 case endorder:
141 break;
142 case leaf:
143 datap = *(int **)nodep;
144 printf("%6d\n", *datap);
145 break;
146 }
147 }
148
149 int main() {
150 int i, *ptr;
151 void *val;
152
153 srand(time(NULL));
154 for (i = 0; i < 12; i++) {
155 ptr = (int *)xmalloc(sizeof(int));
156 *ptr = rand()&0xff;
157 val = tsearch((void *)ptr, &root, compare);
158 if (val == NULL) exit(1);
159 }
160 twalk(root, action);
161 return 0;
162 }
163
165 SVr4, POSIX.1-2001. The function tdestroy() is a GNU extension.
166
168 bsearch(3), hsearch(3), lsearch(3), qsort(3), feature_test_macros(7)
169
170
171
172GNU 1995-09-24 TSEARCH(3)