replaceNode replaces the parent’s child pointer to n with a pointer to the replacement node. parent must not be nil.
(parent, replacement *node)
| 274 | // replaceNode replaces the parent’s child pointer to n with a pointer |
| 275 | // to the replacement node. parent must not be nil. |
| 276 | func (n *node) replaceNode(parent, replacement *node) error { |
| 277 | if n == nil { |
| 278 | return errors.New("replaceNode() not allowed on a nil node") |
| 279 | } |
| 280 | |
| 281 | switch n { |
| 282 | case parent.left: |
| 283 | parent.left = replacement |
| 284 | |
| 285 | default: |
| 286 | parent.right = replacement |
| 287 | } |
| 288 | |
| 289 | return nil |
| 290 | } |
| 291 | |
| 292 | // delete removes an element from the tree. It is an error to try |
| 293 | // deleting an element that does not exist. In order to remove an |