Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ tsearch(3C) — GL2 W2.3

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

bsearch(3C)

hsearch(3C)

lsearch(3C)

TSEARCH(3C)  —  Silicon Graphics

NAME

tsearch, tdelete, twalk − manage binary search trees

SYNOPSIS

#include <search.h>

char ∗tsearch ((char ∗) key, (char ∗∗) rootp, compar)
int (∗compar)( );

char ∗tdelete ((char ∗) key, (char ∗∗) rootp, compar)
int (∗compar)( );

void twalk ((char ∗) root, action)
void (∗action)( );

DESCRIPTION

Tsearch is a binary tree search routine generalized from Knuth (6.2.2) Algorithm T.  It returns a pointer into a tree indicating where a datum may be found.  If the datum does not occur, it is added at an appropriate point in the tree.  Key points to the datum to be sought in the tree.  Rootp points to a variable that points to the root of the tree.  A NULL pointer value for the variable denotes an empty tree; in this case, the variable will be set to point to the datum at the root of the new tree.  Compar is the name of the comparison function.  It is called with two arguments that point to the elements being compared.  The function must return an integer less than, equal to, or greater than zero according as the first argument is to be considered less than, equal to, or greater than the second. 

Tdelete 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 tsearch. The variable pointed to by rootp will be changed if the deleted node was the root of the tree.  Tdelete returns a pointer to the parent of the deleted node, or a NULL pointer if the node is not found. 

Twalk traverses a binary search tree.  Root is 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.)  Action is the name of a routine to 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 typedef enum { preorder, postorder, endorder, leaf } VISIT; (defined in the <search.h> header file), depending on whether this is the first, second or third time 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 node in the tree, with the root being level zero. 

NOTES

The pointers to the key and the root of the tree should be of type pointer-to-element, and cast to type pointer-to-character.  The comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to the values being compared.  Although declared as type pointer-to-character, the value returned should be cast into type pointer-to-element. 
Warning: the root argument to twalk is one level of indirection less than the rootp arguments to tsearch and tdelete.

DIAGNOSTICS

A NULL pointer is returned by tsearch if there is not enough space available to create a new node. 
A NULL pointer is returned by tsearch and tdelete if rootp is NULL on entry. 

SEE ALSO

bsearch(3C), hsearch(3C), lsearch(3C). 

BUGS

Awful things can happen if the calling function alters the pointer to the root. 

Version 2.3  —  July 04, 1985

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026