Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d7000fb

Browse files
committed
Merge remote-tracking branch 'up/master'
2 parents 93cc506 + 2323351 commit d7000fb

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
2-
coverage/
2+
coverage/
3+
.vs/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const Queue = require('../../../Queue');
2+
3+
// Determines the bottom view of a binary tree
4+
// Takes a BinaryTree as a parameter
5+
// Returns an integer array
6+
// Time complexity: O(n) where n is the number of nodes in the tree
7+
8+
module.exports = function bottomView(binaryTree) {
9+
if (binaryTree == null || binaryTree.root == null) {
10+
return [];
11+
}
12+
13+
// root's horizontal distance = 0
14+
const horizontalDistance = 0;
15+
16+
// create a map to track most recent visited nodes per hd
17+
const hdToNodeValue = new Map();
18+
19+
// perform bfs
20+
const q = new Queue();
21+
q.enqueue([binaryTree.root, horizontalDistance]);
22+
23+
while (q.length() > 0) {
24+
const currentNodeTuple = q.dequeue();
25+
const currentNode = currentNodeTuple[0];
26+
const currentHd = currentNodeTuple[1];
27+
hdToNodeValue.set(currentHd, currentNode.value);
28+
29+
if (currentNode.leftChild != null && currentNode.leftChild.value != null) {
30+
q.enqueue([currentNode.leftChild, currentHd - 1]);
31+
}
32+
33+
if (currentNode.rightChild != null && currentNode.rightChild.value != null) {
34+
q.enqueue([currentNode.rightChild, currentHd + 1]);
35+
}
36+
}
37+
38+
return Array.from(hdToNodeValue.values());
39+
};
Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/* eslint-disable no-unused-vars */
2-
const Trie = require('../index');
3-
41
function uniqueWordCount(root) {
52
let result = 0;
63
if (root.isEndOfWord) {
@@ -14,10 +11,4 @@ function uniqueWordCount(root) {
1411
return result;
1512
}
1613

17-
// const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
18-
// const trie = new Trie();
19-
20-
// words.forEach(word => trie.insert(word));
21-
// console.log(uniqueWordCount(trie.root));
22-
2314
module.exports = uniqueWordCount;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const Trie = require('../index');
2+
const uniqueWordCount = require('.');
3+
4+
describe('Trie Unique Word Count', () => {
5+
it('counts an empty trie', () => {
6+
const trie = new Trie();
7+
const wordCount = uniqueWordCount(trie.root);
8+
expect(wordCount).toEqual(0);
9+
});
10+
11+
it('counts unique words', () => {
12+
const trie = new Trie();
13+
const words = ['one', 'two', 'three', 'four'];
14+
words.forEach(word => trie.insert(word));
15+
const wordCount = uniqueWordCount(trie.root);
16+
expect(wordCount).toEqual(4);
17+
});
18+
19+
it('does not count duplicate words', () => {
20+
const trie = new Trie();
21+
const words = ['one', 'one', 'two', 'three'];
22+
words.forEach(word => trie.insert(word));
23+
const wordCount = uniqueWordCount(trie.root);
24+
expect(wordCount).toEqual(3);
25+
});
26+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /