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