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