Skip to content

Commit a133529

Browse files
authored
merge: Bugfix AVLTree comparator (#1084)
The original insertBalance function was doing raw value comparisons as opposed to using the tree's comparator. This is clearly unintentional, and would (ultimately) cause the structure to segfault when constructed with the stringData included in the updated test. I've added the fix, scanned the rest of the code for similar issues, and added the appropriate test case which passes successfully with the fix. The jest code coverage increases slightly as well with the changes.
1 parent 9790468 commit a133529

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Data-Structures/Tree/AVLTree.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ const AVLTree = (function () {
9090
}
9191

9292
// check if tree is balanced else balance it for insertion
93-
const insertBalance = function (node, _val, balanceFactor) {
94-
if (balanceFactor > 1 && _val < node._left._val) {
93+
const insertBalance = function (node, _val, balanceFactor, tree) {
94+
if (balanceFactor > 1 && tree._comp(_val, node._left._val) < 0) {
9595
return rightRotate(node) // Left Left Case
9696
}
97-
if (balanceFactor < 1 && _val > node._right._val) {
97+
if (balanceFactor < 1 && tree._comp(_val, node._right._val) > 0) {
9898
return leftRotate(node) // Right Right Case
9999
}
100-
if (balanceFactor > 1 && _val > node._left._val) {
100+
if (balanceFactor > 1 && tree._comp(_val, node._left._val) > 0) {
101101
node._left = leftRotate(node._left) // Left Right Case
102102
return rightRotate(node)
103103
}
@@ -140,7 +140,7 @@ const AVLTree = (function () {
140140
}
141141
updateHeight(root)
142142
const balanceFactor = getHeightDifference(root)
143-
return isValidBalanceFactor(balanceFactor) ? root : insertBalance(root, val, balanceFactor)
143+
return isValidBalanceFactor(balanceFactor) ? root : insertBalance(root, val, balanceFactor, tree)
144144
}
145145

146146
// delete am element

Data-Structures/Tree/test/AVLTree.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,38 @@ describe('AVLTree Implementation: ', () => {
55
const dataList = []
66
const demoData = [1, 4, 6, 22, 7, 99, 4, 66, 77, 98]
77

8+
const avlStringTree = new AVLTree()
9+
const collator = new Intl.Collator()
10+
const stringData = ['S', 'W', 'z', 'B', 'a']
11+
812
beforeAll(() => {
913
demoData.forEach(item => {
1014
if (avlTree.add(item)) {
1115
dataList.push(item)
1216
}
1317
})
18+
19+
avlStringTree._comp = collator.compare
20+
stringData.forEach(item => avlStringTree.add(item))
1421
})
1522

1623
it('checks if element is inserted properly', () => {
1724
expect(dataList.length).toEqual(avlTree.size)
25+
expect(stringData.length).toEqual(avlStringTree.size)
1826
})
1927

2028
it('search if inserted element is present', () => {
2129
demoData.forEach(data => {
2230
expect(avlTree.find(data)).toBeTruthy()
2331
})
32+
stringData.forEach(data => {
33+
expect(avlStringTree.find(data)).toBeTruthy()
34+
})
2435
})
2536

2637
it('deletes the inserted element', () => {
2738
const deleteElement = dataList[3]
2839
expect(avlTree.remove(deleteElement)).toBeTruthy()
40+
expect(avlStringTree.remove(stringData[3])).toBeTruthy()
2941
})
3042
})

0 commit comments

Comments
 (0)