(Compatible with UNIX System V C)
#include <search.h> char *tsearch(); ptr = tsearch( key, rootptr, compar);
"tsearch" searches through a binary tree for a particular "key". It uses your "compar" function to compare the key to tree nodes. If the key is found, "tsearch" returns a pointer to it. If the key is not found, "tsearch" creates a new tree node for the key and returns a pointer to this new node.
The "compar" you use does not have to do a byte-by-byte comparison between the key and the tree nodes. For example, each key may be a C structure and "compar" may only look at a particular field when comparing two structures. In this case, the original search key might just be a dummy, with only the significant field filled in.
Each node in the tree is represented by a struct which contains pointers to adjacent tree nodes and to the data stored at the node. This struct is allocated with "malloc". If a new tree node cannot be allocated (e.g. because you are out of space), a NULL pointer will be returned. Note that the tree nodes do not contain the actual information: your program must store the data itself.
Copyright © 1996, Thinkage Ltd.