annotate NGSrich_0.5.5/src/datastructures/AVLTree.java @ 0:89ad0a9cca52 default tip

Uploaded
author pfrommolt
date Mon, 21 Nov 2011 08:12:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
1 package datastructures;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
2
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
3 // BinarySearchTree class
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
4 //
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
5 // CONSTRUCTION: with no initializer
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
6 //
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
7 // ******************PUBLIC OPERATIONS*********************
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
8 // void insert( x ) --> Insert x
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
9 // void remove( x ) --> Remove x (unimplemented)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
10 // Comparable find( x ) --> Return item that matches x
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
11 // Comparable findMin( ) --> Return smallest item
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
12 // Comparable findMax( ) --> Return largest item
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
13 // boolean isEmpty( ) --> Return true if empty; else false
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
14 // void makeEmpty( ) --> Remove all items
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
15 // void printTree( ) --> Print tree in sorted order
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
16
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
17 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
18 * Implements an AVL tree.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
19 * Note that all "matching" is based on the compareTo method.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
20 * @author Mark Allen Weiss
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
21 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
22 public class AVLTree
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
23 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
24 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
25 * Construct the tree.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
26 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
27 public AVLTree( )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
28 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
29 root = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
30 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
31
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
32 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
33 * Insert into the tree; duplicates are ignored.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
34 * @param x the item to insert.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
35 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
36 @SuppressWarnings("unchecked")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
37 public void insert(Comparable x )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
38 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
39 root = insert( x, root );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
40 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
41
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
42 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
43 * Remove from the tree. Nothing is done if x is not found.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
44 * @param x the item to remove.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
45 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
46 @SuppressWarnings("unchecked")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
47 public void remove( Comparable x )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
48 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
49 System.out.println( "Sorry, remove unimplemented" );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
50 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
51
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
52 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
53 * Find the smallest item in the tree.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
54 * @return smallest item or null if empty.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
55 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
56 @SuppressWarnings("unchecked")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
57 public Comparable findMin( )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
58 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
59 return elementAt( findMin( root ) );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
60 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
61
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
62 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
63 * Find the largest item in the tree.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
64 * @return the largest item of null if empty.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
65 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
66 @SuppressWarnings("unchecked")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
67 public Comparable findMax( )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
68 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
69 return elementAt( findMax( root ) );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
70 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
71
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
72 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
73 * Find an item in the tree.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
74 * @param x the item to search for.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
75 * @return the matching item or null if not found.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
76 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
77 @SuppressWarnings("unchecked")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
78 public Comparable find( Comparable x )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
79 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
80 return elementAt( find( x, root ) );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
81 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
82
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
83 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
84 * Make the tree logically empty.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
85 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
86 public void makeEmpty( )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
87 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
88 root = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
89 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
90
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
91 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
92 * Test if the tree is logically empty.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
93 * @return true if empty, false otherwise.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
94 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
95 public boolean isEmpty( )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
96 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
97 return root == null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
98 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
99
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
100 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
101 * Print the tree contents in sorted order.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
102 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
103 public void printTree( )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
104 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
105 if( isEmpty( ) )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
106 System.out.println( "Empty tree" );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
107 else
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
108 printTree( root );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
109 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
110
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
111 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
112 * Internal method to get element field.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
113 * @param t the node.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
114 * @return the element field or null if t is null.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
115 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
116 @SuppressWarnings("unchecked")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
117 private Comparable elementAt( AVLNode t )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
118 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
119 return t == null ? null : t.element;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
120 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
121
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
122 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
123 * Internal method to insert into a subtree.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
124 * @param x the item to insert.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
125 * @param t the node that roots the tree.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
126 * @return the new root.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
127 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
128 @SuppressWarnings("unchecked")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
129 private AVLNode insert( Comparable x, AVLNode t )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
130 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
131 if( t == null )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
132 t = new AVLNode( x, null, null );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
133 else if( x.compareTo( t.element ) < 0 )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
134 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
135 t.left = insert( x, t.left );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
136 if( height( t.left ) - height( t.right ) == 2 )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
137 if( x.compareTo( t.left.element ) < 0 )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
138 t = rotateWithLeftChild( t );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
139 else
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
140 t = doubleWithLeftChild( t );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
141 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
142 else if( x.compareTo( t.element ) > 0 )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
143 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
144 t.right = insert( x, t.right );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
145 if( height( t.right ) - height( t.left ) == 2 )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
146 if( x.compareTo( t.right.element ) > 0 )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
147 t = rotateWithRightChild( t );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
148 else
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
149 t = doubleWithRightChild( t );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
150 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
151 else
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
152 ; // Duplicate; do nothing
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
153 t.height = max( height( t.left ), height( t.right ) ) + 1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
154 return t;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
155 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
156
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
157 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
158 * Internal method to find the smallest item in a subtree.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
159 * @param t the node that roots the tree.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
160 * @return node containing the smallest item.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
161 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
162 private AVLNode findMin( AVLNode t )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
163 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
164 if( t == null )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
165 return t;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
166
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
167 while( t.left != null )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
168 t = t.left;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
169 return t;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
170 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
171
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
172 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
173 * Internal method to find the largest item in a subtree.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
174 * @param t the node that roots the tree.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
175 * @return node containing the largest item.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
176 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
177 private AVLNode findMax( AVLNode t )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
178 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
179 if( t == null )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
180 return t;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
181
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
182 while( t.right != null )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
183 t = t.right;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
184 return t;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
185 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
186
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
187 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
188 * Internal method to find an item in a subtree.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
189 * @param x is item to search for.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
190 * @param t the node that roots the tree.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
191 * @return node containing the matched item.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
192 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
193 @SuppressWarnings("unchecked")
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
194 private AVLNode find( Comparable x, AVLNode t )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
195 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
196 while( t != null )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
197 if( x.compareTo( t.element ) < 0 )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
198 t = t.left;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
199 else if( x.compareTo( t.element ) > 0 )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
200 t = t.right;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
201 else
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
202 return t; // Match
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
203
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
204 return null; // No match
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
205 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
206
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
207 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
208 * Internal method to print a subtree in sorted order.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
209 * @param t the node that roots the tree.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
210 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
211 private void printTree( AVLNode t )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
212 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
213 if( t != null )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
214 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
215 printTree( t.left );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
216 System.out.println( t.element );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
217 printTree( t.right );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
218 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
219 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
220
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
221 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
222 * Return the height of node t, or -1, if null.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
223 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
224 private static int height( AVLNode t )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
225 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
226 return t == null ? -1 : t.height;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
227 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
228
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
229 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
230 * Return maximum of lhs and rhs.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
231 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
232 private static int max( int lhs, int rhs )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
233 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
234 return lhs > rhs ? lhs : rhs;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
235 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
236
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
237 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
238 * Rotate binary tree node with left child.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
239 * For AVL trees, this is a single rotation for case 1.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
240 * Update heights, then return new root.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
241 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
242 private static AVLNode rotateWithLeftChild( AVLNode k2 )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
243 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
244 AVLNode k1 = k2.left;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
245 k2.left = k1.right;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
246 k1.right = k2;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
247 k2.height = max( height( k2.left ), height( k2.right ) ) + 1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
248 k1.height = max( height( k1.left ), k2.height ) + 1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
249 return k1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
250 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
251
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
252 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
253 * Rotate binary tree node with right child.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
254 * For AVL trees, this is a single rotation for case 4.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
255 * Update heights, then return new root.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
256 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
257 private static AVLNode rotateWithRightChild( AVLNode k1 )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
258 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
259 AVLNode k2 = k1.right;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
260 k1.right = k2.left;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
261 k2.left = k1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
262 k1.height = max( height( k1.left ), height( k1.right ) ) + 1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
263 k2.height = max( height( k2.right ), k1.height ) + 1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
264 return k2;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
265 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
266
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
267 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
268 * Double rotate binary tree node: first left child
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
269 * with its right child; then node k3 with new left child.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
270 * For AVL trees, this is a double rotation for case 2.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
271 * Update heights, then return new root.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
272 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
273 private static AVLNode doubleWithLeftChild( AVLNode k3 )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
274 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
275 k3.left = rotateWithRightChild( k3.left );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
276 return rotateWithLeftChild( k3 );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
277 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
278
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
279 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
280 * Double rotate binary tree node: first right child
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
281 * with its left child; then node k1 with new right child.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
282 * For AVL trees, this is a double rotation for case 3.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
283 * Update heights, then return new root.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
284 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
285 private static AVLNode doubleWithRightChild( AVLNode k1 )
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
286 {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
287 k1.right = rotateWithLeftChild( k1.right );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
288 return rotateWithRightChild( k1 );
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
289 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
290
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
291 /** The tree root. */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
292 private AVLNode root;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
293
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
294 }