From a13352912224ada9ed5bb7660672e8dc2d68da62 Mon Sep 17 00:00:00 2001 From: k ho k ho? <6939499+kho-kho-kho@users.noreply.github.com> Date: Mon, 15 Aug 2022 09:27:31 -0700 Subject: [PATCH] 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. --- Data-Structures/Tree/AVLTree.js | 10 +++++----- Data-Structures/Tree/test/AVLTree.test.js | 12 ++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Data-Structures/Tree/AVLTree.js b/Data-Structures/Tree/AVLTree.js index d2b34362c..1390defd3 100644 --- a/Data-Structures/Tree/AVLTree.js +++ b/Data-Structures/Tree/AVLTree.js @@ -90,14 +90,14 @@ const AVLTree = (function () { } // check if tree is balanced else balance it for insertion - const insertBalance = function (node, _val, balanceFactor) { - if (balanceFactor > 1 && _val < node._left._val) { + const insertBalance = function (node, _val, balanceFactor, tree) { + if (balanceFactor > 1 && tree._comp(_val, node._left._val) < 0) { return rightRotate(node) // Left Left Case } - if (balanceFactor < 1 && _val > node._right._val) { + if (balanceFactor < 1 && tree._comp(_val, node._right._val) > 0) { return leftRotate(node) // Right Right Case } - if (balanceFactor > 1 && _val > node._left._val) { + if (balanceFactor > 1 && tree._comp(_val, node._left._val) > 0) { node._left = leftRotate(node._left) // Left Right Case return rightRotate(node) } @@ -140,7 +140,7 @@ const AVLTree = (function () { } updateHeight(root) const balanceFactor = getHeightDifference(root) - return isValidBalanceFactor(balanceFactor) ? root : insertBalance(root, val, balanceFactor) + return isValidBalanceFactor(balanceFactor) ? root : insertBalance(root, val, balanceFactor, tree) } // delete am element diff --git a/Data-Structures/Tree/test/AVLTree.test.js b/Data-Structures/Tree/test/AVLTree.test.js index 64cdd3ff4..f7ebc455e 100644 --- a/Data-Structures/Tree/test/AVLTree.test.js +++ b/Data-Structures/Tree/test/AVLTree.test.js @@ -5,26 +5,38 @@ describe('AVLTree Implementation: ', () => { const dataList = [] const demoData = [1, 4, 6, 22, 7, 99, 4, 66, 77, 98] + const avlStringTree = new AVLTree() + const collator = new Intl.Collator() + const stringData = ['S', 'W', 'z', 'B', 'a'] + beforeAll(() => { demoData.forEach(item => { if (avlTree.add(item)) { dataList.push(item) } }) + + avlStringTree._comp = collator.compare + stringData.forEach(item => avlStringTree.add(item)) }) it('checks if element is inserted properly', () => { expect(dataList.length).toEqual(avlTree.size) + expect(stringData.length).toEqual(avlStringTree.size) }) it('search if inserted element is present', () => { demoData.forEach(data => { expect(avlTree.find(data)).toBeTruthy() }) + stringData.forEach(data => { + expect(avlStringTree.find(data)).toBeTruthy() + }) }) it('deletes the inserted element', () => { const deleteElement = dataList[3] expect(avlTree.remove(deleteElement)).toBeTruthy() + expect(avlStringTree.remove(stringData[3])).toBeTruthy() }) })