Skip to content

Bugfix AVLTree comparator #1084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Data-Structures/Tree/AVLTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions Data-Structures/Tree/test/AVLTree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})