-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathProblem1.js
More file actions
62 lines (50 loc) · 1.61 KB
/
Problem1.js
File metadata and controls
62 lines (50 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
Flatten a binary search tree into linked lists by levels, such that...
4
2 6
1 3 5 7
Flattens to...
{
0: {4},
1: {2} -> {6},
3: {1} -> {3} -> {5} -> {7}
}
NOTE: feel free to rewrite any methods.
NOTE: Linked List does not have to be in order
NOTE: There is a property on the BST that i've added called flattenedBST.
It's just an empty object. Set the flattenedBST[level] should be a new LinkedList();
*/
var BinaryTree = require('./ds').BinaryTree;
var Node = require('./ds').BSTNode;
var LinkedList = require('./ds').LinkedList;
var testCases = require('./test/problem1cases');
BinaryTree.prototype.levelInsert = function () {
// TODO: Implement
};
/////////////////////////////////////////////////////////////
// TESTS
/////////////////////////////////////////////////////////////
// Makes array Binary Trees
testCases.map(function (testCase) {
return testCase.reduce(function (tree, number) {
// addes all the numbers from testCase into the BinaryTree
tree.levelInsert(number);
// adds the array of numbers to the tree just so it prints pretty
tree.input = testCase;
return tree;
}, new BinaryTree());
}).forEach(function (testCase) {
// Prints testCases
console.log('TestCase: ', testCase);
for (var level in testCase.flattenedBST) {
// What level we're on
console.log("Level: ", level);
var str = '';
// Traverses the linkedList
testCase.flattenedBST[level].traverse(function (n) {
str += n.value + ' ';
});
// Prints the values on each level
console.log(str);
}
});