/algorithms/avl_insert.tex

http://github.com/fmela/libdict · LaTeX · 151 lines · 122 code · 26 blank · 3 comment · 0 complexity · 68bdd282f5069847d2389611f0080404 MD5 · raw file

  1. % avl_insert.tex
  2. % 11-19-2001
  3. \nopagenumbers
  4. \parskip=2pt
  5. \parindent=0pt
  6. {\sl An Iterative Algorithm for AVL Tree Insertion
  7. \hfill \rm Farooq Mela $<$farooq.mela@gmail.com$>$}
  8. \medskip
  9. \noindent {\bf Algorithm A} ({\it AVL Tree Insertion}).
  10. Given a set of nodes which form an AVL tree {\tt T}, and a key to insert
  11. {\tt K}, this algorithm will insert the node into the tree while maintaining
  12. the tree's balance properties. Each node is assumed to contain {\tt KEY},
  13. {\tt BAL}, {\tt LLINK}, {\tt RLINK}, and {\tt PARENT} fields. For any given
  14. node {\tt N}, {\tt KEY(N)} gives the key field of {\tt N}, {\tt BAL(N)} gives
  15. the balance field of {\tt N}, {\tt LLINK(N)} and {\tt RLINK(N)} are pointers to
  16. {\tt N}'s left and right subtrees, respectively, and {\tt PARENT(N)} is a
  17. pointer to the node of which {\tt N} is a subtree. Any or all of these three
  18. link fields may be $\Lambda$, which for {\tt LLINK(N)} and {\tt RLINK(N)}
  19. indicates that {\tt N} has no left or right subtree, respectively, and for
  20. {\tt PARENT(N)} indicates that {\tt N} is the root of the tree. The {\tt BAL}
  21. field must be able to represent an integer on the range $[-2, +2]$. The tree
  22. has a field {\tt ROOT} which is a pointer to the root node of the tree.
  23. \medskip
  24. You can find an implementation of this algorithm, as well as many others, in
  25. {\bf libdict}, which is available on the web at
  26. {\tt http://github.com/fmela/libdict}.
  27. \medskip
  28. \parindent=36pt
  29. \item{\bf A1.} [Initialize.]
  30. Set ${\tt N} \gets {\tt ROOT(T)}$, ${\tt P} \gets {\tt Q}\gets \Lambda$.
  31. \item{\bf A2.} [Find insertion point.]
  32. If ${\tt N} = \Lambda$, go to step A3.
  33. If ${\tt K} = {\tt KEY(N)}$,
  34. the key is already in the tree and the algorithm terminates with an error.
  35. Set ${\tt P} \gets {\tt N}$;
  36. if ${\tt BAL(P)} \neq 0$, set ${\tt Q} \gets {\tt P}$.
  37. If ${\tt K} < {\tt KEY(N)}$,
  38. then set ${\tt N} \gets {\tt LLINK(N)}$,
  39. otherwise set ${\tt N} \gets {\tt RLINK(N)}$. Repeat this step.
  40. \item{\bf A3.} [Insert.]
  41. Set ${\tt N} \gets {\tt AVAIL}$.
  42. If ${\tt N} = \Lambda$, the algorithm terminates with an out of memory error.
  43. Set ${\tt KEY(N)} \gets {\tt K}$,
  44. ${\tt LLINK(N)} \gets {\tt RLINK(N)} \gets \Lambda$,
  45. ${\tt PARENT(N)} \gets {\tt P}$, and
  46. ${\tt BAL(N)} \gets 0$.
  47. If ${\tt P} = \Lambda$, set ${\tt ROOT(T)} \gets {\tt N}$, and go to step A8.
  48. If ${\tt K} < {\tt KEY(P)}$,
  49. set ${\tt LLINK(P)} \gets {\tt N}$; otherwise,
  50. set ${\tt RLINK(P)} \gets {\tt N}$.
  51. \item{\bf A4.} [Adjust balance factors.]
  52. If ${\tt P} = {\tt Q}$, go to step A5.
  53. If ${\tt LLINK(P)} = {\tt N}$,
  54. set ${\tt BAL(P)} \gets -1$; otherwise,
  55. set ${\tt BAL(P)} \gets +1$.
  56. Then set ${\tt N} \gets {\tt P}$, and ${\tt P} \gets {\tt PARENT(P)}$,
  57. and repeat this step.
  58. \item{\bf A5.} [Check for imbalance.] If ${\tt Q} = \Lambda$, go to step A8.
  59. Otherwise:
  60. \itemitem{i.} If ${\tt LLINK(Q)} = {\tt N}$,
  61. set ${\tt BAL(Q)} \gets {\tt BAL(Q)} - 1$.
  62. If ${\tt BAL(Q)} = -2$, go to step A6, otherwise, go to step A8.
  63. \itemitem{ii.} If ${\tt RLINK(Q)} = {\tt N}$,
  64. set ${\tt BAL(Q)} \gets {\tt BAL(Q)} + 1$.
  65. If ${\tt BAL(Q)} = +2$, go to step A7, otherwise, go to step A8.
  66. \item{\bf A6.} [Left imbalance.]
  67. If ${\tt BAL(LLINK(Q))} > 0$, rotate {\tt LLINK(Q)} left. Rotate {\tt Q} right.
  68. Go to step A8.
  69. \item{\bf A7.} [Right imbalance.]
  70. If ${\tt BAL(RLINK(Q))} < 0$, rotate {\tt RLINK(Q)} right. Rotate {\tt Q} left.
  71. Go to step A8.
  72. \item{\bf A8.} [All done.] The algorithm terminates successfully.
  73. \medskip
  74. \parindent=0pt
  75. {\bf Rotations in AVL Trees}
  76. \noindent {\bf Algorithm R} ({\it Right Rotation}).
  77. Given a tree {\tt T} and a node in the tree {\tt N}, this routine will rotate
  78. {\tt N} right.
  79. \parindent=36pt
  80. \item{\bf R1.} [Do the rotation.]
  81. Set
  82. ${\tt L} \gets {\tt LLINK(N)}$ and
  83. ${\tt LLINK(N)} \gets {\tt RLINK(L)}$.
  84. If ${\tt RLINK(L)} \neq \Lambda$,
  85. then set ${\tt PARENT(RLINK(L))} \gets {\tt N}$.
  86. Set ${\tt P} \gets {\tt PARENT(N)}$, ${\tt PARENT(L)} \gets {\tt P}$.
  87. If ${\tt P} = \Lambda$, then set ${\tt ROOT(T)} \gets {\tt L}$;
  88. if ${\tt P} \neq \Lambda$ and ${\tt LLINK(P)} = {\tt N}$,
  89. set ${\tt LLINK(P)} \gets {\tt L}$, otherwise
  90. set ${\tt RLINK(P)} \gets {\tt L}$.
  91. Finally, set ${\tt RLINK(L)} \gets {\tt N}$,
  92. and ${\tt PARENT(N)} \gets {\tt L}$.
  93. \item{\bf R2.} [Recompute balance factors.]
  94. Set
  95. ${\tt BAL(N)} \gets {\tt BAL(N)} + (1 - {\tt MIN(BAL(L), 0)})$,
  96. ${\tt BAL(L)} \gets {\tt BAL(L)} + (1 + {\tt MAX(BAL(N), 0)})$.
  97. \parindent=0pt
  98. \medskip
  99. The code for a left rotation is symmetric. At the risk of being repetitive, it
  100. appears below.
  101. \medskip
  102. \noindent {\bf Algorithm L} ({\it Left Rotation}).
  103. Given a tree {\tt T} and a node in the tree {\tt N}, this routine will rotate
  104. {\tt N} left.
  105. \parindent=36pt
  106. \item{\bf L1.} [Do the rotation.]
  107. Set
  108. ${\tt R} \gets {\tt RLINK(N)}$ and
  109. ${\tt RLINK(N)} \gets {\tt LLINK(R)}$.
  110. If ${\tt LLINK(R)} \neq \Lambda$,
  111. then set ${\tt PARENT(LLINK(R))} \gets {\tt N}$.
  112. Set ${\tt P} \gets {\tt PARENT(N)}$, ${\tt PARENT(R)} \gets {\tt P}$.
  113. If ${\tt P} = \Lambda$, then set ${\tt ROOT(T)} \gets {\tt R}$;
  114. if ${\tt P} \neq \Lambda$ and ${\tt LLINK(P)} = {\tt N}$,
  115. set ${\tt LLINK(P)} \gets {\tt R}$, otherwise
  116. set ${\tt RLINK(P)} \gets {\tt R}$.
  117. Finally, set ${\tt LLINK(R)} \gets {\tt N}$,
  118. and ${\tt PARENT(N)} \gets {\tt R}$.
  119. \item{\bf L2.} [Recompute balance factors.]
  120. Set
  121. ${\tt BAL(N)} \gets {\tt BAL(N)} - (1 + {\tt MAX(BAL(R), 0)})$,
  122. ${\tt BAL(R)} \gets {\tt BAL(R)} - (1 - {\tt MIN(BAL(N), 0)})$.
  123. \parindent=0pt
  124. \bye
  125. % EOF