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

Function delBalance

Data-Structures/Tree/AVLTree.js:154–171  ·  view source on GitHub ↗
(node)

Source from the content-addressed store, hash-verified

152
153// check if tree is balanced after deletion
154const delBalance = function (node) {
155 const balanceFactor1 = getHeightDifference(node)
156 if (isValidBalanceFactor(balanceFactor1)) {
157 return node
158 }
159 if (balanceFactor1 > 1) {
160 if (getHeightDifference(node._left) >= 0) {
161 return rightRotate(node) // Left Left
162 }
163 node._left = leftRotate(node._left)
164 return rightRotate(node) // Left Right
165 }
166 if (getHeightDifference(node._right) > 0) {
167 node._right = rightRotate(node._right)
168 return leftRotate(node) // Right Left
169 }
170 return leftRotate(node) // Right Right
171}
172
173// implement avl tree insertion
174const insert = function (root, val, tree) {

Callers 1

deleteElementFunction · 0.85

Calls 4

getHeightDifferenceFunction · 0.85
isValidBalanceFactorFunction · 0.85
rightRotateFunction · 0.85
leftRotateFunction · 0.85

Tested by

no test coverage detected