📄 tsearch.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>tsearch</title></head><body bgcolor=white><center><font size=2>The Single UNIX ® Specification, Version 2<br>Copyright © 1997 The Open Group</font></center><hr size=2 noshade><h4><a name = "tag_000_010_157"> </a>NAME</h4><blockquote>tdelete, tfind, tsearch, twalk - manage a binary search tree</blockquote><h4><a name = "tag_000_010_158"> </a>SYNOPSIS</h4><blockquote><pre><code>#include <<a href="search.h.html">search.h</a>>void *tsearch(const void *<i>key</i>, void **<i>rootp</i>, int (*<i>compar</i>)(const void *, const void *));void *tfind(const void *<i>key</i>, void *const *<i>rootp</i>, int(*<i>compar</i>)(const void *, const void *));void *tdelete(const void *<i>key</i>, void **<i>rootp</i>, int(*<i>compar</i>)(const void *, const void *));void twalk(const void *<i>root</i>, void (*<i>action</i>)(const void *, VISIT, int));</code></pre></blockquote><h4><a name = "tag_000_010_159"> </a>DESCRIPTION</h4><blockquote>The<i>tsearch()</i>,<i><a href="tfind.html">tfind()</a></i>,<i><a href="tdelete.html">tdelete()</a></i>and<i><a href="twalk.html">twalk()</a></i>functions manipulate binary search trees. Comparisons are made with auser-supplied routine, the address of which is passed as the<i>compar</i>argument. This routine is called with two arguments, the pointers to theelements being compared. The user-supplied routine must return an integerless than, equal to or greater than 0, according to whether the first argumentis to be considered less than, equal to or greater than the second argument.The comparison function need not compare every byte, so arbitrary data may becontained in the elements in addition to the values being compared.<p>The<i>tsearch()</i>function is used to build and access the tree. The<i>key</i>argument is a pointer to an element to be accessed or stored. If there is anode in the tree whose element is equal to the value pointed to by <i>key</i>,a pointer to this found node is returned. Otherwise, the value pointed to by<i>key</i>is inserted (that is, a new node is created and the value of<i>key</i>is copied to this node), and a pointer to this node returned. Only pointersare copied, so the calling routine must store the data. The<i>rootp</i>argument points to a variable that points to the root node of the tree. Anull pointer value for the variable pointed to by<i>rootp</i>denotes an empty tree; in this case, the variable will be set to point to thenode which will be at the root of the new tree.<p>Like<i>tsearch()</i>,<i><a href="tfind.html">tfind()</a></i>will search for a node in the tree, returning a pointer to it if found.However, if it is not found,<i><a href="tfind.html">tfind()</a></i>will return a null pointer. The arguments for<i><a href="tfind.html">tfind()</a></i>are the same as for<i>tsearch()</i>.<p>The<i><a href="tdelete.html">tdelete()</a></i>function deletes a node from a binary search tree. The arguments are the sameas for<i>tsearch()</i>.The variable pointed to by<i>rootp</i>will be changed if the deleted node was the root of the tree. The<i><a href="tdelete.html">tdelete()</a></i>function returns a pointer to the parent of the deleted node,or a null pointer if the node is not found.<p>The<i><a href="twalk.html">twalk()</a></i>function traverses a binary search tree. The<i>root</i>argument is a pointer to the root node of the tree to be traversed. (Any nodein a tree may be used as the root for a walk below that node.) The argument<i>action</i>is the name of a routine to be invoked at each node. This routine is, inturn, called with three arguments. The first argument is the address of thenode being visited. The structure pointed to by this argument is unspecifiedand must not be modified by the application, but it is guaranteed that apointer-to-node can be converted to pointer-to-pointer-to-element to accessthe element stored in the node. The second argument is a value from anenumeration data type:<pre><code>typedef enum { preorder, postorder, endorder, leaf } VISIT;</code></pre><p>(defined in<i><a href="search.h.html"><search.h></a></i>),depending on whether this is the first, second or third time that the node isvisited (during a depth-first, left-to-right traversal of the tree), orwhether the node is a leaf. The third argument is the level of the node inthe tree, with the root being level 0.<p>If the calling function alters the pointer to the root, the result isundefined.</blockquote><h4><a name = "tag_000_010_160"> </a>RETURN VALUE</h4><blockquote>If the node is found, both<i>tsearch()</i>and<i><a href="tfind.html">tfind()</a></i>return a pointer to it. If not,<i><a href="tfind.html">tfind()</a></i>returns a null pointer, and<i>tsearch()</i>returns a pointer to the inserted item.<p>A null pointer is returned by<i>tsearch()</i>if there is not enough space available to create a new node.<p>A null pointer is returned by<i>tsearch()</i>,<i><a href="tfind.html">tfind()</a></i>and<i><a href="tdelete.html">tdelete()</a></i>if<i>rootp</i>is a null pointer on entry.<p>The<i><a href="tdelete.html">tdelete()</a></i>function returns a pointer to the parent of the deleted node, or a nullpointer if the node is not found.<p>The<i><a href="twalk.html">twalk()</a></i>function returns no value.</blockquote><h4><a name = "tag_000_010_161"> </a>ERRORS</h4><blockquote>No errors are defined.</blockquote><h4><a name = "tag_000_010_162"> </a>EXAMPLES</h4><blockquote>The following code reads in strings and stores structures containing a pointerto each string and a count of its length. It then walks the tree, printingout the stored strings and their lengths in alphabetical order.<pre><code>#include <search.h>#include <string.h>#include <stdio.h>#define STRSZ 10000#define NODSZ 500struct node { /* pointers to these are stored in the tree */ char *string; int length;};char string_space[STRSZ]; /* space to store strings */struct node nodes[NODSZ]; /* nodes to store */void *root = NULL; /* this points to the root */int main(int argc, char *argv[]){ char *strptr = string_space; struct node *nodeptr = nodes; void print_node(const void *, VISIT, int); int i = 0, node_compare(const void *, const void *); while (gets(strptr) != NULL && i++ < NODSZ) { /* set node */ nodeptr->string = strptr; nodeptr->length = strlen(strptr); /* put node into the tree */ (void) tsearch((void *)nodeptr, (void **)&root, node_compare); /* adjust pointers, so we do not overwrite tree */ strptr += nodeptr->length + 1; nodeptr++; } twalk(root, print_node); return 0;}/* * This routine compares two nodes, based on an * alphabetical ordering of the string field. */intnode_compare(const void *node1, const void *node2){ return strcmp(((const struct node *) node1)->string, ((const struct node *) node2)->string);}/* * This routine prints out a node, the second time * twalk encounters it or if it is a leaf. */voidprint_node(const void *ptr, VISIT order, int level){ const struct node *p = *(const struct node **) ptr; if (order == postorder \(or order == leaf) { (void) printf("string = %s, length = %d\n", p->string, p->length); }}</code></pre></blockquote><h4><a name = "tag_000_010_163"> </a>APPLICATION USAGE</h4><blockquote>The<i>root</i>argument to<i><a href="twalk.html">twalk()</a></i>is one level of indirection less than the<i>rootp</i>arguments to<i>tsearch()</i>and<i><a href="tdelete.html">tdelete()</a></i>.<p>There are two nomenclatures used to refer to the order in which tree nodes arevisited. The<i>tsearch()</i>functionuses <b>preorder</b>, <b>postorder</b> and <b>endorder</b>to refer respectively tovisiting a node before any of its children, after its left childand before its right, and after both its children.The alternative nomenclature uses <b>preorder</b>, <b>inorder</b> and<b>postorder</b> torefer to the same visits, which could result in some confusion overthe meaning of <b>postorder</b>.</blockquote><h4><a name = "tag_000_010_164"> </a>FUTURE DIRECTIONS</h4><blockquote>None.</blockquote><h4><a name = "tag_000_010_165"> </a>SEE ALSO</h4><blockquote><i><a href="bsearch.html">bsearch()</a></i>,<i><a href="hsearch.html">hsearch()</a></i>,<i><a href="lsearch.html">lsearch()</a></i>,<i><a href="search.h.html"><search.h></a></i>.</blockquote><h4>DERIVATION</h4><blockquote>Derived from Issue 1 of the SVID.</blockquote><hr size=2 noshade><center><font size=2>UNIX ® is a registered Trademark of The Open Group.<br>Copyright © 1997 The Open Group<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]</font></center><hr size=2 noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -