(root, _val, tree)
| 192 | |
| 193 | // delete am element |
| 194 | const deleteElement = function (root, _val, tree) { |
| 195 | if (root == null) { |
| 196 | return root |
| 197 | } |
| 198 | if (tree._comp(root._val, _val) === 0) { |
| 199 | // key found case |
| 200 | if (root._left === null && root._right === null) { |
| 201 | root = null |
| 202 | tree.size-- |
| 203 | } else if (root._left === null) { |
| 204 | root = root._right |
| 205 | tree.size-- |
| 206 | } else if (root._right === null) { |
| 207 | root = root._left |
| 208 | tree.size-- |
| 209 | } else { |
| 210 | let temp = root._right |
| 211 | while (temp._left != null) { |
| 212 | temp = temp._left |
| 213 | } |
| 214 | root._val = temp._val |
| 215 | root._right = deleteElement(root._right, temp._val, tree) |
| 216 | } |
| 217 | } else { |
| 218 | if (tree._comp(root._val, _val) < 0) { |
| 219 | root._right = deleteElement(root._right, _val, tree) |
| 220 | } else { |
| 221 | root._left = deleteElement(root._left, _val, tree) |
| 222 | } |
| 223 | } |
| 224 | updateHeight(root) |
| 225 | root = delBalance(root) |
| 226 | return root |
| 227 | } |
| 228 | // search tree for a element |
| 229 | const searchAVLTree = function (root, val, tree) { |
| 230 | if (root == null) { |
no test coverage detected