From ed5db20c10a22a9da5e8f8b79c25ab022937067f Mon Sep 17 00:00:00 2001 From: k ho k ho? <6939499+kho-kho-kho@users.noreply.github.com> Date: Sun, 14 Aug 2022 18:14:03 -0700 Subject: [PATCH 1/3] Bugfix AVLTree comparator 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 d2b34362c0..1390defd35 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 64cdd3ff48..f7ebc455e5 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() }) }) From 2ea10b0a4f3436320157387a4a2b6224ddce8e29 Mon Sep 17 00:00:00 2001 From: k ho k ho? <6939499+kho-kho-kho@users.noreply.github.com> Date: Mon, 12 Sep 2022 20:28:02 -0700 Subject: [PATCH 2/3] 100% jest code coverage Added a couple of extra elements to the original test tree, and then removed elements in an order such that all previously uncovered branches of code are now covered. Also added an emptyTree structure to test some additional (trivial) base cases. --- Data-Structures/Tree/test/AVLTree.test.js | 40 ++++++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/Data-Structures/Tree/test/AVLTree.test.js b/Data-Structures/Tree/test/AVLTree.test.js index f7ebc455e5..9d4c25f9ff 100644 --- a/Data-Structures/Tree/test/AVLTree.test.js +++ b/Data-Structures/Tree/test/AVLTree.test.js @@ -3,12 +3,14 @@ import { AVLTree } from '../AVLTree' describe('AVLTree Implementation: ', () => { const avlTree = new AVLTree() const dataList = [] - const demoData = [1, 4, 6, 22, 7, 99, 4, 66, 77, 98] + const demoData = [1, 4, 6, 22, 7, 99, 4, 66, 77, 98, 87, 54, 32, 15] const avlStringTree = new AVLTree() const collator = new Intl.Collator() const stringData = ['S', 'W', 'z', 'B', 'a'] + const emptyTree = new AVLTree(collator.compare) + beforeAll(() => { demoData.forEach(item => { if (avlTree.add(item)) { @@ -20,6 +22,11 @@ describe('AVLTree Implementation: ', () => { stringData.forEach(item => avlStringTree.add(item)) }) + it('delete and search from empty tree', () => { + expect(emptyTree.remove(0)).toBeFalsy() + expect(emptyTree.find(0)).toBeFalsy() + }) + it('checks if element is inserted properly', () => { expect(dataList.length).toEqual(avlTree.size) expect(stringData.length).toEqual(avlStringTree.size) @@ -34,9 +41,32 @@ describe('AVLTree Implementation: ', () => { }) }) - it('deletes the inserted element', () => { - const deleteElement = dataList[3] - expect(avlTree.remove(deleteElement)).toBeTruthy() - expect(avlStringTree.remove(stringData[3])).toBeTruthy() + it('delete element with two valid children', () => { + expect(avlTree.remove(77)).toBeTruthy() + }) + + it('delete element missing L-child', () => { + expect(avlTree.remove(98)).toBeTruthy() + }) + + it('delete elements forcing single R-rotation', () => { + expect(avlTree.remove(99)).toBeTruthy() + expect(avlTree.remove(87)).toBeTruthy() }) + + it('delete elements forcing R-rotation and L-rotation', () => { + expect(avlTree.remove(1)).toBeTruthy() + expect(avlTree.remove(4)).toBeTruthy() + }) + + it('delete elements forcing single L-rotation', () => { + expect(avlTree.remove(7)).toBeTruthy() + expect(avlTree.remove(15)).toBeTruthy() + expect(avlTree.remove(6)).toBeTruthy() + }) + + it('delete element forcing single L-rotation and R-rotation', () => { + expect(avlTree.remove(66)).toBeTruthy() + }) + }) From 5f3b57c6c7e909917882e20ddf35d185dab2270f Mon Sep 17 00:00:00 2001 From: k ho k ho? <6939499+kho-kho-kho@users.noreply.github.com> Date: Mon, 12 Sep 2022 22:16:18 -0700 Subject: [PATCH 3/3] standard style fix missed this from my previous commit --- Data-Structures/Tree/test/AVLTree.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Data-Structures/Tree/test/AVLTree.test.js b/Data-Structures/Tree/test/AVLTree.test.js index 9d4c25f9ff..0b2f485174 100644 --- a/Data-Structures/Tree/test/AVLTree.test.js +++ b/Data-Structures/Tree/test/AVLTree.test.js @@ -68,5 +68,4 @@ describe('AVLTree Implementation: ', () => { it('delete element forcing single L-rotation and R-rotation', () => { expect(avlTree.remove(66)).toBeTruthy() }) - })