(&mut self, key: &K)
| 341 | } |
| 342 | |
| 343 | fn delete(&mut self, key: &K) { |
| 344 | unsafe { |
| 345 | let mut parent = null_mut(); |
| 346 | let mut node = self.root; |
| 347 | |
| 348 | // 找到待删除节点 |
| 349 | while !node.is_null() { |
| 350 | node = match (*node).key.cmp(key) { |
| 351 | Less => { |
| 352 | parent = node; |
| 353 | (*node).right |
| 354 | } |
| 355 | Equal => break, |
| 356 | Greater => { |
| 357 | parent = node; |
| 358 | (*node).left |
| 359 | } |
| 360 | }; |
| 361 | } |
| 362 | |
| 363 | if node.is_null() { |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | let cl = (*node).left; |
| 368 | let cr = (*node).right; |
| 369 | let mut deleted_color; |
| 370 | |
| 371 | // 删除又分为多种情况 |
| 372 | if cl.is_null() { |
| 373 | replace_node(self, parent, node, cr); |
| 374 | |
| 375 | /* 左右子节点均为空,n 随便着色 |
| 376 | * (n could be either color here) |
| 377 | * |
| 378 | * (n) NULL |
| 379 | * / \ --> |
| 380 | * NULL NULL |
| 381 | */ |
| 382 | if cr.is_null() { |
| 383 | deleted_color = (*node).color; |
| 384 | } else { |
| 385 | /* |
| 386 | * 左子节点空,右子节点不空 |
| 387 | * N Cr |
| 388 | * / \ --> / \ |
| 389 | * NULL cr NULL NULL |
| 390 | */ |
| 391 | (*cr).parent = parent; |
| 392 | (*cr).color = Color::Black; |
| 393 | deleted_color = Color::Red; |
| 394 | } |
| 395 | } else if cr.is_null() { |
| 396 | /* |
| 397 | * 左子节点不空,右子节点空 |
| 398 | * N Cl |
| 399 | * / \ --> / \ |
| 400 | * cl NULL NULL NULL |
no test coverage detected