10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /* Copyright (c) 1984 AT&T */
230Sstevel@tonic-gate /* All Rights Reserved */
240Sstevel@tonic-gate
25*722Smuffin #pragma ident "%Z%%M% %I% %E% SMI"
260Sstevel@tonic-gate
270Sstevel@tonic-gate /*LINTLIBRARY*/
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * Tree search algorithm, generalized from Knuth (6.2.2) Algorithm T.
300Sstevel@tonic-gate *
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * The NODE * arguments are declared in the lint files as char *,
330Sstevel@tonic-gate * because the definition of NODE isn't available to the user.
340Sstevel@tonic-gate */
350Sstevel@tonic-gate
360Sstevel@tonic-gate #include <search.h>
37*722Smuffin #include <stdio.h>
38*722Smuffin #include <malloc.h>
39*722Smuffin
400Sstevel@tonic-gate typedef char *POINTER;
410Sstevel@tonic-gate typedef struct node { POINTER key; struct node *llink, *rlink; } NODE;
420Sstevel@tonic-gate
43*722Smuffin /*
44*722Smuffin * Find or insert key into search tree
45*722Smuffin *
46*722Smuffin * Arguments
47*722Smuffin * key: Key to be located
48*722Smuffin * rootp: Address of the root of the tree
49*722Smuffin * compar: Comparison function
50*722Smuffin */
510Sstevel@tonic-gate NODE *
tsearch(POINTER key,NODE ** rootp,int (* compar)(POINTER,POINTER))52*722Smuffin tsearch(POINTER key, NODE **rootp, int (*compar)(POINTER, POINTER))
530Sstevel@tonic-gate {
54*722Smuffin NODE *q; /* New node if key not found */
550Sstevel@tonic-gate
560Sstevel@tonic-gate if (rootp == NULL)
570Sstevel@tonic-gate return (NULL);
580Sstevel@tonic-gate while (*rootp != NULL) { /* T1: */
590Sstevel@tonic-gate int r = (*compar)(key, (*rootp)->key); /* T2: */
600Sstevel@tonic-gate if (r == 0)
610Sstevel@tonic-gate return (*rootp); /* Key found */
620Sstevel@tonic-gate rootp = (r < 0) ?
630Sstevel@tonic-gate &(*rootp)->llink : /* T3: Take left branch */
640Sstevel@tonic-gate &(*rootp)->rlink; /* T4: Take right branch */
650Sstevel@tonic-gate }
660Sstevel@tonic-gate q = (NODE *) malloc(sizeof(NODE)); /* T5: Not found */
670Sstevel@tonic-gate if (q != NULL) { /* Allocate new node */
680Sstevel@tonic-gate *rootp = q; /* Link new node to old */
690Sstevel@tonic-gate q->key = key; /* Initialize new node */
700Sstevel@tonic-gate q->llink = q->rlink = NULL;
710Sstevel@tonic-gate }
720Sstevel@tonic-gate return (q);
730Sstevel@tonic-gate }
740Sstevel@tonic-gate
75*722Smuffin /*
76*722Smuffin * Delete node with key key
77*722Smuffin *
78*722Smuffin * Arguments
79*722Smuffin * key: Key to be deleted
80*722Smuffin * rootp: Address of the root of tree
81*722Smuffin * compar: Comparison function
82*722Smuffin */
830Sstevel@tonic-gate NODE *
tdelete(POINTER key,NODE ** rootp,int (* compar)(POINTER,POINTER))84*722Smuffin tdelete(POINTER key, NODE **rootp, int (*compar)(POINTER, POINTER))
850Sstevel@tonic-gate {
860Sstevel@tonic-gate NODE *p; /* Parent of node to be deleted */
87*722Smuffin NODE *q; /* Successor node */
88*722Smuffin NODE *r; /* Right son node */
890Sstevel@tonic-gate int ans; /* Result of comparison */
900Sstevel@tonic-gate
910Sstevel@tonic-gate if (rootp == NULL || (p = *rootp) == NULL)
920Sstevel@tonic-gate return (NULL);
930Sstevel@tonic-gate while ((ans = (*compar)(key, (*rootp)->key)) != 0) {
940Sstevel@tonic-gate p = *rootp;
950Sstevel@tonic-gate rootp = (ans < 0) ?
960Sstevel@tonic-gate &(*rootp)->llink : /* Take left branch */
970Sstevel@tonic-gate &(*rootp)->rlink; /* Take right branch */
980Sstevel@tonic-gate if (*rootp == NULL)
990Sstevel@tonic-gate return (NULL); /* Key not found */
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate r = (*rootp)->rlink; /* D1: */
1020Sstevel@tonic-gate if ((q = (*rootp)->llink) == NULL) /* Llink NULL? */
1030Sstevel@tonic-gate q = r;
1040Sstevel@tonic-gate else if (r != NULL) { /* Rlink NULL? */
1050Sstevel@tonic-gate if (r->llink == NULL) { /* D2: Find successor */
1060Sstevel@tonic-gate r->llink = q;
1070Sstevel@tonic-gate q = r;
1080Sstevel@tonic-gate } else { /* D3: Find NULL link */
1090Sstevel@tonic-gate for (q = r->llink; q->llink != NULL; q = r->llink)
1100Sstevel@tonic-gate r = q;
1110Sstevel@tonic-gate r->llink = q->rlink;
1120Sstevel@tonic-gate q->llink = (*rootp)->llink;
1130Sstevel@tonic-gate q->rlink = (*rootp)->rlink;
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate free((POINTER) *rootp); /* D4: Free node */
1170Sstevel@tonic-gate *rootp = q; /* Link parent to replacement */
1180Sstevel@tonic-gate return (p);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate
121*722Smuffin static void _twalk(NODE *, void (*)(NODE *, VISIT, int), int);
122*722Smuffin
123*722Smuffin /*
124*722Smuffin * Walk the nodes of a tree
125*722Smuffin *
126*722Smuffin * Arguments
127*722Smuffin * root: Root of the tree to be walked
128*722Smuffin * action: Function to be called at each node
129*722Smuffin */
1300Sstevel@tonic-gate void
twalk(NODE * root,void (* action)(NODE *,VISIT,int))131*722Smuffin twalk(NODE *root, void (*action)(NODE *, VISIT, int))
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate if (root != NULL && action != NULL)
1350Sstevel@tonic-gate _twalk(root, action, 0);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate
138*722Smuffin /*
139*722Smuffin * Walk the nodes of a tree
140*722Smuffin *
141*722Smuffin * Arguments
142*722Smuffin * root: Root of the tree to be walked
143*722Smuffin * action: Function to be called at each node
144*722Smuffin */
1450Sstevel@tonic-gate static void
_twalk(NODE * root,void (* action)(NODE *,VISIT,int),int level)146*722Smuffin _twalk(NODE *root, void (*action)(NODE *, VISIT, int), int level)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate if (root->llink == NULL && root->rlink == NULL)
1490Sstevel@tonic-gate (*action)(root, leaf, level);
1500Sstevel@tonic-gate else {
1510Sstevel@tonic-gate (*action)(root, preorder, level);
1520Sstevel@tonic-gate if (root->llink != NULL)
1530Sstevel@tonic-gate _twalk(root->llink, action, level + 1);
1540Sstevel@tonic-gate (*action)(root, postorder, level);
1550Sstevel@tonic-gate if (root->rlink != NULL)
1560Sstevel@tonic-gate _twalk(root->rlink, action, level + 1);
1570Sstevel@tonic-gate (*action)(root, endorder, level);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate }
160