Skip to content

Commit

Permalink
feat: add size() function
Browse files Browse the repository at this point in the history
Adds a size() function to measure the tree size.
  • Loading branch information
jniles committed Dec 21, 2020
1 parent 81a063e commit b5ec97d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ class Tree {
return pruned;
}

size() {
let size = 0;
this.walk(() => size++);
return size;
}

toArray() {
const array = [];
this.walk((node) => array.push(node));
Expand Down
4 changes: 4 additions & 0 deletions test/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ test('#prune() should remove nodes based on pruning function', t => {
t.false(afterIds.includes(5));
});

test('#size() should return the correct size of the tree', t => {
t.is(tree.size(), 7);
});

test('#walk() should be able to compute the balances of nodes', t => {
const node1 = tree.find(1);
const node4 = tree.find(4);
Expand Down

0 comments on commit b5ec97d

Please sign in to comment.