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

Function insert

Data-Structures/Tree/AVLTree.js:174–191  ·  view source on GitHub ↗
(root, val, tree)

Source from the content-addressed store, hash-verified

172
173// implement avl tree insertion
174const insert = function (root, val, tree) {
175 if (root == null) {
176 tree.size++
177 return new Node(val)
178 }
179 if (tree._comp(root._val, val) < 0) {
180 root._right = insert(root._right, val, tree)
181 } else if (tree._comp(root._val, val) > 0) {
182 root._left = insert(root._left, val, tree)
183 } else {
184 return root
185 }
186 updateHeight(root)
187 const balanceFactor = getHeightDifference(root)
188 return isValidBalanceFactor(balanceFactor)
189 ? root
190 : insertBalance(root, val, balanceFactor, tree)
191}
192
193// delete am element
194const deleteElement = function (root, _val, tree) {

Callers 1

addMethod · 0.85

Calls 4

updateHeightFunction · 0.85
getHeightDifferenceFunction · 0.85
isValidBalanceFactorFunction · 0.85
insertBalanceFunction · 0.85

Tested by

no test coverage detected