Skip to content

Commit 12be618

Browse files
✨ feat: Add isEmpty method.
1 parent 112acd0 commit 12be618

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/adt/RedBlackTree.js

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ export class RedBlackTree {
2222
this.root = null;
2323
}
2424

25+
/**
26+
* Tells whether the tree is empty.
27+
*
28+
* @returns {boolean} true if empty, false otherwise.
29+
*/
30+
isEmpty() {
31+
return this.root === null;
32+
}
33+
2534
/**
2635
* Adds a key to the tree.
2736
*

test/fixtures.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {increasing as _inc, decreasing as _dec} from '@aureooms/js-compare';
2+
3+
export const increasing = (a, b) => _inc(a, b);
4+
export const decreasing = (a, b) => _dec(a, b);

test/src/RedBlackTree/isEmpty.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import test from 'ava';
2+
3+
import {range} from '@aureooms/js-itertools';
4+
5+
import {increasing, decreasing} from '../../fixtures.js';
6+
7+
import {RedBlackTree} from '../../../src/index.js';
8+
9+
const macro = (t, compare, items, expected) => {
10+
const tree = RedBlackTree.from(compare, items);
11+
t.is(tree.isEmpty(), expected);
12+
};
13+
14+
macro.title = (title, compare, items, _expected) =>
15+
`RedBlackTree.from(${compare.name}, ${
16+
title || JSON.stringify(items)
17+
}).isEmpty()`;
18+
19+
for (const compare of [increasing, decreasing]) {
20+
test(macro, compare, [], true);
21+
test(macro, compare, [1], false);
22+
test(macro, compare, '', true);
23+
test(macro, compare, 'ab', false);
24+
test(macro, compare, 'abc', false);
25+
test('range(0)', macro, compare, range(0), true);
26+
test('range(1)', macro, compare, range(1), false);
27+
test('range(10)', macro, compare, range(10), false);
28+
test('range(10000)', macro, compare, range(10000), false);
29+
}

0 commit comments

Comments
 (0)