17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* Copyright (c) 1984 AT&T */ 237c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 247c478bd9Sstevel@tonic-gate 25*5d54f3d8Smuffin #pragma ident "%Z%%M% %I% %E% SMI" 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/ 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * Tree search algorithm, generalized from Knuth (6.2.2) Algorithm T. 307c478bd9Sstevel@tonic-gate * 317c478bd9Sstevel@tonic-gate * 327c478bd9Sstevel@tonic-gate * The NODE * arguments are declared in the lint files as char *, 337c478bd9Sstevel@tonic-gate * because the definition of NODE isn't available to the user. 347c478bd9Sstevel@tonic-gate */ 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include <search.h> 37*5d54f3d8Smuffin #include <stdio.h> 38*5d54f3d8Smuffin #include <malloc.h> 39*5d54f3d8Smuffin 407c478bd9Sstevel@tonic-gate typedef char *POINTER; 417c478bd9Sstevel@tonic-gate typedef struct node { POINTER key; struct node *llink, *rlink; } NODE; 427c478bd9Sstevel@tonic-gate 43*5d54f3d8Smuffin /* 44*5d54f3d8Smuffin * Find or insert key into search tree 45*5d54f3d8Smuffin * 46*5d54f3d8Smuffin * Arguments 47*5d54f3d8Smuffin * key: Key to be located 48*5d54f3d8Smuffin * rootp: Address of the root of the tree 49*5d54f3d8Smuffin * compar: Comparison function 50*5d54f3d8Smuffin */ 517c478bd9Sstevel@tonic-gate NODE * 52*5d54f3d8Smuffin tsearch(POINTER key, NODE **rootp, int (*compar)(POINTER, POINTER)) 537c478bd9Sstevel@tonic-gate { 54*5d54f3d8Smuffin NODE *q; /* New node if key not found */ 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate if (rootp == NULL) 577c478bd9Sstevel@tonic-gate return (NULL); 587c478bd9Sstevel@tonic-gate while (*rootp != NULL) { /* T1: */ 597c478bd9Sstevel@tonic-gate int r = (*compar)(key, (*rootp)->key); /* T2: */ 607c478bd9Sstevel@tonic-gate if (r == 0) 617c478bd9Sstevel@tonic-gate return (*rootp); /* Key found */ 627c478bd9Sstevel@tonic-gate rootp = (r < 0) ? 637c478bd9Sstevel@tonic-gate &(*rootp)->llink : /* T3: Take left branch */ 647c478bd9Sstevel@tonic-gate &(*rootp)->rlink; /* T4: Take right branch */ 657c478bd9Sstevel@tonic-gate } 667c478bd9Sstevel@tonic-gate q = (NODE *) malloc(sizeof(NODE)); /* T5: Not found */ 677c478bd9Sstevel@tonic-gate if (q != NULL) { /* Allocate new node */ 687c478bd9Sstevel@tonic-gate *rootp = q; /* Link new node to old */ 697c478bd9Sstevel@tonic-gate q->key = key; /* Initialize new node */ 707c478bd9Sstevel@tonic-gate q->llink = q->rlink = NULL; 717c478bd9Sstevel@tonic-gate } 727c478bd9Sstevel@tonic-gate return (q); 737c478bd9Sstevel@tonic-gate } 747c478bd9Sstevel@tonic-gate 75*5d54f3d8Smuffin /* 76*5d54f3d8Smuffin * Delete node with key key 77*5d54f3d8Smuffin * 78*5d54f3d8Smuffin * Arguments 79*5d54f3d8Smuffin * key: Key to be deleted 80*5d54f3d8Smuffin * rootp: Address of the root of tree 81*5d54f3d8Smuffin * compar: Comparison function 82*5d54f3d8Smuffin */ 837c478bd9Sstevel@tonic-gate NODE * 84*5d54f3d8Smuffin tdelete(POINTER key, NODE **rootp, int (*compar)(POINTER, POINTER)) 857c478bd9Sstevel@tonic-gate { 867c478bd9Sstevel@tonic-gate NODE *p; /* Parent of node to be deleted */ 87*5d54f3d8Smuffin NODE *q; /* Successor node */ 88*5d54f3d8Smuffin NODE *r; /* Right son node */ 897c478bd9Sstevel@tonic-gate int ans; /* Result of comparison */ 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate if (rootp == NULL || (p = *rootp) == NULL) 927c478bd9Sstevel@tonic-gate return (NULL); 937c478bd9Sstevel@tonic-gate while ((ans = (*compar)(key, (*rootp)->key)) != 0) { 947c478bd9Sstevel@tonic-gate p = *rootp; 957c478bd9Sstevel@tonic-gate rootp = (ans < 0) ? 967c478bd9Sstevel@tonic-gate &(*rootp)->llink : /* Take left branch */ 977c478bd9Sstevel@tonic-gate &(*rootp)->rlink; /* Take right branch */ 987c478bd9Sstevel@tonic-gate if (*rootp == NULL) 997c478bd9Sstevel@tonic-gate return (NULL); /* Key not found */ 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate r = (*rootp)->rlink; /* D1: */ 1027c478bd9Sstevel@tonic-gate if ((q = (*rootp)->llink) == NULL) /* Llink NULL? */ 1037c478bd9Sstevel@tonic-gate q = r; 1047c478bd9Sstevel@tonic-gate else if (r != NULL) { /* Rlink NULL? */ 1057c478bd9Sstevel@tonic-gate if (r->llink == NULL) { /* D2: Find successor */ 1067c478bd9Sstevel@tonic-gate r->llink = q; 1077c478bd9Sstevel@tonic-gate q = r; 1087c478bd9Sstevel@tonic-gate } else { /* D3: Find NULL link */ 1097c478bd9Sstevel@tonic-gate for (q = r->llink; q->llink != NULL; q = r->llink) 1107c478bd9Sstevel@tonic-gate r = q; 1117c478bd9Sstevel@tonic-gate r->llink = q->rlink; 1127c478bd9Sstevel@tonic-gate q->llink = (*rootp)->llink; 1137c478bd9Sstevel@tonic-gate q->rlink = (*rootp)->rlink; 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate free((POINTER) *rootp); /* D4: Free node */ 1177c478bd9Sstevel@tonic-gate *rootp = q; /* Link parent to replacement */ 1187c478bd9Sstevel@tonic-gate return (p); 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate 121*5d54f3d8Smuffin static void _twalk(NODE *, void (*)(NODE *, VISIT, int), int); 122*5d54f3d8Smuffin 123*5d54f3d8Smuffin /* 124*5d54f3d8Smuffin * Walk the nodes of a tree 125*5d54f3d8Smuffin * 126*5d54f3d8Smuffin * Arguments 127*5d54f3d8Smuffin * root: Root of the tree to be walked 128*5d54f3d8Smuffin * action: Function to be called at each node 129*5d54f3d8Smuffin */ 1307c478bd9Sstevel@tonic-gate void 131*5d54f3d8Smuffin twalk(NODE *root, void (*action)(NODE *, VISIT, int)) 1327c478bd9Sstevel@tonic-gate { 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate if (root != NULL && action != NULL) 1357c478bd9Sstevel@tonic-gate _twalk(root, action, 0); 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate 138*5d54f3d8Smuffin /* 139*5d54f3d8Smuffin * Walk the nodes of a tree 140*5d54f3d8Smuffin * 141*5d54f3d8Smuffin * Arguments 142*5d54f3d8Smuffin * root: Root of the tree to be walked 143*5d54f3d8Smuffin * action: Function to be called at each node 144*5d54f3d8Smuffin */ 1457c478bd9Sstevel@tonic-gate static void 146*5d54f3d8Smuffin _twalk(NODE *root, void (*action)(NODE *, VISIT, int), int level) 1477c478bd9Sstevel@tonic-gate { 1487c478bd9Sstevel@tonic-gate if (root->llink == NULL && root->rlink == NULL) 1497c478bd9Sstevel@tonic-gate (*action)(root, leaf, level); 1507c478bd9Sstevel@tonic-gate else { 1517c478bd9Sstevel@tonic-gate (*action)(root, preorder, level); 1527c478bd9Sstevel@tonic-gate if (root->llink != NULL) 1537c478bd9Sstevel@tonic-gate _twalk(root->llink, action, level + 1); 1547c478bd9Sstevel@tonic-gate (*action)(root, postorder, level); 1557c478bd9Sstevel@tonic-gate if (root->rlink != NULL) 1567c478bd9Sstevel@tonic-gate _twalk(root->rlink, action, level + 1); 1577c478bd9Sstevel@tonic-gate (*action)(root, endorder, level); 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate } 160