@@ -45,11 +45,8 @@ public void testLeftLeftRotation() {
45
45
assertEquals (tree .root , tree .root .left .parent );
46
46
assertEquals (tree .root , tree .root .right .parent );
47
47
48
- assertNull (tree .root .parent );
49
- assertNull (tree .root .left .left );
50
- assertNull (tree .root .left .right );
51
- assertNull (tree .root .right .left );
52
- assertNull (tree .root .right .right );
48
+ assertNullChildren (tree .root .left , tree .root .right );
49
+ assertCorrectParentLinks (tree .root , null );
53
50
54
51
}
55
52
@@ -71,11 +68,8 @@ public void testLeftRightRotation() {
71
68
assertEquals (tree .root , tree .root .left .parent );
72
69
assertEquals (tree .root , tree .root .right .parent );
73
70
74
- assertNull (tree .root .parent );
75
- assertNull (tree .root .left .left );
76
- assertNull (tree .root .left .right );
77
- assertNull (tree .root .right .left );
78
- assertNull (tree .root .right .right );
71
+ assertNullChildren (tree .root .left , tree .root .right );
72
+ assertCorrectParentLinks (tree .root , null );
79
73
80
74
}
81
75
@@ -97,11 +91,8 @@ public void testRightLeftRotation() {
97
91
assertEquals (tree .root , tree .root .left .parent );
98
92
assertEquals (tree .root , tree .root .right .parent );
99
93
100
- assertNull (tree .root .parent );
101
- assertNull (tree .root .left .left );
102
- assertNull (tree .root .left .right );
103
- assertNull (tree .root .right .left );
104
- assertNull (tree .root .right .right );
94
+ assertNullChildren (tree .root .left , tree .root .right );
95
+ assertCorrectParentLinks (tree .root , null );
105
96
106
97
}
107
98
@@ -123,12 +114,62 @@ public void testRightRightRotation() {
123
114
assertEquals (tree .root , tree .root .left .parent );
124
115
assertEquals (tree .root , tree .root .right .parent );
125
116
126
- assertNull (tree .root .parent );
127
- assertNull (tree .root .left .left );
128
- assertNull (tree .root .left .right );
117
+ assertNullChildren (tree .root .left , tree .root .right );
118
+ assertCorrectParentLinks (tree .root , null );
119
+
120
+ }
121
+
122
+ @ Test
123
+ public void testLeftRedUncleCase () {
124
+
125
+ tree .insert (1 );
126
+ tree .insert (2 );
127
+ tree .insert (3 );
128
+ tree .insert (4 );
129
+
130
+ assertEquals (2 , tree .root .value .intValue ());
131
+ assertEquals (1 , tree .root .left .value .intValue ());
132
+ assertEquals (3 , tree .root .right .value .intValue ());
133
+ assertEquals (4 , tree .root .right .right .value .intValue ());
134
+
135
+ assertEquals (RedBlackTree .BLACK , tree .root .color );
136
+ assertEquals (RedBlackTree .BLACK , tree .root .left .color );
137
+ assertEquals (RedBlackTree .BLACK , tree .root .right .color );
138
+ assertEquals (RedBlackTree .RED , tree .root .right .right .color );
139
+
129
140
assertNull (tree .root .right .left );
130
- assertNull (tree .root .right .right );
141
+ assertNullChildren (tree .root .left , tree .root .right .right );
142
+ assertCorrectParentLinks (tree .root , null );
143
+
144
+ tree .insert (5 );
145
+
146
+ assertEquals (2 , tree .root .value .intValue ());
147
+ assertEquals (1 , tree .root .left .value .intValue ());
148
+ assertEquals (4 , tree .root .right .value .intValue ());
149
+ assertEquals (3 , tree .root .right .left .value .intValue ());
150
+ assertEquals (5 , tree .root .right .right .value .intValue ());
151
+
152
+ assertEquals (RedBlackTree .BLACK , tree .root .color );
153
+ assertEquals (RedBlackTree .BLACK , tree .root .left .color );
154
+ assertEquals (RedBlackTree .BLACK , tree .root .right .color );
155
+ assertEquals (RedBlackTree .RED , tree .root .right .left .color );
156
+ assertEquals (RedBlackTree .RED , tree .root .right .right .color );
157
+ assertCorrectParentLinks (tree .root , null );
158
+
159
+ }
160
+
161
+ static void assertNullChildren (RedBlackTree .Node ... nodes ) {
162
+ for (RedBlackTree .Node node : nodes ) {
163
+ assertNull (node .left );
164
+ assertNull (node .right );
165
+ }
166
+ }
131
167
168
+ static void assertCorrectParentLinks (RedBlackTree .Node node , RedBlackTree .Node parent ) {
169
+ if (node == null ) return ;
170
+ assertEquals (node .parent , parent );
171
+ assertCorrectParentLinks (node .left , node );
172
+ assertCorrectParentLinks (node .right , node );
132
173
}
133
174
134
175
static List <Integer > genRandList (int sz ) {
0 commit comments