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

Function insertBalance

Data-Structures/Tree/AVLTree.js:138–151  ·  view source on GitHub ↗
(node, _val, balanceFactor, tree)

Source from the content-addressed store, hash-verified

136
137// check if tree is balanced else balance it for insertion
138const insertBalance = function (node, _val, balanceFactor, tree) {
139 if (balanceFactor > 1 && tree._comp(_val, node._left._val) < 0) {
140 return rightRotate(node) // Left Left Case
141 }
142 if (balanceFactor < 1 && tree._comp(_val, node._right._val) > 0) {
143 return leftRotate(node) // Right Right Case
144 }
145 if (balanceFactor > 1 && tree._comp(_val, node._left._val) > 0) {
146 node._left = leftRotate(node._left) // Left Right Case
147 return rightRotate(node)
148 }
149 node._right = rightRotate(node._right)
150 return leftRotate(node)
151}
152
153// check if tree is balanced after deletion
154const delBalance = function (node) {

Callers 1

insertFunction · 0.85

Calls 2

rightRotateFunction · 0.85
leftRotateFunction · 0.85

Tested by

no test coverage detected