tsearch.3
来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· 3 代码 · 共 186 行
3
186 行
.\" SCCSID: @(#)tsearch.3 8.1 9/11/90.TH tsearch 3 .SH Nametsearch, tfind, tdelete, twalk \- manage binary search trees.SH Syntax.B #include <search.h>.PP.B void *tsearch (\fIkey, rootp, compar\fP).br.B void *\fIkey\fP;.br.B void **\fIrootp\fP;.br.B int (*\fIcompar\fP)\^( )\^;.PP.B void *tfind (\fIkey, rootp, compar\fP).br.B void *\fIkey\fP;.br.B void **\fIrootp\fP;.br.B int (*\fIcompar\fP)\^( )\^;.PP.B void *tdelete (\fIkey, rootp, compar\fP).br.B void *\fIkey\fP;.br.B void **\fIrootp\fP;.br.B int (*\fIcompar\fP)\^( )\^;.PP.B void twalk (\fIroot, action\fP).br.B void * \fIroot\fP;.br.B void (*\fIaction\fP)\^( )\^;.SH Description.NXR "tsearch subroutine".NXR "tfind subroutine".NXR "tdelete subroutine".NXR "twalk subroutine".NXR "binary search routine" "managing tree searches"The.PN tsearchsubroutineis a binary tree search routinegeneralized from Knuth (6.2.2) Algorithm T.It returns a pointer into a tree indicating wherea datum may be found.If the datum does not occur, it is addedat an appropriate point in the tree.The.I key\^points to the datum to be sought in the tree.The.I rootp\^points to a variable that points to the rootof the tree.A NULLpointer value for the variable denotes an empty tree;in this case,the variable will be set to point to the datum at the rootof the new tree. The.I comparis the name of the comparison function.It is called with two arguments that pointto the elements being compared.The function must returnan integer less than, equal to, or greater than zeroaccording as the first argument is to be consideredless than, equal to, or greater than the second..PPLike .PN tsearch ,.PN tfindwill search for a datum in the tree,returning a pointer to it if found. However, if it is not found,.PN tfindwill return a NULL pointer. The arguments for .PN tfindare the same as for .PN tsearch ..PPThe.PN tdeletesubroutine deletes a node from a binary search tree.It is generalized from Knuth (6.2.2)algorithm D.The arguments are the same as for .PN tsearch .The variable pointed to by.I rootp\^will be changed if the deleted node was the root of the tree.The.PN tdeletesubroutine returns a pointer to the parent of the deleted node,or a NULL pointer if the node is not found..PPThe.PN twalksubroutine traverses a binary search tree.The.I rootis the root of the tree to be traversed.(Any node in a tree may be used as the root for a walk below that node.)The.I actionis the name of a routineto be invoked at each node.This routine is, in turn,called with three arguments.The first argument is the address of the node being visited.The second argument is a value from an enumeration data type.I "typedef enum { preorder, postorder, endorder, leaf }"VISIT;(defined in the <search.h> header file),depending on whether this is the first, second or thirdtime that the node has been visited(during a depth-first, left-to-right traversal of the tree),or whether the node is a leaf.The third argument is the level of the nodein the tree, with the root being level zero..NT "Notes"The pointers to the key and the root of the tree should beof type pointer-to-element,and cast to type pointer-to-character..spThe comparison function need not compare every byte,so arbitrary datamay be contained in the elements in addition to the valuesbeing compared..spAlthough declared as type pointer-to-character,the value returned should be cast into type pointer-to-element..spNote that the.I rootargument to .PN twalkis one level of indirection less than the.I rootparguments to.PN tsearchand.PN tdelete ..NE.SH Return ValuesA NULL pointer is returned by .PN tsearchif thereis not enough space available to create a new node..brA NULL pointer is returned by.PN tsearch ,.PN tfind ,and.PN tdeleteif \fIrootp\fP is NULL on entry..brIf the datum is found, both .PN tsearchand.PN tfindreturn a pointer to it. If not, .PN tfindreturns NULL,and .PN tsearchreturns a pointer to the inserted item..SH Restrictions.NXR "tsearch subroutine" "restrictions"Results are unpredictable if the callingfunction alters the pointer to the root..SH Diagnostics.NXR "tsearch subroutine" "diagnostics"A NULLpointer is returned by.PN tsearchand.PN tdeleteif.I rootp\^is NULL on entry..SH See Alsobsearch(3), hsearch(3), lsearch(3)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?