Skip to content

Commit a338104

Browse files
Andre StackhouseAndre Stackhouse
Andre Stackhouse
authored and
Andre Stackhouse
committed
Implement new solutions.
1 parent df6bbe9 commit a338104

File tree

6 files changed

+115
-3
lines changed

6 files changed

+115
-3
lines changed

data-structures/TreeNode.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ const TreeNode = (data) => {
4343
}
4444
}
4545
console.log(feedback_string);
46+
},
47+
depthFirstSearch: function(target_node) {
48+
if (this === target_node) {
49+
return this;
50+
} else {
51+
let left_search = this.left ? this.left.depthFirstSearch(target_node) : -1;
52+
let right_search = this.right ? this.right.depthFirstSearch(target_node) : -1;
53+
if (left_search !== -1) {
54+
return left_search;
55+
} else if (right_search !== -1) {
56+
return right_search;
57+
} else {
58+
return -1;
59+
}
60+
}
4661
}
4762
}
4863
}

index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,15 @@ <h3>Mocha Test Output</h3>
171171
<script src="solutions/04-trees-and-graphs/04-02-minimal_tree.js"></script>
172172
<script src="solutions/04-trees-and-graphs/04-03-list_of_depths.js"></script>
173173
<script src="solutions/04-trees-and-graphs/04-04-check_balanced.js"></script>
174+
<script src="solutions/04-trees-and-graphs/04-08-first_common_ancestor.js"></script>
174175
<script src="solutions/04-trees-and-graphs/is_binary_search_tree.js"></script>
175176

176177
<!-- Recursion and Dynamic Programming -->
177178
<script src="solutions/08-recursion-and-dynamic-programming/08-01-triple_step.js"></script>
178179
<script src="solutions/08-recursion-and-dynamic-programming/08-03-magic_index.js"></script>
180+
<script src="solutions/08-recursion-and-dynamic-programming/08-04-power_set.js"></script>
179181
<script src="solutions/08-recursion-and-dynamic-programming/08-05-recursive_multiply.js"></script>
182+
<script src="solutions/08-recursion-and-dynamic-programming/max_nonadjacent_subset.js"></script>
180183

181184
<!-- Sorting and Searching -->
182185
<script src="solutions/10-sorting-and-searching/10-01-sorted_merge.js"></script>

object-instances/sample_trees.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@ valid_bst.left = TreeNode(5);
77
valid_bst.right = TreeNode(11);
88

99
valid_bst.left.left = TreeNode(2);
10-
valid_bst.left.right = TreeNode(7);
10+
let shallow_left = TreeNode(7);
11+
valid_bst.left.right = shallow_left;
1112

1213
valid_bst.left.left.right = TreeNode(4);
13-
valid_bst.left.left.right.left = TreeNode(3);
14+
let far_left_node = TreeNode(3);
15+
valid_bst.left.left.right.left = far_left_node;
1416

1517
valid_bst.right.left = TreeNode(10);
1618
valid_bst.right.right = TreeNode(18);
1719

18-
valid_bst.right.right.right = TreeNode(20);
20+
let close_to_bottom = TreeNode(20);
21+
valid_bst.right.right.right = close_to_bottom;
1922

2023
// Invalid BST
2124
let invalid_bst = TreeNode(8);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function first_common_ancestor(root, target_1, target_2) {
2+
let helper = (current) => {
3+
if (current.left && current.left.depthFirstSearch(target_1) !== -1 && current.left.depthFirstSearch(target_2) !== -1) {
4+
return helper(current.left);
5+
} else if (current.right && current.right.depthFirstSearch(target_1) !== -1 && current.right.depthFirstSearch(target_2) !== -1) {
6+
return helper(current.right);
7+
} else {
8+
return current;
9+
}
10+
}
11+
return helper(root);
12+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
function match_query(words, query) {
2+
let result = [];
3+
4+
let query_components = query.split('?');
5+
6+
words.filter(word => {
7+
let word_copy = word;
8+
if (word.length !== query.length) {
9+
return false;
10+
} else {
11+
for (let component of query_components) {
12+
if (component === '') {
13+
word_copy.slice(1, word_copy.length);
14+
} else {
15+
if (word_copy.indexOf(component) !== 0) {
16+
return false;
17+
}
18+
}
19+
}
20+
return true;
21+
}
22+
});
23+
24+
return result;
25+
}
26+
27+
28+
function getAllKeys() {
29+
return(fs.ls);
30+
}
31+
32+
function removeItem(key) {
33+
fs.remove(key);
34+
console.log('Deleted');
35+
}
36+
37+
function setItem(key, value, callback) {
38+
operations.add_operation_to_queue(fs.writeFile(hash(key), value)
39+
.done(result => {
40+
callback();
41+
return result
42+
})
43+
);
44+
}
45+
46+
function execute_file_operations(operations) {
47+
for (operation of operations) {
48+
operation().done(() => {
49+
continue;
50+
});
51+
}
52+
}
53+
54+
function add_operation_to_queue(key, value, operation) {
55+
operations.push(setItem(key, value, print_result));
56+
}
57+
58+
function execute_operations() {
59+
while (!operations.empty()) {
60+
execute_file_operations(operations);
61+
}
62+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function max_nonadjacent_subset(numbers) {
2+
let one_previous = 0;
3+
let two_previous = 0;
4+
5+
let helper = index => {
6+
let new_addend = numbers[index];
7+
let new_max = one_previous > two_previous + new_addend ? one_previous : new_addend + two_previous;
8+
two_previous = one_previous;
9+
one_previous = new_max;
10+
}
11+
12+
for (let i = 0; i < numbers.length; i++) {
13+
helper(i);
14+
}
15+
16+
return one_previous;
17+
}

0 commit comments

Comments
 (0)