MCPcopy Index your code
hub / github.com/TheAlgorithms/JavaScript / deleteElement

Function deleteElement

Data-Structures/Tree/AVLTree.js:194–227  ·  view source on GitHub ↗
(root, _val, tree)

Source from the content-addressed store, hash-verified

192
193// delete am element
194const 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
229const searchAVLTree = function (root, val, tree) {
230 if (root == null) {

Callers 1

removeMethod · 0.85

Calls 2

updateHeightFunction · 0.85
delBalanceFunction · 0.85

Tested by

no test coverage detected