home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3232 < prev    next >
Encoding:
Internet Message Format  |  1991-04-22  |  42.9 KB

  1. From: asp@cs.cmu.edu (James Aspnes)
  2. Newsgroups: alt.sources
  3. Subject: living without X_Hash
  4. Message-ID: <CMM.0.88.672291729.asp@FURST.THEORY.CS.CMU.EDU>
  5. Date: 22 Apr 91 03:42:09 GMT
  6.  
  7. Lest anybody be tempted to send $15 to Kenneth, even under his present
  8. allegedly-friendlier terms, I'd like to point out that:
  9.  
  10.  1. Hash tables and doubly-linked lists are both covered in the first
  11.     month of sophomore-level programming courses.
  12.  
  13.  2. Kenneth's implementation isn't all that good.
  14.  
  15.  3. You are better off using your $15 as a down-payment for a good
  16.     algorithms text (Sedgewick's _Algorithms in C_, which runs at
  17.     about $40 in hardcover, contains code for several varieties of
  18.     hash tables and linked lists, and much besides.)
  19.  
  20.  4. If you just want to hack around with a generic data structure that
  21.     does just about everything, but don't want to know how it works,
  22.     the following free, public domain code from the comp.sources.unix
  23.     archives does everything a hash table or a linked list does, and
  24.     more.
  25.  
  26. --James Aspnes <asp@cs.cmu.edu>
  27.  
  28. Subject:  v14i087:  Splay tree library
  29. Newsgroups: comp.sources.unix
  30. Sender: sources
  31. Approved: rsalz@uunet.UU.NET
  32.  
  33. Submitted-by: Dave Brower <rtech!llama!daveb>
  34. Posting-number: Volume 14, Issue 87
  35. Archive-name: splay-tree
  36.  
  37. [  Kinda like AVL or balanced tree stuff, but not really.  --r$ ]
  38.  
  39. Here is a library for working with the splay trees Tarjan talked about
  40. in his ACM Turing Lecture.  I use it for symbol tables and the like.
  41. Others might want to change the key type and the use of strcmp as the
  42. ordering function.
  43.  
  44. It is a transliteration from Pascal code given me by Doug Jones at the U
  45. of Iowa.  Send thank you notes to him as jones@cs.uiowa.edu.
  46.  
  47. There is a man page and a Makefile for "libsptree.a."  I would be
  48. shocked, shocked indeed to find any critical system dependencies.  
  49. (Some of the statistics might want to be longs on 16 bit machines to
  50. avoid overflow).
  51.  
  52. Users must supply their own emalloc() function.  The cavalier might
  53. do "-Demalloc=malloc" during the compilation.
  54.  
  55. -dB
  56.  
  57. #!/bin/sh
  58. # This is a shell archive, meaning:
  59. # 1. Remove everything above the #!/bin/sh line.
  60. # 2. Save the resulting text in a file.
  61. # 3. Execute the file with /bin/sh (not csh) to create the files:
  62. #    Makefile
  63. #    spaux.c
  64. #    spdaveb.c
  65. #    sptree.3
  66. #    sptree.c
  67. #    sptree.h
  68. # This archive created: Wed Feb 10 19:41:38 1988
  69. export PATH; PATH=/bin:$PATH
  70. if test -f 'Makefile'
  71. then
  72.     echo shar: over-writing existing file "'Makefile'"
  73. fi
  74. sed 's/^X//' << \SHAR_EOF > 'Makefile'
  75. X#
  76. X# makefile for splay tree library
  77. X
  78. Xall:    libsptree.a
  79. X
  80. Xlibsptree.a:    sptree.o spaux.o spdaveb.o
  81. X    ar rvu libsptree.a sptree.o spaux.o spdaveb.o
  82. X
  83. SHAR_EOF
  84. if test -f 'spaux.c'
  85. then
  86.     echo shar: over-writing existing file "'spaux.c'"
  87. fi
  88. sed 's/^X//' << \SHAR_EOF > 'spaux.c'
  89. X/*
  90. X  spaux.c:  This code implements the following operations on an event-set
  91. X  or priority-queue implemented using splay trees:
  92. X  
  93. X  n = sphead( q )        n is the head item in q (not removed).
  94. X  spdelete( n, q )        n is removed from q.
  95. X  n = spnext( np, q )        n is the successor of np in q.
  96. X  n = spprev( np, q )        n is the predecessor of np in q.
  97. X  spenqbefore( n, np, q )    n becomes the predecessor of np in q.
  98. X  spenqafter( n, np, q )    n becomes the successor of np in q.
  99. X  
  100. X  In the above, n and np are pointers to single items (type
  101. X  SPBLK *); q is an event-set (type SPTREE *),
  102. X  The type definitions for these are taken
  103. X  from file sptree.h.  All of these operations rest on basic
  104. X  splay tree operations from file sptree.c.
  105. X  
  106. X  The basic splay tree algorithms were originally presented in:
  107. X  
  108. X  Self Adjusting Binary Trees,
  109. X  by D. D. Sleator and R. E. Tarjan,
  110. X  Proc. ACM SIGACT Symposium on Theory
  111. X  of Computing (Boston, Apr 1983) 235-245.
  112. X  
  113. X  The operations in this package supplement the operations from
  114. X  file splay.h to provide support for operations typically needed
  115. X  on the pending event set in discrete event simulation.  See, for
  116. X  example,
  117. X  
  118. X  Introduction to Simula 67,
  119. X  by Gunther Lamprecht, Vieweg & Sohn, Braucschweig, Wiesbaden, 1981.
  120. X  (Chapter 14 contains the relevant discussion.)
  121. X  
  122. X  Simula Begin,
  123. X  by Graham M. Birtwistle, et al, Studentlitteratur, Lund, 1979.
  124. X  (Chapter 9 contains the relevant discussion.)
  125. X  
  126. X  Many of the routines in this package use the splay procedure,
  127. X  for bottom-up splaying of the queue.  Consequently, item n in
  128. X  delete and item np in all operations listed above must be in the
  129. X  event-set prior to the call or the results will be
  130. X  unpredictable (eg:  chaos will ensue).
  131. X  
  132. X  Note that, in all cases, these operations can be replaced with
  133. X  the corresponding operations formulated for a conventional
  134. X  lexicographically ordered tree.  The versions here all use the
  135. X  splay operation to ensure the amortized bounds; this usually
  136. X  leads to a very compact formulation of the operations
  137. X  themselves, but it may slow the average performance.
  138. X  
  139. X  Alternative versions based on simple binary tree operations are
  140. X  provided (commented out) for head, next, and prev, since these
  141. X  are frequently used to traverse the entire data structure, and
  142. X  the cost of traversal is independent of the shape of the
  143. X  structure, so the extra time taken by splay in this context is
  144. X  wasted.
  145. X  
  146. X  This code was written by:
  147. X  Douglas W. Jones with assistance from Srinivas R. Sataluri
  148. X  
  149. X  Translated to C by David Brower, daveb@rtech.uucp
  150. X  
  151. X */
  152. X
  153. X# include    "sptree.h"
  154. X
  155. X
  156. X/*----------------
  157. X *
  158. X * sphead() --    return the "lowest" element in the tree.
  159. X *
  160. X *    returns a reference to the head event in the event-set q,
  161. X *    represented as a splay tree; q->root ends up pointing to the head
  162. X *    event, and the old left branch of q is shortened, as if q had
  163. X *    been splayed about the head element; this is done by dequeueing
  164. X *    the head and then making the resulting queue the right son of
  165. X *    the head returned by spdeq; an alternative is provided which
  166. X *    avoids splaying but just searches for and returns a pointer to
  167. X *    the bottom of the left branch
  168. X */
  169. XSPBLK *
  170. Xsphead( q )
  171. X
  172. Xregister SPTREE * q;
  173. X
  174. X{
  175. X    register SPBLK * x;
  176. X    
  177. X    /* splay version, good amortized bound */
  178. X    x = spdeq( q->root );
  179. X    if( x != NULL )
  180. X    {
  181. X        x->rightlink = q->root;
  182. X        x->leftlink = NULL;
  183. X        x->uplink = NULL;
  184. X        if( q->root != NULL )
  185. X        q->root->uplink = x;
  186. X    }
  187. X    q->root = x;
  188. X    
  189. X    /* alternative version, bad amortized bound,
  190. X       but faster on the average */
  191. X    
  192. X# if 0
  193. X    x = q->root;
  194. X    while( x->leftlink != NULL )
  195. X    x = x->leftlink;
  196. X# endif
  197. X    
  198. X    return( x );
  199. X    
  200. X} /* sphead */
  201. X
  202. X
  203. X
  204. X/*----------------
  205. X *
  206. X * spdelete() -- Delete node from a tree.
  207. X *
  208. X *    n is deleted from q; the resulting splay tree has been splayed
  209. X *    around its new root, which is the successor of n
  210. X *
  211. X */
  212. Xvoid
  213. Xspdelete( n, q )
  214. X
  215. Xregister SPBLK * n;
  216. Xregister SPTREE * q;
  217. X
  218. X{
  219. X    register SPBLK * x;
  220. X    
  221. X    splay( n, q );
  222. X    x = spdeq( q->root->rightlink );
  223. X    if( x == NULL )        /* empty right subtree */
  224. X    {
  225. X        q->root = q->root->leftlink;
  226. X        q->root->uplink = NULL;
  227. X    }
  228. X    else            /* non-empty right subtree */
  229. X    {
  230. X        x->uplink = NULL;
  231. X        x->leftlink = q->root->leftlink;
  232. X        x->rightlink = q->root->rightlink;
  233. X        if( x->leftlink != NULL )
  234. X        x->leftlink->uplink = x;
  235. X        if( x->rightlink != NULL )
  236. X        x->rightlink->uplink = x;
  237. X        q->root = x;
  238. X    }
  239. X    
  240. X} /* spdelete */
  241. X
  242. X
  243. X
  244. X/*----------------
  245. X *
  246. X * spnext() -- return next higer item in the tree, or NULL.
  247. X *
  248. X *    return the successor of n in q, represented as a splay tree; the
  249. X *    successor becomes the root; two alternate versions are provided,
  250. X *    one which is shorter, but slower, and one which is faster on the
  251. X *    average because it does not do any splaying
  252. X *
  253. X */
  254. XSPBLK *
  255. Xspnext( n, q )
  256. X
  257. Xregister SPBLK * n;
  258. Xregister SPTREE * q;
  259. X
  260. X{
  261. X    register SPBLK * next;
  262. X    register SPBLK * x;
  263. X    
  264. X    /* splay version */
  265. X    splay( n, q );
  266. X    x = spdeq( n->rightlink );
  267. X    if( x != NULL )
  268. X    {
  269. X        x->leftlink = n;
  270. X        n->uplink = x;
  271. X        x->rightlink = n->rightlink;
  272. X        n->rightlink = NULL;
  273. X        if( x->rightlink != NULL )
  274. X        x->rightlink->uplink = x;
  275. X        q->root = x;
  276. X        x->uplink = NULL;
  277. X    }
  278. X    next = x;
  279. X    
  280. X    /* shorter slower version;
  281. X       deleting last "if" undoes the amortized bound */
  282. X    
  283. X# if 0
  284. X    splay( n, q );
  285. X    x = n->rightlink;
  286. X    if( x != NULL )
  287. X    while( x->leftlink != NULL )
  288. X        x = x->leftlink;
  289. X    next = x;
  290. X    if( x != NULL )
  291. X    splay( x, q );
  292. X# endif
  293. X    
  294. X    return( next );
  295. X    
  296. X} /* spnext */
  297. X
  298. X
  299. X
  300. X/*----------------
  301. X *
  302. X * spprev() -- return previous node in a tree, or NULL.
  303. X *
  304. X *    return the predecessor of n in q, represented as a splay tree;
  305. X *    the predecessor becomes the root; an alternate version is
  306. X *    provided which is faster on the average because it does not do
  307. X *    any splaying
  308. X *
  309. X */
  310. XSPBLK *
  311. Xspprev( n, q )
  312. X
  313. Xregister SPBLK * n;
  314. Xregister SPTREE * q;
  315. X
  316. X{
  317. X    register SPBLK * prev;
  318. X    register SPBLK * x;
  319. X    
  320. X    /* splay version;
  321. X       note: deleting the last "if" undoes the amortized bound */
  322. X    
  323. X    splay( n, q );
  324. X    x = n->leftlink;
  325. X    if( x != NULL )
  326. X    while( x->rightlink != NULL )
  327. X        x = x->rightlink;
  328. X    prev = x;
  329. X    if( x != NULL )
  330. X    splay( x, q );
  331. X    
  332. X    return( prev );
  333. X    
  334. X} /* spprev */
  335. X
  336. X
  337. X
  338. X/*----------------
  339. X *
  340. X * spenqbefore() -- insert node before another in a tree.
  341. X *
  342. X *    returns pointer to n.
  343. X *
  344. X *    event n is entered in the splay tree q as the immediate
  345. X *    predecessor of n1; in doing so, n1 becomes the root of the tree
  346. X *    with n as its left son
  347. X *
  348. X */
  349. XSPBLK *
  350. Xspenqbefore( n, n1, q )
  351. X
  352. Xregister SPBLK * n;
  353. Xregister SPBLK * n1;
  354. Xregister SPTREE * q;
  355. X
  356. X{
  357. X    splay( n1, q );
  358. X    n->key = n1->key;
  359. X    n->leftlink = n1->leftlink;
  360. X    if( n->leftlink != NULL )
  361. X    n->leftlink->uplink = n;
  362. X    n->rightlink = NULL;
  363. X    n->uplink = n1;
  364. X    n1->leftlink = n;
  365. X    
  366. X    return( n );
  367. X    
  368. X} /* spenqbefore */
  369. X
  370. X
  371. X
  372. X/*----------------
  373. X *
  374. X * spenqafter() -- enter n after n1 in tree q.
  375. X *
  376. X *    returns a pointer to n.
  377. X *
  378. X *    event n is entered in the splay tree q as the immediate
  379. X *    successor of n1; in doing so, n1 becomes the root of the tree
  380. X *    with n as its right son
  381. X */
  382. XSPBLK *
  383. Xspenqafter( n, n1, q )
  384. X
  385. Xregister SPBLK * n;
  386. Xregister SPBLK * n1;
  387. Xregister SPTREE * q;
  388. X
  389. X{
  390. X    splay( n1, q );
  391. X    n->key = n1->key;
  392. X    n->rightlink = n1->rightlink;
  393. X    if( n->rightlink != NULL )
  394. X    n->rightlink->uplink = n;
  395. X    n->leftlink = NULL;
  396. X    n->uplink = n1;
  397. X    n1->rightlink = n;
  398. X    
  399. X    return( n );
  400. X    
  401. X} /* spenqafter */
  402. X
  403. X
  404. SHAR_EOF
  405. if test -f 'spdaveb.c'
  406. then
  407.     echo shar: over-writing existing file "'spdaveb.c'"
  408. fi
  409. sed 's/^X//' << \SHAR_EOF > 'spdaveb.c'
  410. X/*
  411. X * spdaveb.c -- daveb's new splay tree functions.
  412. X *
  413. X * The functions in this file provide an interface that is nearly
  414. X * the same as the hash library I swiped from mkmf, allowing
  415. X * replacement of one by the other.  Hey, it worked for me!
  416. X *
  417. X * splookup() -- given a key, find a node in a tree.
  418. X * spinstall() -- install an item in the tree, overwriting existing value.
  419. X * spfhead() -- fast (non-splay) find the first node in a tree.
  420. X * spftail() -- fast (non-splay) find the last node in a tree.
  421. X * spscan() -- forward scan tree from the head.
  422. X * sprscan() -- reverse scan tree from the tail.
  423. X * spfnext() -- non-splaying next.
  424. X * spfprev() -- non-splaying prev.
  425. X * spstats() -- make char string of stats for a tree.
  426. X *
  427. X * Written by David Brower, daveb@rtech.uucp 1/88.
  428. X */
  429. X
  430. X
  431. X# include "sptree.h"
  432. X
  433. X/* USER SUPPLIED! */
  434. X
  435. Xextern char *emalloc();
  436. X
  437. X
  438. X/*----------------
  439. X *
  440. X * splookup() -- given key, find a node in a tree.
  441. X *
  442. X *    Splays the found node to the root.
  443. X */
  444. XSPBLK *
  445. Xsplookup( key, q )
  446. X
  447. Xregister char * key;
  448. Xregister SPTREE * q;
  449. X
  450. X{
  451. X    register SPBLK * n;
  452. X    register int Sct;
  453. X    register int c;
  454. X
  455. X    /* find node in the tree */
  456. X    n = q->root;
  457. X    c = ++(q->lkpcmps);
  458. X    q->lookups++;
  459. X    while( n && (Sct = STRCMP( key, n->key ) ) )
  460. X    {
  461. X    c++;
  462. X    n = ( Sct < 0 ) ? n->leftlink : n->rightlink;
  463. X    }
  464. X    q->lkpcmps = c;
  465. X
  466. X    /* reorganize tree around this node */
  467. X    if( n != NULL )
  468. X    splay( n, q );
  469. X
  470. X    return( n );
  471. X}
  472. X
  473. X
  474. X
  475. X/*----------------
  476. X *
  477. X * spinstall() -- install an entry in a tree, overwriting any existing node.
  478. X *
  479. X *    If the node already exists, replace its contents.
  480. X *    If it does not exist, then allocate a new node and fill it in.
  481. X */
  482. X
  483. XSPBLK *
  484. Xspinstall( key, data, datb, q )
  485. X
  486. Xregister char * key;
  487. Xregister char * data;
  488. Xregister char * datb;
  489. Xregister SPTREE *q;
  490. X
  491. X{
  492. X    register SPBLK *n;
  493. X
  494. X    if( NULL == ( n = splookup( key, q ) ) )
  495. X    {
  496. X    n = (SPBLK *) emalloc( sizeof( *n ) );
  497. X    n->key = key;
  498. X    n->leftlink = NULL;
  499. X    n->rightlink = NULL;
  500. X    n->uplink = NULL;
  501. X    spenq( n, q );
  502. X    }
  503. X
  504. X    n->data = data;
  505. X    n->datb = datb;
  506. X
  507. X    return( n );
  508. X}
  509. X
  510. X
  511. X
  512. X
  513. X/*----------------
  514. X *
  515. X * spfhead() --    return the "lowest" element in the tree.
  516. X *
  517. X *    returns a reference to the head event in the event-set q.
  518. X *    avoids splaying but just searches for and returns a pointer to
  519. X *    the bottom of the left branch.
  520. X */
  521. XSPBLK *
  522. Xspfhead( q )
  523. X
  524. Xregister SPTREE * q;
  525. X
  526. X{
  527. X    register SPBLK * x;
  528. X
  529. X    if( NULL != ( x = q->root ) )
  530. X    while( x->leftlink != NULL )
  531. X        x = x->leftlink;
  532. X
  533. X    return( x );
  534. X
  535. X} /* spfhead */
  536. X
  537. X
  538. X
  539. X
  540. X/*----------------
  541. X *
  542. X * spftail() -- find the last node in a tree.
  543. X *
  544. X *    Fast version does not splay result or intermediate steps.
  545. X */
  546. XSPBLK *
  547. Xspftail( q )
  548. X
  549. XSPTREE * q;
  550. X
  551. X{
  552. X    register SPBLK * x;
  553. X
  554. X
  555. X    if( NULL != ( x = q->root ) )
  556. X    while( x->rightlink != NULL )
  557. X        x = x->rightlink;
  558. X
  559. X    return( x );
  560. X
  561. X} /* spftail */
  562. X
  563. X
  564. X/*----------------
  565. X *
  566. X * spscan() -- apply a function to nodes in ascending order.
  567. X *
  568. X *    if n is given, start at that node, otherwise start from
  569. X *    the head.
  570. X */
  571. Xvoid
  572. Xspscan( f, n, q )
  573. X
  574. Xregister int (*f)();
  575. Xregister SPBLK * n;
  576. Xregister SPTREE * q;
  577. X
  578. X{
  579. X    register SPBLK * x;
  580. X
  581. X    for( x = n != NULL ? n : spfhead( q ); x != NULL ; x = spfnext( x ) )
  582. X        (*f)( x );
  583. X}
  584. X
  585. X
  586. X
  587. X/*----------------
  588. X *
  589. X * sprscan() -- apply a function to nodes in descending order.
  590. X *
  591. X *    if n is given, start at that node, otherwise start from
  592. X *    the tail.
  593. X */
  594. Xvoid
  595. Xsprscan( f, n, q )
  596. X
  597. Xregister int (*f)();
  598. Xregister SPBLK * n;
  599. Xregister SPTREE * q;
  600. X
  601. X{
  602. X    register SPBLK *x;
  603. X
  604. X    for( x = n != NULL ? n : spftail( q ); x != NULL ; x = spfprev( x ) )
  605. X        (*f)( x );
  606. X}
  607. X
  608. X
  609. X
  610. X/*----------------
  611. X *
  612. X * spfnext() -- fast return next higer item in the tree, or NULL.
  613. X *
  614. X *    return the successor of n in q, represented as a splay tree.
  615. X *    This is a fast (on average) version that does not splay.
  616. X */
  617. XSPBLK *
  618. Xspfnext( n )
  619. X
  620. Xregister SPBLK * n;
  621. X
  622. X{
  623. X    register SPBLK * next;
  624. X    register SPBLK * x;
  625. X
  626. X    /* a long version, avoids splaying for fast average,
  627. X     * poor amortized bound
  628. X     */
  629. X
  630. X    if( n == NULL )
  631. X        return( n );
  632. X
  633. X    x = n->rightlink;
  634. X    if( x != NULL )
  635. X    {
  636. X        while( x->leftlink != NULL )
  637. X        x = x->leftlink;
  638. X        next = x;
  639. X    }
  640. X    else    /* x == NULL */
  641. X    {
  642. X        x = n->uplink;
  643. X        next = NULL;
  644. X        while( x != NULL )
  645. X    {
  646. X            if( x->leftlink == n )
  647. X        {
  648. X                next = x;
  649. X                x = NULL;
  650. X            }
  651. X        else
  652. X        {
  653. X                n = x;
  654. X                x = n->uplink;
  655. X            }
  656. X        }
  657. X    }
  658. X
  659. X    return( next );
  660. X
  661. X} /* spfnext */
  662. X
  663. X
  664. X
  665. X/*----------------
  666. X *
  667. X * spfprev() -- return fast previous node in a tree, or NULL.
  668. X *
  669. X *    return the predecessor of n in q, represented as a splay tree.
  670. X *    This is a fast (on average) version that does not splay.
  671. X */
  672. XSPBLK *
  673. Xspfprev( n )
  674. X
  675. Xregister SPBLK * n;
  676. X
  677. X{
  678. X    register SPBLK * prev;
  679. X    register SPBLK * x;
  680. X
  681. X    /* a long version,
  682. X     * avoids splaying for fast average, poor amortized bound
  683. X     */
  684. X
  685. X    if( n == NULL )
  686. X        return( n );
  687. X
  688. X    x = n->leftlink;
  689. X    if( x != NULL )
  690. X    {
  691. X        while( x->rightlink != NULL )
  692. X        x = x->rightlink;
  693. X        prev = x;
  694. X    }
  695. X    else
  696. X    {
  697. X        x = n->uplink;
  698. X        prev = NULL;
  699. X        while( x != NULL )
  700. X    {
  701. X            if( x->rightlink == n )
  702. X        {
  703. X                prev = x;
  704. X                x = NULL;
  705. X            }
  706. X        else
  707. X        {
  708. X                n = x;
  709. X                x = n->uplink;
  710. X            }
  711. X        }
  712. X    }
  713. X
  714. X    return( prev );
  715. X
  716. X} /* spfprev */
  717. X
  718. X
  719. X
  720. Xchar *
  721. Xspstats( q )
  722. XSPTREE *q;
  723. X{
  724. X    static char buf[ 128 ];
  725. X    float llen;
  726. X    float elen;
  727. X    float sloops;
  728. X
  729. X    if( q == NULL )
  730. X    return("");
  731. X
  732. X    llen = q->lookups ? (float)q->lkpcmps / q->lookups : 0;
  733. X    elen = q->enqs ? (float)q->enqcmps/q->enqs : 0;
  734. X    sloops = q->splays ? (float)q->splayloops/q->splays : 0;
  735. X
  736. X    sprintf(buf, "f(%d %4.2f) i(%d %4.2f) s(%d %4.2f)",
  737. X    q->lookups, llen, q->enqs, elen, q->splays, sloops );
  738. X
  739. X    return buf;
  740. X}
  741. X
  742. SHAR_EOF
  743. if test -f 'sptree.3'
  744. then
  745.     echo shar: over-writing existing file "'sptree.3'"
  746. fi
  747. sed 's/^X//' << \SHAR_EOF > 'sptree.3'
  748. X.TH SPTREE 3  "10 February 1988"
  749. X.UC 4
  750. X.SH NAME
  751. Xspdelete, spdeq, spempty, spenq, spenqafter, spenqbefore, spenqprior,
  752. Xspfhead, spfnext, spfprev, spftail, sphead, spinit, spinstall, splay,
  753. Xsplookup, spnext, spprev, sprscan, spscan, spstats \- splay tree operations
  754. X.SH SYNOPSIS
  755. X.nf
  756. X.B #include "sptree.h"
  757. X.PP
  758. X.B void spdelete(n, q)
  759. X.B SPBLK *n;
  760. X.B SPTREE *q;
  761. X.PP
  762. X.B SPBLK *spdeq(n)
  763. X.B SPBLK *n;
  764. X.PP
  765. X.B int spempty(q)
  766. X.B SPTREE *q;
  767. X.PP
  768. X.B SPBLK *spenq(n, q)
  769. X.B SPBLK *n;
  770. X.B SPTREE *q;
  771. X.PP
  772. X.B SPBLK *spenqafter(n, n1, q)
  773. X.B SPBLK *n, *n1;
  774. X.B SPTREE *q;
  775. X.PP
  776. X.B SPBLK *spenqbefore(n, n1, q)
  777. X.B SPBLK *n, *n1;
  778. X.B SPTREE *q;
  779. X.PP
  780. X.B SPBLK *spenqprior(n, q)
  781. X.B SPBLK *n;
  782. X.B SPTREE *q;
  783. X.PP
  784. X.B SPBLK *spfhead(q)
  785. X.B SPTREE *q;
  786. X.PP
  787. X.B SPBLK *spfnext(n)
  788. X.B SPBLK *n;
  789. X.PP
  790. X.B SPBLK *spfprev(n)
  791. X.B SPBLK *n;
  792. X.PP
  793. X.B SPBLK *spftail(q)
  794. X.B SPTREE *q;
  795. X.PP
  796. X.B SPBLK *sphead(q)
  797. X.B SPTREE *q;
  798. X.PP
  799. X.B SPTREE *spinit();
  800. X.PP
  801. X.B SPBLK *spinstall(key, data, datb, q)
  802. X.B char *key, *data, *datb;
  803. X.B SPTREE *q;
  804. X.PP
  805. X.B void splay(n, q)
  806. X.B SPBLK *n;
  807. X.B SPTREE *q;
  808. X.PP
  809. X.B SPBLK *splookup(key, q)
  810. X.B char *key;
  811. X.B SPTREE *q;
  812. X.PP
  813. X.B SPBLK *spnext(n, q)
  814. X.B SPBLK *n;
  815. X.B SPTREE *q;
  816. X.PP
  817. X.B SPBLK *spprev(n, q)
  818. X.B SPBLK *n;
  819. X.B SPTREE *q;
  820. X.PP
  821. X.B void sprscan(f, n, q)
  822. X.B int (*f)();
  823. X.B SPBLK *n;
  824. X.B SPTREE *q;
  825. X.PP
  826. X.B void spscan(f, n, q)
  827. X.B int (*f)();
  828. X.B SPBLK *n;
  829. X.B SPTREE *q;
  830. X.PP
  831. X.B char *spstats(q)
  832. X.B SPTREE *q;
  833. X.PP
  834. X.fi
  835. X.SH DESCRIPTION
  836. XThese functions operate on an event\-set or priority\-queue implemented
  837. Xusing splay trees.  These are similar to avl\-trees, but are not
  838. Xconcerned with keeping the tree strictly balanced.  Instead, the tree is
  839. Xdynamically reorganized in a simple way that yields a good amortized
  840. Xbound at the expense of worst case performance.
  841. X.PP
  842. XThe SPTREE structure declared in sptree.h should only be handled
  843. Xindirectly.  A pointer to an SPTREE is returned by
  844. X.I spinit
  845. Xand should be handed blindly to other access functions.
  846. X.PP
  847. XThe nodes in a splay tree are defined by the following structure,
  848. Xdeclared in sptree.h.
  849. X.PP
  850. X.nf
  851. Xtypedef struct _spblk SPBLK;
  852. Xtypedef struct _spblk
  853. X{
  854. X    .
  855. X    .
  856. X    .
  857. X
  858. X    char    *key;
  859. X    char    *data;
  860. X    char    *datb;
  861. X};
  862. X.fi
  863. X.PP
  864. XYou should only refer to the
  865. X.I key,
  866. X.I data
  867. Xand
  868. X.I datb
  869. Xmembers.
  870. X.PP
  871. XThe
  872. X.I key
  873. Xis interpreted as a pointer to a null terminated string, and ordering is
  874. Xdetermined by calls to the usual
  875. X.I strcmp
  876. Xroutine.
  877. X.PP
  878. XNo meaning is associated with the auxiliary members
  879. X.I data
  880. Xor
  881. X.I datb,
  882. Xand you are free to stuff them with whatever good conscience and a legal
  883. Xcast will allow.
  884. X.PP
  885. X.I Spdelete
  886. Xdeletes the node
  887. X.I n
  888. Xfrom the tree
  889. X.I q.
  890. XThe resulting tree is splayed around a new root, which is the successor
  891. Xto
  892. X.I n.
  893. X.PP
  894. X.I Spdeq
  895. Xremoves and returns the head node from the sub\-tree rooted at node
  896. X.I n.
  897. X.PP
  898. X.I Spempty
  899. Xreturns non\-zero if the tree
  900. X.I q
  901. Xhas no members.
  902. X.PP
  903. X.I Spenq
  904. Xinserts node
  905. X.I n
  906. Xinto tree
  907. X.I q
  908. Xafter all other nodes with the same key.  When this is done,
  909. X.I n
  910. Xwill be the root of the tree.
  911. X.PP
  912. X.I Spenqafter
  913. Xinserts node
  914. X.I n
  915. Xas the immediate sucessor of node
  916. X.I n1
  917. Xin tree
  918. X.I q.
  919. XIn so doing,
  920. X.I n1
  921. Xbecomes the root of the tree with
  922. X.I n
  923. Xas its right son.
  924. X.PP
  925. X.I Spenqbefore
  926. Xinserts node
  927. X.I n
  928. Xas the immediate predecessor of node
  929. X.I n1
  930. Xin tree
  931. X.I q.
  932. XIn doing so,
  933. X.I n1
  934. Xbecomes the root of the tree with
  935. X.I n
  936. Xas its left son.
  937. X.PP
  938. X.I Spenqprior
  939. Xinserts node
  940. X.I n
  941. Xinto the tree
  942. X.I q
  943. Xbefore all other nodes with the same key; after this is done,
  944. X.I n
  945. Xwill be the root of the tree.
  946. X.PP
  947. X.I Spfhead
  948. Xreturns a pointer to the head element in the tree
  949. X.I q,
  950. Xbut does not splay it to the root.
  951. X.PP
  952. X.I Spfnext
  953. Xreturns a pointer to the immediate successor of node
  954. X.I n
  955. Xwithout doing any reorganization.
  956. X.PP
  957. X.I Spfprev
  958. Xreturns a pointer to the immediate predecessor of node
  959. X.I n
  960. Xwithout doing any reoganization.
  961. X.PP
  962. X.I Spftail
  963. Xreturns a reference to the last node in the tree
  964. X.I q
  965. Xwithout doing any reorganization.
  966. X.PP
  967. X.I Sphead
  968. Xreturns a pointer to the head event in the tree
  969. X.I q.
  970. XThe returned node is made the root of the tree, as if
  971. X.I q
  972. Xhad been splayed around
  973. X.I n.
  974. X.PP
  975. X.I Spinit
  976. Xcreates a new splay tree using a \fImalloc\fP\-like routine named
  977. X.I emalloc
  978. Xthat must be supplied by the user.
  979. X.PP
  980. X.I Spinstall
  981. Xinserts an entry with the key value pointed to by
  982. X.I key
  983. Xwith the auxiliary values
  984. X.I data
  985. Xand
  986. X.I datb
  987. Xinto the tree
  988. X.I q.
  989. XIf a node with the key value already exists, its auxiliarly values are
  990. Xreplaced.  If the node does not already exist, a new one is allocated
  991. Xwith \fImalloc\fP\-like function named
  992. X.I emalloc
  993. Xthat must be supplied by the user.
  994. X.PP
  995. X.I Splay
  996. Xreorganizes the tree so that node
  997. X.I n
  998. Xbecomes the root of the tree in
  999. X.I q.
  1000. XResults are unpredicatable if
  1001. X.I n
  1002. Xis not in
  1003. X.I q
  1004. Xto start with.
  1005. X.I Q
  1006. Xis split from
  1007. X.I n
  1008. Xup to the old root, with all nodes to the left of
  1009. X.I n
  1010. Xending up in the left sub\-tree, and all nodes to the right of
  1011. X.I n
  1012. Xending up in the right sub\-tree.  The left branch of the right
  1013. Xsub\-tree and the right branch of the left sub\-tree are shortened in
  1014. Xthe process.
  1015. X.PP
  1016. X.I Splookup
  1017. Xsearches for a node containing the key value pointed to by
  1018. X.I key
  1019. Xin the tree
  1020. X.I q.
  1021. XA found node is splayed to the root and returned.  If the key is not
  1022. Xfound, the function returns NULL and no reorganization is done.
  1023. X.PP
  1024. X.I
  1025. XSpnext returns a pointer to the successor of
  1026. X.I n
  1027. Xin
  1028. X.I q.
  1029. XThe successor becomes the root of the tree.
  1030. X.PP
  1031. X.I Spprev
  1032. Xreturns the predecessor of
  1033. X.I n
  1034. Xin
  1035. X.I q.
  1036. XThe predecessor becomes the root.
  1037. X.PP
  1038. X.I Sprscan
  1039. Xapplies the function
  1040. X.I f
  1041. Xstarting at node
  1042. X.I n
  1043. Xto the members of the tree
  1044. X.I q
  1045. Xin reverse order.  If
  1046. X.I n
  1047. Xis NULL, then the scan starts at the tail of the tree.  The tree is not
  1048. Xreorganized during the reverse scan.  The function is called with one
  1049. Xargument, a pointer to an SPBLK.  Its return value is ignored.
  1050. X.PP
  1051. X.I Spscan
  1052. Xapplies the function
  1053. X.I f
  1054. Xstarting at node
  1055. X.I n
  1056. Xin tree
  1057. X.I q
  1058. Xand all successive nodes, in order.  If
  1059. X.I n
  1060. Xis NULL, then the scan starts at the head of the tree.  The tree is not
  1061. Xreorganized during the scan.  The function is called with one argument,
  1062. Xa pointer to an SPBLK.  Its return value is ignored.
  1063. X.PP
  1064. X.I Spstats
  1065. Xreturns a string of statistics on the activities in the tree
  1066. X.I q.
  1067. XIt shows how many times
  1068. X.I splookup
  1069. Xwas called, and how many comparisons were needed per call,
  1070. Xthe number of nodes that have been added with
  1071. X.I spenq
  1072. Xand the number of comparisons needed per call, and finally, the number
  1073. Xof
  1074. X.I splay
  1075. Xoperations performed, and the number of loops done in each splay.  These
  1076. Xstatistics give an indication of the average effective depth of the tree
  1077. Xfor various operations.  The function returns a pointer to a static
  1078. Xbuffer that is overwritten with each call.
  1079. X.SH AUTHORS
  1080. XThe code was originally written in Pascal by Douglas W. Jones
  1081. X(jones@cs.uiowa.edu) with assistance from Srinivas R. Sataluri.  It was
  1082. Xtranslated to C with some new functions by Dave Brower
  1083. X(daveb@rtech.uucp).
  1084. X.SH REFERENCES
  1085. XThe basic splay tree algorithms were originally presented in:
  1086. X.PP
  1087. X.nf
  1088. X  Self Adjusting Binary Trees,
  1089. X  by D. D. Sleator and R. E. Tarjan,
  1090. X  Proc. ACM SIGACT Symposium on Theory
  1091. X  of Computing (Boston, Apr 1983) 235-245.
  1092. X.fi
  1093. X.PP
  1094. XMore operations on priority queues were added to help support discrete
  1095. Xevent simulation.  See, for example Chapter 14 of
  1096. X.PP
  1097. X.nf
  1098. X  Introduction to Simula 67,
  1099. X  by Gunther Lamprecht,
  1100. X  Vieweg & Sohn, Braucschweig, Wiesbaden, 1981.
  1101. X.fi
  1102. X.PP
  1103. Xand Chapter 9 of
  1104. X.PP
  1105. X.nf
  1106. X  Simula Begin,
  1107. X  by Graham M. Birtwistle, et al,
  1108. X  Studentlitteratur, Lund, 1979.
  1109. X.fi
  1110. X.PP
  1111. XSplay trees are compared with other data structures in
  1112. X.PP
  1113. X.nf
  1114. X  An Empirical Comparison of Priority-Queue and Event-Set Implementations,
  1115. X  by Douglas W. Jones,
  1116. X  Comm. ACM 29, 4 (Apr. 1986) 300-311.
  1117. X.fi
  1118. SHAR_EOF
  1119. if test -f 'sptree.c'
  1120. then
  1121.     echo shar: over-writing existing file "'sptree.c'"
  1122. fi
  1123. sed 's/^X//' << \SHAR_EOF > 'sptree.c'
  1124. X/*
  1125. X *
  1126. X *  sptree.c:  The following code implements the basic operations on
  1127. X *  an event-set or priority-queue implemented using splay trees:
  1128. X *
  1129. X *  SPTREE *spinit( compare )    Make a new tree
  1130. X *  int spempty();        Is tree empty?
  1131. X *  SPBLK *spenq( n, q )    Insert n in q after all equal keys.
  1132. X *  SPBLK *spdeq( q )        Return first key in q, removing it.
  1133. X *  SPBLK *spenqprior( n, q )    Insert n in q before all equal keys.
  1134. X *  void splay( n, q )        n (already in q) becomes the root.
  1135. X *
  1136. X *  In the above, n points to an SPBLK type, while q points to an
  1137. X *  SPTREE.
  1138. X *
  1139. X *  The implementation used here is based on the implementation
  1140. X *  which was used in the tests of splay trees reported in:
  1141. X *
  1142. X *    An Empirical Comparison of Priority-Queue and Event-Set Implementations,
  1143. X *    by Douglas W. Jones, Comm. ACM 29, 4 (Apr. 1986) 300-311.
  1144. X *
  1145. X *  The changes made include the addition of the enqprior
  1146. X *  operation and the addition of up-links to allow for the splay
  1147. X *  operation.  The basic splay tree algorithms were originally
  1148. X *  presented in:
  1149. X *
  1150. X *    Self Adjusting Binary Trees,
  1151. X *        by D. D. Sleator and R. E. Tarjan,
  1152. X *            Proc. ACM SIGACT Symposium on Theory
  1153. X *            of Computing (Boston, Apr 1983) 235-245.
  1154. X *
  1155. X *  The enq and enqprior routines use variations on the
  1156. X *  top-down splay operation, while the splay routine is bottom-up.
  1157. X *  All are coded for speed.
  1158. X *
  1159. X *  Written by:
  1160. X *    Douglas W. Jones
  1161. X *
  1162. X *  Translated to C by:
  1163. X *    David Brower, daveb@rtech.uucp
  1164. X *
  1165. X */
  1166. X
  1167. X# include "sptree.h"
  1168. X
  1169. X/* USER SUPPLIED! */
  1170. X
  1171. Xextern char *emalloc();
  1172. X
  1173. X
  1174. X/*----------------
  1175. X *
  1176. X * spinit() -- initialize an empty splay tree
  1177. X *
  1178. X */
  1179. XSPTREE *
  1180. Xspinit()
  1181. X{
  1182. X    register SPTREE * q;
  1183. X
  1184. X    q = (SPTREE *) emalloc( sizeof( *q ) );
  1185. X
  1186. X    q->lookups = 0;
  1187. X    q->lkpcmps = 0;
  1188. X    q->enqs = 0;
  1189. X    q->enqcmps = 0;
  1190. X    q->splays = 0;
  1191. X    q->splayloops = 0;
  1192. X    q->root = NULL;
  1193. X    return( q );
  1194. X}
  1195. X
  1196. X/*----------------
  1197. X *
  1198. X * spempty() -- is an event-set represented as a splay tree empty?
  1199. X */
  1200. Xint
  1201. Xspempty( q )
  1202. X
  1203. XSPTREE *q;
  1204. X
  1205. X{
  1206. X    return( q == NULL || q->root == NULL );
  1207. X}
  1208. X
  1209. X
  1210. X/*----------------
  1211. X *
  1212. X *  spenq() -- insert item in a tree.
  1213. X *
  1214. X *  put n in q after all other nodes with the same key; when this is
  1215. X *  done, n will be the root of the splay tree representing q, all nodes
  1216. X *  in q with keys less than or equal to that of n will be in the
  1217. X *  left subtree, all with greater keys will be in the right subtree;
  1218. X *  the tree is split into these subtrees from the top down, with rotations
  1219. X *  performed along the way to shorten the left branch of the right subtree
  1220. X *  and the right branch of the left subtree
  1221. X */
  1222. XSPBLK *
  1223. Xspenq( n, q )
  1224. X
  1225. Xregister SPBLK * n;
  1226. Xregister SPTREE * q;
  1227. X
  1228. X{
  1229. X    register SPBLK * left;    /* the rightmost node in the left tree */
  1230. X    register SPBLK * right;    /* the leftmost node in the right tree */
  1231. X    register SPBLK * next;    /* the root of the unsplit part */
  1232. X    register SPBLK * temp;
  1233. X
  1234. X    register char * key;
  1235. X    register int Sct;        /* Strcmp value */
  1236. X
  1237. X    q->enqs++;
  1238. X    n->uplink = NULL;
  1239. X    next = q->root;
  1240. X    q->root = n;
  1241. X    if( next == NULL )    /* trivial enq */
  1242. X    {
  1243. X        n->leftlink = NULL;
  1244. X        n->rightlink = NULL;
  1245. X    }
  1246. X    else        /* difficult enq */
  1247. X    {
  1248. X        key = n->key;
  1249. X        left = n;
  1250. X        right = n;
  1251. X
  1252. X        /* n's left and right children will hold the right and left
  1253. X       splayed trees resulting from splitting on n->key;
  1254. X       note that the children will be reversed! */
  1255. X
  1256. X    q->enqcmps++;
  1257. X        if ( STRCMP( next->key, key ) > 0 )
  1258. X        goto two;
  1259. X
  1260. X    one:    /* assert next->key <= key */
  1261. X
  1262. X    do    /* walk to the right in the left tree */
  1263. X    {
  1264. X            temp = next->rightlink;
  1265. X            if( temp == NULL )
  1266. X        {
  1267. X                left->rightlink = next;
  1268. X                next->uplink = left;
  1269. X                right->leftlink = NULL;
  1270. X                goto done;    /* job done, entire tree split */
  1271. X            }
  1272. X
  1273. X        q->enqcmps++;
  1274. X            if( STRCMP( temp->key, key ) > 0 )
  1275. X        {
  1276. X                left->rightlink = next;
  1277. X                next->uplink = left;
  1278. X                left = next;
  1279. X                next = temp;
  1280. X                goto two;    /* change sides */
  1281. X            }
  1282. X
  1283. X            next->rightlink = temp->leftlink;
  1284. X            if( temp->leftlink != NULL )
  1285. X            temp->leftlink->uplink = next;
  1286. X            left->rightlink = temp;
  1287. X            temp->uplink = left;
  1288. X            temp->leftlink = next;
  1289. X            next->uplink = temp;
  1290. X            left = temp;
  1291. X            next = temp->rightlink;
  1292. X            if( next == NULL )
  1293. X        {
  1294. X                right->leftlink = NULL;
  1295. X                goto done;    /* job done, entire tree split */
  1296. X            }
  1297. X
  1298. X        q->enqcmps++;
  1299. X
  1300. X    } while( STRCMP( next->key, key ) <= 0 );    /* change sides */
  1301. X
  1302. X    two:    /* assert next->key > key */
  1303. X
  1304. X    do    /* walk to the left in the right tree */
  1305. X    {
  1306. X            temp = next->leftlink;
  1307. X            if( temp == NULL )
  1308. X        {
  1309. X                right->leftlink = next;
  1310. X                next->uplink = right;
  1311. X                left->rightlink = NULL;
  1312. X                goto done;    /* job done, entire tree split */
  1313. X            }
  1314. X
  1315. X        q->enqcmps++;
  1316. X            if( STRCMP( temp->key, key ) <= 0 )
  1317. X        {
  1318. X                right->leftlink = next;
  1319. X                next->uplink = right;
  1320. X                right = next;
  1321. X                next = temp;
  1322. X                goto one;    /* change sides */
  1323. X            }
  1324. X            next->leftlink = temp->rightlink;
  1325. X            if( temp->rightlink != NULL )
  1326. X            temp->rightlink->uplink = next;
  1327. X            right->leftlink = temp;
  1328. X            temp->uplink = right;
  1329. X            temp->rightlink = next;
  1330. X            next->uplink = temp;
  1331. X            right = temp;
  1332. X            next = temp->leftlink;
  1333. X            if( next == NULL )
  1334. X        {
  1335. X                left->rightlink = NULL;
  1336. X                goto done;    /* job done, entire tree split */
  1337. X            }
  1338. X
  1339. X        q->enqcmps++;
  1340. X
  1341. X    } while( STRCMP( next->key, key ) > 0 );    /* change sides */
  1342. X
  1343. X        goto one;
  1344. X
  1345. X    done:    /* split is done, branches of n need reversal */
  1346. X
  1347. X        temp = n->leftlink;
  1348. X        n->leftlink = n->rightlink;
  1349. X        n->rightlink = temp;
  1350. X    }
  1351. X
  1352. X    return( n );
  1353. X
  1354. X} /* spenq */
  1355. X
  1356. X
  1357. X/*----------------
  1358. X *
  1359. X *  spdeq() -- return and remove head node from a subtree.
  1360. X *
  1361. X *  remove and return the head node from the node set; this deletes
  1362. X *  (and returns) the leftmost node from q, replacing it with its right
  1363. X *  subtree (if there is one); on the way to the leftmost node, rotations
  1364. X *  are performed to shorten the left branch of the tree
  1365. X */
  1366. XSPBLK *
  1367. Xspdeq( n )
  1368. X
  1369. Xregister SPBLK * n;
  1370. X
  1371. X{
  1372. X    register SPBLK * deq;        /* one to return */
  1373. X    register SPBLK * next;           /* the next thing to deal with */
  1374. X    register SPBLK * left;          /* the left child of next */
  1375. X    register SPBLK * farleft;        /* the left child of left */
  1376. X    register SPBLK * farfarleft;    /* the left child of farleft */
  1377. X
  1378. X    if( n == NULL )
  1379. X    {
  1380. X        deq = NULL;
  1381. X    }
  1382. X    else
  1383. X    {
  1384. X        next = n;
  1385. X        left = next->leftlink;
  1386. X        if( left == NULL )
  1387. X    {
  1388. X            deq = next;
  1389. X            n = next->rightlink;
  1390. X            if( n != NULL )
  1391. X        n->uplink = NULL;
  1392. X        }
  1393. X    else for(;;)
  1394. X    {
  1395. X            /* next is not it, left is not NULL, might be it */
  1396. X            farleft = left->leftlink;
  1397. X            if( farleft == NULL )
  1398. X        {
  1399. X                deq = left;
  1400. X                next->leftlink = left->rightlink;
  1401. X                if( left->rightlink != NULL )
  1402. X            left->rightlink->uplink = next;
  1403. X        break;
  1404. X            }
  1405. X
  1406. X            /* next, left are not it, farleft is not NULL, might be it */
  1407. X            farfarleft = farleft->leftlink;
  1408. X            if( farfarleft == NULL )
  1409. X        {
  1410. X                deq = farleft;
  1411. X                left->leftlink = farleft->rightlink;
  1412. X                if( farleft->rightlink != NULL )
  1413. X            farleft->rightlink->uplink = left;
  1414. X        break;
  1415. X            }
  1416. X
  1417. X            /* next, left, farleft are not it, rotate */
  1418. X            next->leftlink = farleft;
  1419. X            farleft->uplink = next;
  1420. X            left->leftlink = farleft->rightlink;
  1421. X            if( farleft->rightlink != NULL )
  1422. X        farleft->rightlink->uplink = left;
  1423. X            farleft->rightlink = left;
  1424. X            left->uplink = farleft;
  1425. X            next = farleft;
  1426. X            left = farfarleft;
  1427. X    }
  1428. X    }
  1429. X
  1430. X    return( deq );
  1431. X
  1432. X} /* spdeq */
  1433. X
  1434. X
  1435. X/*----------------
  1436. X *
  1437. X *  spenqprior() -- insert into tree before other equal keys.
  1438. X *
  1439. X *  put n in q before all other nodes with the same key; after this is
  1440. X *  done, n will be the root of the splay tree representing q, all nodes in
  1441. X *  q with keys less than that of n will be in the left subtree, all with
  1442. X *  greater or equal keys will be in the right subtree; the tree is split
  1443. X *  into these subtrees from the top down, with rotations performed along
  1444. X *  the way to shorten the left branch of the right subtree and the right
  1445. X *  branch of the left subtree; the logic of spenqprior is exactly the
  1446. X *  same as that of spenq except for a substitution of comparison
  1447. X *  operators
  1448. X */
  1449. XSPBLK *
  1450. Xspenqprior( n, q )
  1451. X
  1452. Xregister SPBLK * n;
  1453. XSPTREE * q;
  1454. X
  1455. X{
  1456. X
  1457. X    register SPBLK * left;    /* the rightmost node in the left tree */
  1458. X    register SPBLK * right;    /* the leftmost node in the right tree */
  1459. X    register SPBLK * next;    /* the root of unsplit part of tree */
  1460. X    register SPBLK * temp;
  1461. X    register int Sct;        /* Strcmp value */
  1462. X    register char *key;
  1463. X
  1464. X    n->uplink = NULL;
  1465. X    next = q->root;
  1466. X    q->root = n;
  1467. X    if( next == NULL )    /* trivial enq */
  1468. X    {
  1469. X        n->leftlink = NULL;
  1470. X        n->rightlink = NULL;
  1471. X    }
  1472. X    else        /* difficult enq */
  1473. X    {
  1474. X        key = n->key;
  1475. X        left = n;
  1476. X        right = n;
  1477. X
  1478. X        /* n's left and right children will hold the right and left
  1479. X       splayed trees resulting from splitting on n->key;
  1480. X       note that the children will be reversed! */
  1481. X
  1482. X        if( STRCMP( next->key, key ) >= 0 )
  1483. X        goto two;
  1484. X
  1485. X    one:    /* assert next->key < key */
  1486. X
  1487. X    do    /* walk to the right in the left tree */
  1488. X    {
  1489. X            temp = next->rightlink;
  1490. X            if( temp == NULL )
  1491. X        {
  1492. X                left->rightlink = next;
  1493. X                next->uplink = left;
  1494. X                right->leftlink = NULL;
  1495. X                goto done;    /* job done, entire tree split */
  1496. X            }
  1497. X            if( STRCMP( temp->key, key ) >= 0 )
  1498. X        {
  1499. X                left->rightlink = next;
  1500. X                next->uplink = left;
  1501. X                left = next;
  1502. X                next = temp;
  1503. X                goto two;    /* change sides */
  1504. X            }
  1505. X            next->rightlink = temp->leftlink;
  1506. X            if( temp->leftlink != NULL )
  1507. X        temp->leftlink->uplink = next;
  1508. X            left->rightlink = temp;
  1509. X            temp->uplink = left;
  1510. X            temp->leftlink = next;
  1511. X            next->uplink = temp;
  1512. X            left = temp;
  1513. X            next = temp->rightlink;
  1514. X            if( next == NULL )
  1515. X        {
  1516. X                right->leftlink = NULL;
  1517. X                goto done;    /* job done, entire tree split */
  1518. X            }
  1519. X
  1520. X    } while( STRCMP( next->key, key ) < 0 );    /* change sides */
  1521. X
  1522. X    two:    /* assert next->key >= key */
  1523. X
  1524. X    do     /* walk to the left in the right tree */
  1525. X    {
  1526. X            temp = next->leftlink;
  1527. X            if( temp == NULL )
  1528. X        {
  1529. X                right->leftlink = next;
  1530. X                next->uplink = right;
  1531. X                left->rightlink = NULL;
  1532. X                goto done;    /* job done, entire tree split */
  1533. X            }
  1534. X            if( STRCMP( temp->key, key ) < 0 )
  1535. X        {
  1536. X                right->leftlink = next;
  1537. X                next->uplink = right;
  1538. X                right = next;
  1539. X                next = temp;
  1540. X                goto one;    /* change sides */
  1541. X            }
  1542. X            next->leftlink = temp->rightlink;
  1543. X            if( temp->rightlink != NULL )
  1544. X        temp->rightlink->uplink = next;
  1545. X            right->leftlink = temp;
  1546. X            temp->uplink = right;
  1547. X            temp->rightlink = next;
  1548. X            next->uplink = temp;
  1549. X            right = temp;
  1550. X            next = temp->leftlink;
  1551. X            if( next == NULL )
  1552. X        {
  1553. X                left->rightlink = NULL;
  1554. X                goto done;    /* job done, entire tree split */
  1555. X            }
  1556. X
  1557. X    } while( STRCMP( next->key, key ) >= 0 );    /* change sides */
  1558. X
  1559. X        goto one;
  1560. X
  1561. X    done:    /* split is done, branches of n need reversal */
  1562. X
  1563. X        temp = n->leftlink;
  1564. X        n->leftlink = n->rightlink;
  1565. X        n->rightlink = temp;
  1566. X    }
  1567. X
  1568. X    return( n );
  1569. X
  1570. X} /* spenqprior */
  1571. X
  1572. X/*----------------
  1573. X *
  1574. X *  splay() -- reorganize the tree.
  1575. X *
  1576. X *  the tree is reorganized so that n is the root of the
  1577. X *  splay tree representing q; results are unpredictable if n is not
  1578. X *  in q to start with; q is split from n up to the old root, with all
  1579. X *  nodes to the left of n ending up in the left subtree, and all nodes
  1580. X *  to the right of n ending up in the right subtree; the left branch of
  1581. X *  the right subtree and the right branch of the left subtree are
  1582. X *  shortened in the process
  1583. X *
  1584. X *  this code assumes that n is not NULL and is in q; it can sometimes
  1585. X *  detect n not in q and complain
  1586. X */
  1587. X
  1588. Xvoid
  1589. Xsplay( n, q )
  1590. X
  1591. Xregister SPBLK * n;
  1592. XSPTREE * q;
  1593. X
  1594. X{
  1595. X    register SPBLK * up;    /* points to the node being dealt with */
  1596. X    register SPBLK * prev;    /* a descendent of up, already dealt with */
  1597. X    register SPBLK * upup;    /* the parent of up */
  1598. X    register SPBLK * upupup;    /* the grandparent of up */
  1599. X    register SPBLK * left;    /* the top of left subtree being built */
  1600. X    register SPBLK * right;    /* the top of right subtree being built */
  1601. X
  1602. X    left = n->leftlink;
  1603. X    right = n->rightlink;
  1604. X    prev = n;
  1605. X    up = prev->uplink;
  1606. X
  1607. X    q->splays++;
  1608. X
  1609. X    while( up != NULL )
  1610. X    {
  1611. X    q->splayloops++;
  1612. X
  1613. X        /* walk up the tree towards the root, splaying all to the left of
  1614. X       n into the left subtree, all to right into the right subtree */
  1615. X
  1616. X        upup = up->uplink;
  1617. X        if( up->leftlink == prev )    /* up is to the right of n */
  1618. X    {
  1619. X            if( upup != NULL && upup->leftlink == up )  /* rotate */
  1620. X        {
  1621. X                upupup = upup->uplink;
  1622. X                upup->leftlink = up->rightlink;
  1623. X                if( upup->leftlink != NULL )
  1624. X            upup->leftlink->uplink = upup;
  1625. X                up->rightlink = upup;
  1626. X                upup->uplink = up;
  1627. X                if( upupup == NULL )
  1628. X            q->root = up;
  1629. X        else if( upupup->leftlink == upup )
  1630. X            upupup->leftlink = up;
  1631. X        else
  1632. X            upupup->rightlink = up;
  1633. X                up->uplink = upupup;
  1634. X                upup = upupup;
  1635. X            }
  1636. X            up->leftlink = right;
  1637. X            if( right != NULL )
  1638. X        right->uplink = up;
  1639. X            right = up;
  1640. X
  1641. X        }
  1642. X    else                /* up is to the left of n */
  1643. X    {
  1644. X            if( upup != NULL && upup->rightlink == up )    /* rotate */
  1645. X        {
  1646. X                upupup = upup->uplink;
  1647. X                upup->rightlink = up->leftlink;
  1648. X                if( upup->rightlink != NULL )
  1649. X            upup->rightlink->uplink = upup;
  1650. X                up->leftlink = upup;
  1651. X                upup->uplink = up;
  1652. X                if( upupup == NULL )
  1653. X            q->root = up;
  1654. X        else if( upupup->rightlink == upup )
  1655. X            upupup->rightlink = up;
  1656. X        else
  1657. X            upupup->leftlink = up;
  1658. X                up->uplink = upupup;
  1659. X                upup = upupup;
  1660. X            }
  1661. X            up->rightlink = left;
  1662. X            if( left != NULL )
  1663. X        left->uplink = up;
  1664. X            left = up;
  1665. X        }
  1666. X        prev = up;
  1667. X        up = upup;
  1668. X    }
  1669. X
  1670. X# ifdef DEBUG
  1671. X    if( q->root != prev )
  1672. X    {
  1673. X/*    fprintf(stderr, " *** bug in splay: n not in q *** " ); */
  1674. X    abort();
  1675. X    }
  1676. X# endif
  1677. X
  1678. X    n->leftlink = left;
  1679. X    n->rightlink = right;
  1680. X    if( left != NULL )
  1681. X    left->uplink = n;
  1682. X    if( right != NULL )
  1683. X    right->uplink = n;
  1684. X    q->root = n;
  1685. X    n->uplink = NULL;
  1686. X
  1687. X} /* splay */
  1688. X
  1689. SHAR_EOF
  1690. if test -f 'sptree.h'
  1691. then
  1692.     echo shar: over-writing existing file "'sptree.h'"
  1693. fi
  1694. sed 's/^X//' << \SHAR_EOF > 'sptree.h'
  1695. X/*
  1696. X** sptree.h:  The following type declarations provide the binary tree
  1697. X**  representation of event-sets or priority queues needed by splay trees
  1698. X**
  1699. X**  assumes that data and datb will be provided by the application
  1700. X**  to hold all application specific information
  1701. X**
  1702. X**  assumes that key will be provided by the application, comparable
  1703. X**  with the compare function applied to the addresses of two keys.
  1704. X*/
  1705. X
  1706. X# ifndef SPTREE_H
  1707. X# define SPTREE_H
  1708. X
  1709. X# ifndef NULL
  1710. X# define NULL    0
  1711. X# endif
  1712. X
  1713. X# define STRCMP( a, b ) ( (Sct = *(a) - *(b)) ? Sct : strcmp( (a), (b) ) )
  1714. X
  1715. Xtypedef struct _spblk SPBLK;
  1716. X
  1717. Xtypedef struct _spblk
  1718. X{
  1719. X    SPBLK    * leftlink;
  1720. X    SPBLK    * rightlink;
  1721. X    SPBLK    * uplink;
  1722. X
  1723. X    char    * key;        /* formerly time/timetyp */
  1724. X    char    * data;        /* formerly aux/auxtype */
  1725. X    char    * datb;
  1726. X};
  1727. X
  1728. Xtypedef struct
  1729. X{
  1730. X    SPBLK    * root;        /* root node */
  1731. X
  1732. X    /* Statistics, not strictly necessary, but handy for tuning  */
  1733. X
  1734. X    int        lookups;    /* number of splookup()s */
  1735. X    int        lkpcmps;    /* number of lookup comparisons */
  1736. X    
  1737. X    int        enqs;        /* number of spenq()s */
  1738. X    int        enqcmps;    /* compares in spenq */
  1739. X    
  1740. X    int        splays;
  1741. X    int        splayloops;
  1742. X
  1743. X} SPTREE;
  1744. X
  1745. X
  1746. X/* sptree.c */
  1747. Xextern SPTREE * spinit();    /* init tree */
  1748. Xextern int spempty();        /* is tree empty? */
  1749. Xextern SPBLK * spenq();        /* insert item into the tree */
  1750. Xextern SPBLK * spdeq();        /* return and remove lowest item in subtree */
  1751. Xextern SPBLK * spenqprior();    /* insert before items with same key */
  1752. Xextern void splay();        /* reorganize tree */
  1753. X
  1754. X/* spaux.c */
  1755. Xextern SPBLK * sphead();    /* return first node in tree */
  1756. Xextern void spdelete();        /* delete node from tree */
  1757. Xextern SPBLK * spnext();    /* return next node in tree */
  1758. Xextern SPBLK * spprev();    /* return previous node in tree */
  1759. Xextern SPBLK * spenqbefore();    /* enqueue before existing node */
  1760. Xextern SPBLK * spenqafter();    /* enqueue after existing node */
  1761. X
  1762. X/* spdaveb.c */
  1763. Xextern SPBLK * splookup();    /* find key in a tree */
  1764. Xextern SPBLK * spinstall();    /* enter an item, allocating or replacing */
  1765. Xextern SPBLK * sptail();    /* find end of a tree */
  1766. Xextern void spscan();        /* scan forward through tree */
  1767. Xextern void sprscan();        /* reverse scan through tree */
  1768. Xextern SPBLK * spfnext();    /* fast non-splaying next */
  1769. Xextern SPBLK * spfprev();    /* fast non-splaying prev */
  1770. X
  1771. X# endif
  1772. SHAR_EOF
  1773. #    End of shell archive
  1774. exit 0
  1775.