(
root: Option<NonNull<Node<K, V>>>,
x: Option<NonNull<Node<K, V>>>,
)
| 153 | } |
| 154 | |
| 155 | fn insert_fix<K, V>( |
| 156 | root: Option<NonNull<Node<K, V>>>, |
| 157 | x: Option<NonNull<Node<K, V>>>, |
| 158 | ) -> Option<NonNull<Node<K, V>>> { |
| 159 | let mut t = NodeQuery::new(root); |
| 160 | let mut x = NodeQuery::new(x); |
| 161 | while x.parent().color() == Some(Color::Red) { |
| 162 | if x.uncle().color() == Some(Color::Red) { |
| 163 | // case 1: ((a:R x:R b) y:B c:R) => ((a:R x:B b) y:R c:B) |
| 164 | x.parent().set_color(Color::Black); |
| 165 | x.grandparent().set_color(Color::Red); |
| 166 | x.uncle().set_color(Color::Black); |
| 167 | x = x.grandparent(); |
| 168 | } else if x.parent().i_am_left() { |
| 169 | if x.i_am_right() { |
| 170 | // case 2: ((a x:R b:R) y:B c) => case 3 |
| 171 | x = x.parent(); |
| 172 | t.node = rotate_left(t.node, x.node.unwrap()); |
| 173 | } |
| 174 | // case 3: ((a:R x:R b) y:B c) => (a:R x:B (b y:R c)) |
| 175 | x.parent().set_color(Color::Black); |
| 176 | x.grandparent().set_color(Color::Red); |
| 177 | t.node = rotate_right(t.node, x.grandparent().node.unwrap()); |
| 178 | } else { |
| 179 | if x.i_am_left() { |
| 180 | // case 2': (a x:B (b:R y:R c)) => case 3' |
| 181 | x = x.parent(); |
| 182 | t.node = rotate_right(t.node, x.node.unwrap()); |
| 183 | } |
| 184 | // case 3': (a x:B (b y:R c:R)) => ((a x:R b) y:B c:R) |
| 185 | x.parent().set_color(Color::Black); |
| 186 | x.grandparent().set_color(Color::Red); |
| 187 | t.node = rotate_left(t.node, x.grandparent().node.unwrap()); |
| 188 | } |
| 189 | } |
| 190 | t.set_color(Color::Black); |
| 191 | t.node |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | 左旋操作变换为: |
no test coverage detected