avl.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. /* avl.c - routines to implement an avl tree */
  39. /*
  40. * Copyright (c) 1993 Regents of the University of Michigan.
  41. * All rights reserved.
  42. *
  43. * Redistribution and use in source and binary forms are permitted
  44. * provided that this notice is preserved and that due credit is given
  45. * to the University of Michigan at Ann Arbor. The name of the University
  46. * may not be used to endorse or promote products derived from this
  47. * software without specific prior written permission. This software
  48. * is provided ``as is'' without express or implied warranty.
  49. */
  50. #if 0
  51. static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
  52. static char avl_version[] = "AVL library version 1.0\n";
  53. #endif
  54. #ifdef _WIN32
  55. typedef char *caddr_t;
  56. #define WIN32_LEAN_AND_MEAN
  57. #include <windows.h>
  58. #endif
  59. #include <sys/types.h>
  60. #include <stdlib.h>
  61. #include <stdio.h>
  62. #include "avl.h"
  63. #define ROTATERIGHT(x) { \
  64. Avlnode *tmp;\
  65. if ( *x == NULL || (*x)->avl_left == NULL ) {\
  66. (void) printf("RR error\n"); exit(1); \
  67. }\
  68. tmp = (*x)->avl_left;\
  69. (*x)->avl_left = tmp->avl_right;\
  70. tmp->avl_right = *x;\
  71. *x = tmp;\
  72. }
  73. #define ROTATELEFT(x) { \
  74. Avlnode *tmp;\
  75. if ( *x == NULL || (*x)->avl_right == NULL ) {\
  76. (void) printf("RL error\n"); exit(1); \
  77. }\
  78. tmp = (*x)->avl_right;\
  79. (*x)->avl_right = tmp->avl_left;\
  80. tmp->avl_left = *x;\
  81. *x = tmp;\
  82. }
  83. /*
  84. * ravl_insert - called from avl_insert() to do a recursive insert into
  85. * and balance of an avl tree.
  86. */
  87. static int
  88. ravl_insert(
  89. Avlnode **iroot,
  90. caddr_t data,
  91. int *taller,
  92. IFP fcmp, /* comparison function */
  93. IFP fdup, /* function to call for duplicates */
  94. int depth
  95. )
  96. {
  97. int rc, cmp, tallersub;
  98. Avlnode *l, *r;
  99. if ( *iroot == 0 ) {
  100. if ( (*iroot = (Avlnode *) malloc( sizeof( Avlnode ) ))
  101. == NULL ) {
  102. return( -1 );
  103. }
  104. (*iroot)->avl_left = 0;
  105. (*iroot)->avl_right = 0;
  106. (*iroot)->avl_bf = 0;
  107. (*iroot)->avl_data = data;
  108. *taller = 1;
  109. return( 0 );
  110. }
  111. cmp = (*fcmp)( data, (*iroot)->avl_data );
  112. /* equal - duplicate name */
  113. if ( cmp == 0 ) {
  114. *taller = 0;
  115. return( (*fdup)( (*iroot)->avl_data, data ) );
  116. }
  117. /* go right */
  118. else if ( cmp > 0 ) {
  119. rc = ravl_insert( &((*iroot)->avl_right), data, &tallersub,
  120. fcmp, fdup, depth );
  121. if ( tallersub )
  122. switch ( (*iroot)->avl_bf ) {
  123. case LH : /* left high - balance is restored */
  124. (*iroot)->avl_bf = EH;
  125. *taller = 0;
  126. break;
  127. case EH : /* equal height - now right heavy */
  128. (*iroot)->avl_bf = RH;
  129. *taller = 1;
  130. break;
  131. case RH : /* right heavy to start - right balance */
  132. r = (*iroot)->avl_right;
  133. switch ( r->avl_bf ) {
  134. case LH : /* double rotation left */
  135. l = r->avl_left;
  136. switch ( l->avl_bf ) {
  137. case LH : (*iroot)->avl_bf = EH;
  138. r->avl_bf = RH;
  139. break;
  140. case EH : (*iroot)->avl_bf = EH;
  141. r->avl_bf = EH;
  142. break;
  143. case RH : (*iroot)->avl_bf = LH;
  144. r->avl_bf = EH;
  145. break;
  146. }
  147. l->avl_bf = EH;
  148. ROTATERIGHT( (&r) )
  149. (*iroot)->avl_right = r;
  150. ROTATELEFT( iroot )
  151. *taller = 0;
  152. break;
  153. case EH : /* This should never happen */
  154. break;
  155. case RH : /* single rotation left */
  156. (*iroot)->avl_bf = EH;
  157. r->avl_bf = EH;
  158. ROTATELEFT( iroot )
  159. *taller = 0;
  160. break;
  161. }
  162. break;
  163. }
  164. else
  165. *taller = 0;
  166. }
  167. /* go left */
  168. else {
  169. rc = ravl_insert( &((*iroot)->avl_left), data, &tallersub,
  170. fcmp, fdup, depth );
  171. if ( tallersub )
  172. switch ( (*iroot)->avl_bf ) {
  173. case LH : /* left high to start - left balance */
  174. l = (*iroot)->avl_left;
  175. switch ( l->avl_bf ) {
  176. case LH : /* single rotation right */
  177. (*iroot)->avl_bf = EH;
  178. l->avl_bf = EH;
  179. ROTATERIGHT( iroot )
  180. *taller = 0;
  181. break;
  182. case EH : /* this should never happen */
  183. break;
  184. case RH : /* double rotation right */
  185. r = l->avl_right;
  186. switch ( r->avl_bf ) {
  187. case LH : (*iroot)->avl_bf = RH;
  188. l->avl_bf = EH;
  189. break;
  190. case EH : (*iroot)->avl_bf = EH;
  191. l->avl_bf = EH;
  192. break;
  193. case RH : (*iroot)->avl_bf = EH;
  194. l->avl_bf = LH;
  195. break;
  196. }
  197. r->avl_bf = EH;
  198. ROTATELEFT( (&l) )
  199. (*iroot)->avl_left = l;
  200. ROTATERIGHT( iroot )
  201. *taller = 0;
  202. break;
  203. }
  204. break;
  205. case EH : /* equal height - now left heavy */
  206. (*iroot)->avl_bf = LH;
  207. *taller = 1;
  208. break;
  209. case RH : /* right high - balance is restored */
  210. (*iroot)->avl_bf = EH;
  211. *taller = 0;
  212. break;
  213. }
  214. else
  215. *taller = 0;
  216. }
  217. return( rc );
  218. }
  219. /*
  220. * avl_insert -- insert a node containing data data into the avl tree
  221. * with root root. fcmp is a function to call to compare the data portion
  222. * of two nodes. it should take two arguments and return <, >, or == 0,
  223. * depending on whether its first argument is <, >, or == its second
  224. * argument (like strcmp, e.g.). fdup is a function to call when a duplicate
  225. * node is inserted. it should return 0, or -1 and its return value
  226. * will be the return value from avl_insert in the case of a duplicate node.
  227. * the function will be called with the original node's data as its first
  228. * argument and with the incoming duplicate node's data as its second
  229. * argument. this could be used, for example, to keep a count with each
  230. * node.
  231. *
  232. * NOTE: this routine may malloc memory
  233. */
  234. int
  235. avl_insert(
  236. Avlnode **root,
  237. caddr_t data,
  238. IFP fcmp,
  239. IFP fdup
  240. )
  241. {
  242. int taller;
  243. return( ravl_insert( root, data, &taller, fcmp, fdup, 0 ) );
  244. }
  245. /*
  246. * right_balance() - called from delete when root's right subtree has
  247. * been shortened because of a deletion.
  248. */
  249. static int
  250. right_balance( Avlnode **root )
  251. {
  252. int shorter= 0;
  253. Avlnode *r, *l;
  254. switch( (*root)->avl_bf ) {
  255. case RH: /* was right high - equal now */
  256. (*root)->avl_bf = EH;
  257. shorter = 1;
  258. break;
  259. case EH: /* was equal - left high now */
  260. (*root)->avl_bf = LH;
  261. shorter = 0;
  262. break;
  263. case LH: /* was right high - balance */
  264. l = (*root)->avl_left;
  265. switch ( l->avl_bf ) {
  266. case RH : /* double rotation left */
  267. r = l->avl_right;
  268. switch ( r->avl_bf ) {
  269. case RH :
  270. (*root)->avl_bf = EH;
  271. l->avl_bf = LH;
  272. break;
  273. case EH :
  274. (*root)->avl_bf = EH;
  275. l->avl_bf = EH;
  276. break;
  277. case LH :
  278. (*root)->avl_bf = RH;
  279. l->avl_bf = EH;
  280. break;
  281. }
  282. r->avl_bf = EH;
  283. ROTATELEFT( (&l) )
  284. (*root)->avl_left = l;
  285. ROTATERIGHT( root )
  286. shorter = 1;
  287. break;
  288. case EH : /* right rotation */
  289. (*root)->avl_bf = LH;
  290. l->avl_bf = RH;
  291. ROTATERIGHT( root );
  292. shorter = 0;
  293. break;
  294. case LH : /* single rotation right */
  295. (*root)->avl_bf = EH;
  296. l->avl_bf = EH;
  297. ROTATERIGHT( root )
  298. shorter = 1;
  299. break;
  300. }
  301. break;
  302. }
  303. return( shorter );
  304. }
  305. /*
  306. * left_balance() - called from delete when root's left subtree has
  307. * been shortened because of a deletion.
  308. */
  309. static int
  310. left_balance( Avlnode **root )
  311. {
  312. int shorter= 0;
  313. Avlnode *r, *l;
  314. switch( (*root)->avl_bf ) {
  315. case LH: /* was left high - equal now */
  316. (*root)->avl_bf = EH;
  317. shorter = 1;
  318. break;
  319. case EH: /* was equal - right high now */
  320. (*root)->avl_bf = RH;
  321. shorter = 0;
  322. break;
  323. case RH: /* was right high - balance */
  324. r = (*root)->avl_right;
  325. switch ( r->avl_bf ) {
  326. case LH : /* double rotation left */
  327. l = r->avl_left;
  328. switch ( l->avl_bf ) {
  329. case LH :
  330. (*root)->avl_bf = EH;
  331. r->avl_bf = RH;
  332. break;
  333. case EH :
  334. (*root)->avl_bf = EH;
  335. r->avl_bf = EH;
  336. break;
  337. case RH :
  338. (*root)->avl_bf = LH;
  339. r->avl_bf = EH;
  340. break;
  341. }
  342. l->avl_bf = EH;
  343. ROTATERIGHT( (&r) )
  344. (*root)->avl_right = r;
  345. ROTATELEFT( root )
  346. shorter = 1;
  347. break;
  348. case EH : /* single rotation left */
  349. (*root)->avl_bf = RH;
  350. r->avl_bf = LH;
  351. ROTATELEFT( root );
  352. shorter = 0;
  353. break;
  354. case RH : /* single rotation left */
  355. (*root)->avl_bf = EH;
  356. r->avl_bf = EH;
  357. ROTATELEFT( root )
  358. shorter = 1;
  359. break;
  360. }
  361. break;
  362. }
  363. return( shorter );
  364. }
  365. /*
  366. * ravl_delete() - called from avl_delete to do recursive deletion of a
  367. * node from an avl tree. It finds the node recursively, deletes it,
  368. * and returns shorter if the tree is shorter after the deletion and
  369. * rebalancing.
  370. */
  371. static caddr_t
  372. ravl_delete(
  373. Avlnode **root,
  374. caddr_t data,
  375. IFP fcmp,
  376. int *shorter
  377. )
  378. {
  379. int shortersubtree = 0;
  380. int cmp;
  381. caddr_t savedata;
  382. Avlnode *minnode, *savenode;
  383. if ( *root == NULLAVL )
  384. return( 0 );
  385. cmp = (*fcmp)( data, (*root)->avl_data );
  386. /* found it! */
  387. if ( cmp == 0 ) {
  388. savenode = *root;
  389. savedata = savenode->avl_data;
  390. /* simple cases: no left child */
  391. if ( (*root)->avl_left == 0 ) {
  392. *root = (*root)->avl_right;
  393. *shorter = 1;
  394. free( (char *) savenode );
  395. return( savedata );
  396. /* no right child */
  397. } else if ( (*root)->avl_right == 0 ) {
  398. *root = (*root)->avl_left;
  399. *shorter = 1;
  400. free( (char *) savenode );
  401. return( savedata );
  402. }
  403. /*
  404. * avl_getmin will return to us the smallest node greater
  405. * than the one we are trying to delete. deleting this node
  406. * from the right subtree is guaranteed to end in one of the
  407. * simple cases above.
  408. */
  409. minnode = (*root)->avl_right;
  410. while ( minnode->avl_left != NULLAVL )
  411. minnode = minnode->avl_left;
  412. /* swap the data */
  413. (*root)->avl_data = minnode->avl_data;
  414. minnode->avl_data = savedata;
  415. savedata = ravl_delete( &(*root)->avl_right, data, fcmp,
  416. &shortersubtree );
  417. if ( shortersubtree )
  418. *shorter = right_balance( root );
  419. else
  420. *shorter = 0;
  421. /* go left */
  422. } else if ( cmp < 0 ) {
  423. if ( (savedata = ravl_delete( &(*root)->avl_left, data, fcmp,
  424. &shortersubtree )) == 0 ) {
  425. *shorter = 0;
  426. return( 0 );
  427. }
  428. /* left subtree shorter? */
  429. if ( shortersubtree )
  430. *shorter = left_balance( root );
  431. else
  432. *shorter = 0;
  433. /* go right */
  434. } else {
  435. if ( (savedata = ravl_delete( &(*root)->avl_right, data, fcmp,
  436. &shortersubtree )) == 0 ) {
  437. *shorter = 0;
  438. return( 0 );
  439. }
  440. if ( shortersubtree )
  441. *shorter = right_balance( root );
  442. else
  443. *shorter = 0;
  444. }
  445. return( savedata );
  446. }
  447. /*
  448. * avl_delete() - deletes the node containing data (according to fcmp) from
  449. * the avl tree rooted at root.
  450. */
  451. caddr_t
  452. avl_delete( Avlnode **root, caddr_t data, IFP fcmp )
  453. {
  454. int shorter;
  455. return( ravl_delete( root, data, fcmp, &shorter ) );
  456. }
  457. static int
  458. avl_inapply( Avlnode *root, IFP fn, caddr_t arg, int stopflag )
  459. {
  460. if ( root == 0 )
  461. return( AVL_NOMORE );
  462. if ( root->avl_left != 0 )
  463. if ( avl_inapply( root->avl_left, fn, arg, stopflag )
  464. == stopflag )
  465. return( stopflag );
  466. if ( (*fn)( root->avl_data, arg ) == stopflag )
  467. return( stopflag );
  468. if ( root->avl_right == 0 )
  469. return( AVL_NOMORE );
  470. else
  471. return( avl_inapply( root->avl_right, fn, arg, stopflag ) );
  472. }
  473. static int
  474. avl_postapply( Avlnode *root, IFP fn, caddr_t arg, int stopflag )
  475. {
  476. if ( root == 0 )
  477. return( AVL_NOMORE );
  478. if ( root->avl_left != 0 )
  479. if ( avl_postapply( root->avl_left, fn, arg, stopflag )
  480. == stopflag )
  481. return( stopflag );
  482. if ( root->avl_right != 0 )
  483. if ( avl_postapply( root->avl_right, fn, arg, stopflag )
  484. == stopflag )
  485. return( stopflag );
  486. return( (*fn)( root->avl_data, arg ) );
  487. }
  488. static int
  489. avl_preapply( Avlnode *root, IFP fn, caddr_t arg, int stopflag )
  490. {
  491. if ( root == 0 )
  492. return( AVL_NOMORE );
  493. if ( (*fn)( root->avl_data, arg ) == stopflag )
  494. return( stopflag );
  495. if ( root->avl_left != 0 )
  496. if ( avl_preapply( root->avl_left, fn, arg, stopflag )
  497. == stopflag )
  498. return( stopflag );
  499. if ( root->avl_right == 0 )
  500. return( AVL_NOMORE );
  501. else
  502. return( avl_preapply( root->avl_right, fn, arg, stopflag ) );
  503. }
  504. /*
  505. * avl_apply -- avl tree root is traversed, function fn is called with
  506. * arguments arg and the data portion of each node. if fn returns stopflag,
  507. * the traversal is cut short, otherwise it continues. Do not use -6 as
  508. * a stopflag, as this is what is used to indicate the traversal ran out
  509. * of nodes.
  510. */
  511. int
  512. avl_apply(
  513. Avlnode *root,
  514. IFP fn,
  515. caddr_t arg,
  516. int stopflag,
  517. int type
  518. )
  519. {
  520. switch ( type ) {
  521. case AVL_INORDER:
  522. return( avl_inapply( root, fn, arg, stopflag ) );
  523. case AVL_PREORDER:
  524. return( avl_preapply( root, fn, arg, stopflag ) );
  525. case AVL_POSTORDER:
  526. return( avl_postapply( root, fn, arg, stopflag ) );
  527. default:
  528. fprintf( stderr, "Invalid traversal type %d\n", type );
  529. return( -1 );
  530. }
  531. /* NOTREACHED */
  532. }
  533. /*
  534. * avl_prefixapply - traverse avl tree root, applying function fprefix
  535. * to any nodes that match. fcmp is called with data as its first arg
  536. * and the current node's data as its second arg. it should return
  537. * 0 if they match, < 0 if data is less, and > 0 if data is greater.
  538. * the idea is to efficiently find all nodes that are prefixes of
  539. * some key... Like avl_apply, this routine also takes a stopflag
  540. * and will return prematurely if fmatch returns this value. Otherwise,
  541. * AVL_NOMORE is returned.
  542. */
  543. int
  544. avl_prefixapply(
  545. Avlnode *root,
  546. caddr_t data,
  547. IFP fmatch,
  548. caddr_t marg,
  549. IFP fcmp,
  550. caddr_t carg,
  551. int stopflag
  552. )
  553. {
  554. int cmp;
  555. if ( root == 0 )
  556. return( AVL_NOMORE );
  557. cmp = (*fcmp)( data, root->avl_data, carg );
  558. if ( cmp == 0 ) {
  559. if ( (*fmatch)( root->avl_data, marg ) == stopflag )
  560. return( stopflag );
  561. if ( root->avl_left != 0 )
  562. if ( avl_prefixapply( root->avl_left, data, fmatch,
  563. marg, fcmp, carg, stopflag ) == stopflag )
  564. return( stopflag );
  565. if ( root->avl_right != 0 )
  566. return( avl_prefixapply( root->avl_right, data, fmatch,
  567. marg, fcmp, carg, stopflag ) );
  568. else
  569. return( AVL_NOMORE );
  570. } else if ( cmp < 0 ) {
  571. if ( root->avl_left != 0 )
  572. return( avl_prefixapply( root->avl_left, data, fmatch,
  573. marg, fcmp, carg, stopflag ) );
  574. } else {
  575. if ( root->avl_right != 0 )
  576. return( avl_prefixapply( root->avl_right, data, fmatch,
  577. marg, fcmp, carg, stopflag ) );
  578. }
  579. return( AVL_NOMORE );
  580. }
  581. /*
  582. * avl_free -- traverse avltree root, freeing the memory it is using.
  583. * the dfree() is called to free the data portion of each node. The
  584. * number of items actually freed is returned.
  585. */
  586. int
  587. avl_free( Avlnode *root, IFP dfree )
  588. {
  589. int nleft, nright;
  590. if ( root == 0 )
  591. return( 0 );
  592. nleft = nright = 0;
  593. if ( root->avl_left != 0 )
  594. nleft = avl_free( root->avl_left, dfree );
  595. if ( root->avl_right != 0 )
  596. nright = avl_free( root->avl_right, dfree );
  597. if ( dfree )
  598. (*dfree)( root->avl_data );
  599. free( (char *)root );
  600. return( nleft + nright + 1 );
  601. }
  602. /*
  603. * avl_find -- search avltree root for a node with data data. the function
  604. * cmp is used to compare things. it is called with data as its first arg
  605. * and the current node data as its second. it should return 0 if they match,
  606. * < 0 if arg1 is less than arg2 and > 0 if arg1 is greater than arg2.
  607. */
  608. caddr_t
  609. avl_find( Avlnode *root, caddr_t data, IFP fcmp )
  610. {
  611. int cmp;
  612. while ( root != 0 && (cmp = (*fcmp)( data, root->avl_data )) != 0 ) {
  613. if ( cmp < 0 )
  614. root = root->avl_left;
  615. else
  616. root = root->avl_right;
  617. }
  618. return( root ? root->avl_data : 0 );
  619. }
  620. /*
  621. * avl_find_lin -- search avltree root linearly for a node with data data.
  622. * the function cmp is used to compare things. it is called with data as its
  623. * first arg and the current node data as its second. it should return 0 if
  624. * they match, non-zero otherwise.
  625. */
  626. caddr_t
  627. avl_find_lin( Avlnode *root, caddr_t data, IFP fcmp )
  628. {
  629. caddr_t res;
  630. if ( root == 0 )
  631. return( NULL );
  632. if ( (*fcmp)( data, root->avl_data ) == 0 )
  633. return( root->avl_data );
  634. if ( root->avl_left != 0 )
  635. if ( (res = avl_find_lin( root->avl_left, data, fcmp ))
  636. != NULL )
  637. return( res );
  638. if ( root->avl_right == 0 )
  639. return( NULL );
  640. else
  641. return( avl_find_lin( root->avl_right, data, fcmp ) );
  642. }
  643. static caddr_t *avl_list = (caddr_t *)0;
  644. static int avl_maxlist = 0;
  645. static int avl_nextlist = 0;
  646. #define AVL_GRABSIZE 100
  647. /* ARGSUSED */
  648. static int
  649. avl_buildlist( caddr_t data, int arg )
  650. {
  651. static int slots = 0;
  652. if ( avl_list == (caddr_t *) 0 ) {
  653. avl_list = (caddr_t *) malloc(AVL_GRABSIZE * sizeof(caddr_t));
  654. slots = AVL_GRABSIZE;
  655. avl_maxlist = 0;
  656. } else if ( avl_maxlist == slots ) {
  657. slots += AVL_GRABSIZE;
  658. avl_list = (caddr_t *) realloc( (char *) avl_list,
  659. (unsigned) slots * sizeof(caddr_t));
  660. }
  661. avl_list[ avl_maxlist++ ] = data;
  662. return( 0 );
  663. }
  664. /*
  665. * avl_getfirst() and avl_getnext() are provided as alternate tree
  666. * traversal methods, to be used when a single function cannot be
  667. * provided to be called with every node in the tree. avl_getfirst()
  668. * traverses the tree and builds a linear list of all the nodes,
  669. * returning the first node. avl_getnext() returns the next thing
  670. * on the list built by avl_getfirst(). This means that avl_getfirst()
  671. * can take a while, and that the tree should not be messed with while
  672. * being traversed in this way, and that multiple traversals (even of
  673. * different trees) cannot be active at once.
  674. */
  675. caddr_t
  676. avl_getfirst( Avlnode *root )
  677. {
  678. if ( avl_list ) {
  679. free( (char *) avl_list);
  680. avl_list = (caddr_t *) 0;
  681. }
  682. avl_maxlist = 0;
  683. avl_nextlist = 0;
  684. if ( root == 0 )
  685. return( 0 );
  686. (void) avl_apply( root, avl_buildlist, (caddr_t) 0, -1, AVL_INORDER );
  687. return( avl_list[ avl_nextlist++ ] );
  688. }
  689. caddr_t
  690. avl_getnext()
  691. {
  692. if ( avl_list == 0 )
  693. return( 0 );
  694. if ( avl_nextlist == avl_maxlist ) {
  695. free( (caddr_t) avl_list);
  696. avl_list = (caddr_t *) 0;
  697. return( 0 );
  698. }
  699. return( avl_list[ avl_nextlist++ ] );
  700. }
  701. int avl_dup_error()
  702. {
  703. return( -1 );
  704. }
  705. int avl_dup_ok()
  706. {
  707. return( 0 );
  708. }