mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00

* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see #1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
72 lines
2.0 KiB
JavaScript
72 lines
2.0 KiB
JavaScript
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, 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)) {
|
|
dataList.push(item)
|
|
}
|
|
})
|
|
|
|
avlStringTree._comp = collator.compare
|
|
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)
|
|
})
|
|
|
|
it('search if inserted element is present', () => {
|
|
demoData.forEach((data) => {
|
|
expect(avlTree.find(data)).toBeTruthy()
|
|
})
|
|
stringData.forEach((data) => {
|
|
expect(avlStringTree.find(data)).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()
|
|
})
|
|
})
|