home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1868 / sptree.3 < prev    next >
Encoding:
Text File  |  1990-12-28  |  7.5 KB  |  373 lines

  1.  
  2. TH SPTREE 3  "10 February 1988"
  3. .UC 4
  4. .SH NAME
  5. spdelete, spdeq, spempty, spenq, spenqafter, spenqbefore, spenqprior,
  6. spfhead, spfnext, spfprev, spftail, sphead, spinit, spinstall, splay,
  7. splookup, spnext, spprev, sprscan, spscan, spstats \- splay tree operations
  8. .SH SYNOPSIS
  9. .nf
  10. .B #include "sptree.h"
  11. .PP
  12. .B void spdelete(n, q)
  13. .B SPBLK *n;
  14. .B SPTREE *q;
  15. .PP
  16. .B SPBLK *spdeq(np)
  17. .B SPBLK **np;
  18. .PP
  19. .B int spempty(q)
  20. .B SPTREE *q;
  21. .PP
  22. .B SPBLK *spenq(n, q)
  23. .B SPBLK *n;
  24. .B SPTREE *q;
  25. .PP
  26. .B SPBLK *spenqafter(n, n1, q)
  27. .B SPBLK *n, *n1;
  28. .B SPTREE *q;
  29. .PP
  30. .B SPBLK *spenqbefore(n, n1, q)
  31. .B SPBLK *n, *n1;
  32. .B SPTREE *q;
  33. .PP
  34. .B SPBLK *spenqprior(n, q)
  35. .B SPBLK *n;
  36. .B SPTREE *q;
  37. .PP
  38. .B SPBLK *spfhead(q)
  39. .B SPTREE *q;
  40. .PP
  41. .B SPBLK *spfnext(n)
  42. .B SPBLK *n;
  43. .PP
  44. .B SPBLK *spfprev(n)
  45. .B SPBLK *n;
  46. .PP
  47. .B SPBLK *spftail(q)
  48. .B SPTREE *q;
  49. .PP
  50. .B SPBLK *sphead(q)
  51. .B SPTREE *q;
  52. .PP
  53. .B SPTREE *spinit();
  54. .PP
  55. .B SPBLK *spinstall(key, data, datb, q)
  56. .B char *key, *data, *datb;
  57. .B SPTREE *q;
  58. .PP
  59. .B void splay(n, q)
  60. .B SPBLK *n;
  61. .B SPTREE *q;
  62. .PP
  63. .B SPBLK *splookup(key, q)
  64. .B char *key;
  65. .B SPTREE *q;
  66. .PP
  67. .B SPBLK *spnext(n, q)
  68. .B SPBLK *n;
  69. .B SPTREE *q;
  70. .PP
  71. .B SPBLK *spprev(n, q)
  72. .B SPBLK *n;
  73. .B SPTREE *q;
  74. .PP
  75. .B void sprscan(f, n, q)
  76. .B int (*f)();
  77. .B SPBLK *n;
  78. .B SPTREE *q;
  79. .PP
  80. .B void spscan(f, n, q)
  81. .B int (*f)();
  82. .B SPBLK *n;
  83. .B SPTREE *q;
  84. .PP
  85. .B char *spstats(q)
  86. .B SPTREE *q;
  87. .PP
  88. .fi
  89. .SH DESCRIPTION
  90. These functions operate on an event\-set or priority\-queue implemented
  91. using splay trees.  These are similar to avl\-trees, but are not
  92. concerned with keeping the tree strictly balanced.  Instead, the tree is
  93. dynamically reorganized in a simple way that yields a good amortized
  94. bound at the expense of worst case performance.
  95. .PP
  96. The SPTREE structure declared in sptree.h should only be handled
  97. indirectly.  A pointer to an SPTREE is returned by
  98. .I spinit
  99. and should be handed blindly to other access functions.
  100. .PP
  101. The nodes in a splay tree are defined by the following structure,
  102. declared in sptree.h.
  103. .PP
  104. .nf
  105. typedef struct _spblk SPBLK;
  106. typedef struct _spblk
  107. {
  108.     .
  109.     .
  110.     .
  111.  
  112.     char    *key;
  113.     char    *data;
  114.     char    *datb;
  115. };
  116. .fi
  117. .PP
  118. You should only refer to the
  119. .I key,
  120. .I data
  121. and
  122. .I datb
  123. members.
  124. .PP
  125. The
  126. .I key
  127. is interpreted as a pointer to a null terminated string, and ordering is
  128. determined by calls to the usual
  129. .I strcmp
  130. routine.
  131. .PP
  132. No meaning is associated with the auxiliary members
  133. .I data
  134. or
  135. .I datb,
  136. and you are free to stuff them with whatever good conscience and a legal
  137. cast will allow.
  138. .PP
  139. .I Spdelete
  140. deletes the node
  141. .I n
  142. from the tree
  143. .I q.
  144. The resulting tree is splayed around a new root, which is the successor
  145. to
  146. .I n.
  147. .PP
  148. .I Spdeq
  149. removes and returns the head node from the sub\-tree rooted at node
  150. .I n.
  151. .PP
  152. .I Spempty
  153. returns non\-zero if the tree
  154. .I q
  155. has no members.
  156. .PP
  157. .I Spenq
  158. inserts node
  159. .I n
  160. into tree
  161. .I q
  162. after all other nodes with the same key.  When this is done,
  163. .I n
  164. will be the root of the tree.
  165. .PP
  166. .I Spenqafter
  167. inserts node
  168. .I n
  169. as the immediate sucessor of node
  170. .I n1
  171. in tree
  172. .I q.
  173. In so doing,
  174. .I n1
  175. becomes the root of the tree with
  176. .I n
  177. as its right son.
  178. .PP
  179. .I Spenqbefore
  180. inserts node
  181. .I n
  182. as the immediate predecessor of node
  183. .I n1
  184. in tree
  185. .I q.
  186. In doing so,
  187. .I n1
  188. becomes the root of the tree with
  189. .I n
  190. as its left son.
  191. .PP
  192. .I Spenqprior
  193. inserts node
  194. .I n
  195. into the tree
  196. .I q
  197. before all other nodes with the same key; after this is done,
  198. .I n
  199. will be the root of the tree.
  200. .PP
  201. .I Spfhead
  202. returns a pointer to the head element in the tree
  203. .I q,
  204. but does not splay it to the root.
  205. .PP
  206. .I Spfnext
  207. returns a pointer to the immediate successor of node
  208. .I n
  209. without doing any reorganization.
  210. .PP
  211. .I Spfprev
  212. returns a pointer to the immediate predecessor of node
  213. .I n
  214. without doing any reoganization.
  215. .PP
  216. .I Spftail
  217. returns a reference to the last node in the tree
  218. .I q
  219. without doing any reorganization.
  220. .PP
  221. .I Sphead
  222. returns a pointer to the head event in the tree
  223. .I q.
  224. The returned node is made the root of the tree, as if
  225. .I q
  226. had been splayed around
  227. .I n.
  228. .PP
  229. .I Spinit
  230. creates a new splay tree using a \fImalloc\fP\-like routine named
  231. .I emalloc
  232. that must be supplied by the user.
  233. .PP
  234. .I Spinstall
  235. inserts an entry with the key value pointed to by
  236. .I key
  237. with the auxiliary values
  238. .I data
  239. and
  240. .I datb
  241. into the tree
  242. .I q.
  243. If a node with the key value already exists, its auxiliarly values are
  244. replaced.  If the node does not already exist, a new one is allocated
  245. with \fImalloc\fP\-like function named
  246. .I emalloc
  247. that must be supplied by the user.
  248. .PP
  249. .I Splay
  250. reorganizes the tree so that node
  251. .I n
  252. becomes the root of the tree in
  253. .I q.
  254. Results are unpredicatable if
  255. .I n
  256. is not in
  257. .I q
  258. to start with.
  259. .I Q
  260. is split from
  261. .I n
  262. up to the old root, with all nodes to the left of
  263. .I n
  264. ending up in the left sub\-tree, and all nodes to the right of
  265. .I n
  266. ending up in the right sub\-tree.  The left branch of the right
  267. sub\-tree and the right branch of the left sub\-tree are shortened in
  268. the process.
  269. .PP
  270. .I Splookup
  271. searches for a node containing the key value pointed to by
  272. .I key
  273. in the tree
  274. .I q.
  275. A found node is splayed to the root and returned.  If the key is not
  276. found, the function returns NULL and no reorganization is done.
  277. .PP
  278. .I
  279. Spnext returns a pointer to the successor of
  280. .I n
  281. in
  282. .I q.
  283. The successor becomes the root of the tree.
  284. .PP
  285. .I Spprev
  286. returns the predecessor of
  287. .I n
  288. in
  289. .I q.
  290. The predecessor becomes the root.
  291. .PP
  292. .I Sprscan
  293. applies the function
  294. .I f
  295. starting at node
  296. .I n
  297. to the members of the tree
  298. .I q
  299. in reverse order.  If
  300. .I n
  301. is NULL, then the scan starts at the tail of the tree.  The tree is not
  302. reorganized during the reverse scan.  The function is called with one
  303. argument, a pointer to an SPBLK.  Its return value is ignored.
  304. .PP
  305. .I Spscan
  306. applies the function
  307. .I f
  308. starting at node
  309. .I n
  310. in tree
  311. .I q
  312. and all successive nodes, in order.  If
  313. .I n
  314. is NULL, then the scan starts at the head of the tree.  The tree is not
  315. reorganized during the scan.  The function is called with one argument,
  316. a pointer to an SPBLK.  Its return value is ignored.
  317. .PP
  318. .I Spstats
  319. returns a string of statistics on the activities in the tree
  320. .I q.
  321. It shows how many times
  322. .I splookup
  323. was called, and how many comparisons were needed per call,
  324. the number of nodes that have been added with
  325. .I spenq
  326. and the number of comparisons needed per call, and finally, the number
  327. of
  328. .I splay
  329. operations performed, and the number of loops done in each splay.  These
  330. statistics give an indication of the average effective depth of the tree
  331. for various operations.  The function returns a pointer to a static
  332. buffer that is overwritten with each call.
  333. .SH AUTHORS
  334. The code was originally written in Pascal by Douglas W. Jones
  335. (jones@cs.uiowa.edu) with assistance from Srinivas R. Sataluri. 
  336. It was translated to C with some new functions by Dave Brower
  337. (daveb@rtech.uucp).  Bug fixes by Mark Moraes
  338. (moraes@csri.toronto.edu) are gratefully appreciated.
  339. .SH REFERENCES
  340. The basic splay tree algorithms were originally presented in:
  341. .PP
  342. .nf
  343.   Self Adjusting Binary Trees,
  344.   by D. D. Sleator and R. E. Tarjan,
  345.   Proc. ACM SIGACT Symposium on Theory
  346.   of Computing (Boston, Apr 1983) 235-245.
  347. .fi
  348. .PP
  349. More operations on priority queues were added to help support discrete
  350. event simulation.  See, for example Chapter 14 of
  351. .PP
  352. .nf
  353.   Introduction to Simula 67,
  354.   by Gunther Lamprecht,
  355.   Vieweg & Sohn, Braucschweig, Wiesbaden, 1981.
  356. .fi
  357. .PP
  358. and Chapter 9 of
  359. .PP
  360. .nf
  361.   Simula Begin,
  362.   by Graham M. Birtwistle, et al,
  363.   Studentlitteratur, Lund, 1979.
  364. .fi
  365. .PP
  366. Splay trees are compared with other data structures in
  367. .PP
  368. .nf
  369.   An Empirical Comparison of Priority-Queue and Event-Set Implementations,
  370.   by Douglas W. Jones,
  371.   Comm. ACM 29, 4 (Apr. 1986) 300-311.
  372. .fi
  373.