(root *AVLNode[T], key T)
| 222 | } |
| 223 | |
| 224 | func (avl *AVL[T]) deleteHelper(root *AVLNode[T], key T) *AVLNode[T] { |
| 225 | if root == avl._NIL { |
| 226 | return root |
| 227 | } |
| 228 | |
| 229 | switch { |
| 230 | case key < root.key: |
| 231 | tmp := avl.deleteHelper(root.left, key) |
| 232 | root.left = tmp |
| 233 | if tmp != avl._NIL { |
| 234 | tmp.parent = root |
| 235 | } |
| 236 | case key > root.key: |
| 237 | tmp := avl.deleteHelper(root.right, key) |
| 238 | root.right = tmp |
| 239 | if tmp != avl._NIL { |
| 240 | tmp.parent = root |
| 241 | } |
| 242 | default: |
| 243 | if root.left == avl._NIL || root.right == avl._NIL { |
| 244 | tmp := root.left |
| 245 | if root.right != avl._NIL { |
| 246 | tmp = root.right |
| 247 | } |
| 248 | |
| 249 | if tmp == avl._NIL { |
| 250 | root = avl._NIL |
| 251 | } else { |
| 252 | tmp.parent = root.parent |
| 253 | root = tmp |
| 254 | } |
| 255 | } else { |
| 256 | tmp := minimum[T](root.right, avl._NIL).(*AVLNode[T]) |
| 257 | root.key = tmp.key |
| 258 | del := avl.deleteHelper(root.right, tmp.key) |
| 259 | root.right = del |
| 260 | if del != avl._NIL { |
| 261 | del.parent = root |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if root == avl._NIL { |
| 267 | return root |
| 268 | } |
| 269 | |
| 270 | // balance the tree |
| 271 | root.height = avl.height(root) |
| 272 | bFactor := avl.balanceFactor(root) |
| 273 | switch { |
| 274 | case bFactor > 1: |
| 275 | switch { |
| 276 | case avl.balanceFactor(root.left) >= 0: |
| 277 | return avl.rightRotate(root) |
| 278 | default: |
| 279 | root.left = avl.leftRotate(root.left) |
| 280 | return avl.rightRotate(root) |
| 281 | } |
no test coverage detected