/algorithms/treap_insert.tex

http://github.com/fmela/libdict · LaTeX · 118 lines · 96 code · 19 blank · 3 comment · 0 complexity · c39de380ff784a79063a6da6498f27b9 MD5 · raw file

  1. % treap_insert.tex
  2. % 11-21-2001
  3. \nopagenumbers
  4. \parskip=2pt
  5. \parindent=0pt
  6. {\sl An Iterative Algorithm for Treap Insertion
  7. \hfill \rm Farooq Mela $<$farooq.mela@gmail.com$>$}
  8. \medskip
  9. \noindent {\bf Algorithm I} ({\it Treap Insertion}).
  10. Given a set of nodes which form a treap {\tt T}, and a key to insert
  11. {\tt K}, this algorithm will insert the node into the treap while maintaining
  12. it's heap properties. Each node is assumed to contain {\tt KEY}, {\tt PRIO},
  13. {\tt LLINK}, {\tt RLINK}, and {\tt PARENT} fields. For any given node {\tt N},
  14. {\tt KEY(N)} gives the key field of {\tt N}, {\tt PRIO(N)} gives the priority
  15. field of {\tt N}, {\tt LLINK(N)} and {\tt RLINK(N)} are pointers to {\tt N}'s
  16. left and right subtrees, respectively, and {\tt PARENT(N)} is a pointer to the
  17. node of which {\tt N} is a subtree. Any or all of these three link fields may
  18. be $\Lambda$, which for {\tt LLINK(N)} and {\tt RLINK(N)} indicates that {\tt
  19. N} has no left or right subtree, respectively, and for {\tt PARENT(N)}
  20. indicates that {\tt N} is the root of the treap. The treap has a field
  21. {\tt ROOT} which is a pointer to the root node of the treap.
  22. \medskip
  23. You can find an implementation of this algorithm, as well as many others, in
  24. {\bf libdict}, which is available on the web at
  25. {\tt http://github.com/fmela/libdict}.
  26. \medskip
  27. \parindent=36pt
  28. \item{\bf I1.} [Initialize.]
  29. Set ${\tt N} \gets {\tt ROOT(T)}$, ${\tt P} \gets \Lambda$.
  30. \item{\bf I2.} [Find insertion point.]
  31. If ${\tt N} = \Lambda$, go to step I3.
  32. If ${\tt K} = {\tt KEY(N)}$,
  33. the key is already in the treap and the algorithm terminates with an error.
  34. Set ${\tt P} \gets {\tt N}$;
  35. if ${\tt K} < {\tt KEY(N)}$,
  36. then set ${\tt N} \gets {\tt LLINK(N)}$,
  37. otherwise set ${\tt N} \gets {\tt RLINK(N)}$. Repeat this step.
  38. \item{\bf I3.} [Insert.]
  39. Set ${\tt N} \gets {\tt AVAIL}$.
  40. If ${\tt N} = \Lambda$, the algorithm terminates with an out of memory error.
  41. Set ${\tt KEY(N)} \gets {\tt K}$,
  42. ${\tt LLINK(N)} \gets {\tt RLINK(N)} \gets \Lambda$, and
  43. ${\tt PARENT(N)} \gets {\tt P}$.
  44. Set {\tt PRIO(N)} equal to a random integer.
  45. If ${\tt P} = \Lambda$, set ${\tt ROOT(T)} \gets {\tt N}$, and go to step I5.
  46. If ${\tt K} < {\tt KEY(P)}$,
  47. set ${\tt LLINK(P)} \gets {\tt N}$; otherwise,
  48. set ${\tt RLINK(P)} \gets {\tt N}$.
  49. \item{\bf I4.} [Sift up.]
  50. If ${\tt P} = \Lambda$ or ${\tt PRIO(P)} \leq {\tt PRIO(N)}$, go to step I5.
  51. If ${\tt LLINK(P)} = {\tt N}$, rotate {\tt P} right; otherwise,
  52. rotate {\tt P} left. Then set ${\tt P} \gets {\tt PARENT(N)}$, and repeat this
  53. step.
  54. \item{\bf I5.} [All done.] The algorithm terminates successfully.
  55. \medskip
  56. \parindent=0pt
  57. {\bf Rotations}
  58. \noindent {\bf Algorithm R} ({\it Right Rotation}).
  59. Given a treap {\tt T} and a node in the treap {\tt N}, this routine will rotate
  60. {\tt N} right.
  61. \parindent=36pt
  62. \item{\bf R1.} [Do the rotation.]
  63. Set
  64. ${\tt L} \gets {\tt LLINK(N)}$ and
  65. ${\tt LLINK(N)} \gets {\tt RLINK(L)}$.
  66. If ${\tt RLINK(L)} \neq \Lambda$,
  67. then set ${\tt PARENT(RLINK(L))} \gets {\tt N}$.
  68. Set ${\tt P} \gets {\tt PARENT(N)}$, ${\tt PARENT(L)} \gets {\tt P}$.
  69. If ${\tt P} = \Lambda$, then set ${\tt ROOT(T)} \gets {\tt L}$;
  70. if ${\tt P} \neq \Lambda$ and ${\tt LLINK(P)} = {\tt N}$,
  71. set ${\tt LLINK(P)} \gets {\tt L}$, otherwise
  72. set ${\tt RLINK(P)} \gets {\tt L}$.
  73. Finally, set ${\tt RLINK(L)} \gets {\tt N}$,
  74. and ${\tt PARENT(N)} \gets {\tt L}$.
  75. \parindent=0pt
  76. \medskip
  77. The code for a left rotation is symmetric. At the risk of being repetitive, it
  78. appears below.
  79. \medskip
  80. \noindent {\bf Algorithm L} ({\it Left Rotation}).
  81. Given a treap {\tt T} and a node in the treap {\tt N}, this routine will rotate
  82. {\tt N} left.
  83. \parindent=36pt
  84. \item{\bf L1.} [Do the rotation.]
  85. Set
  86. ${\tt R} \gets {\tt RLINK(N)}$ and
  87. ${\tt RLINK(N)} \gets {\tt LLINK(R)}$.
  88. If ${\tt LLINK(R)} \neq \Lambda$,
  89. then set ${\tt PARENT(LLINK(R))} \gets {\tt N}$.
  90. Set ${\tt P} \gets {\tt PARENT(N)}$, ${\tt PARENT(R)} \gets {\tt P}$.
  91. If ${\tt P} = \Lambda$, then set ${\tt ROOT(T)} \gets {\tt R}$;
  92. if ${\tt P} \neq \Lambda$ and ${\tt LLINK(P)} = {\tt N}$,
  93. set ${\tt LLINK(P)} \gets {\tt R}$, otherwise
  94. set ${\tt RLINK(P)} \gets {\tt R}$.
  95. Finally, set ${\tt LLINK(R)} \gets {\tt N}$,
  96. and ${\tt PARENT(N)} \gets {\tt R}$.
  97. \parindent=0pt
  98. \bye
  99. % EOF