Skip to content
This repository was archived by the owner on Sep 25, 2019. It is now read-only.

Commit d1b2075

Browse files
HugoLiconVscissorsneedfoodtoo
authored andcommitted
fix(challenges): update test and add solution for DS challenge
ISSUES CLOSED: #164
1 parent 7707b18 commit d1b2075

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

challenges/08-coding-interview-prep/data-structures.json

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,15 +2169,15 @@
21692169
},
21702170
{
21712171
"text": "The add method adds elements according to the binary search tree rules.",
2172-
"testString": "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.add !== 'function') { return false; }; test.add(4); test.add(1); test.add(7); test.add(87); test.add(34); test.add(45); test.add(73); test.add(8); return (test.isBinarySearchTree()); })(), 'The add method adds elements according to the binary search tree rules.');"
2172+
"testString": "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.add !== 'function') { return false; }; test.add(4); test.add(1); test.add(7); test.add(87); test.add(34); test.add(45); test.add(73); test.add(8); const expectedResult = [ 1, 4, 7, 8, 34, 45, 73, 87 ]; const result = test.inOrder(); return (expectedResult.toString() === result.toString()); })(), 'The add method adds elements according to the binary search tree rules.');"
21732173
},
21742174
{
21752175
"text": "Adding an element that already exists returns <code>null</code>",
21762176
"testString": "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.add !== 'function') { return false; }; test.add(4); return test.add(4) == null; })(), 'Adding an element that already exists returns <code>null</code>');"
21772177
}
21782178
],
21792179
"releasedOn": "Feb 17, 2017",
2180-
"solutions": [],
2180+
"solutions": ["function Node(value) { \n this.value = value; \n this.left = null; \n this.right = null; \n } \n function BinarySearchTree() { \n this.root = null; \n this.add = function (element) { \n let current = this.root; \n if (!current) { \n this.root = new Node(element) \n return; \n } else { \n const searchTree = function (current) { \n if (current.value > element) { \n if (current.left) { //si existe \n return searchTree(current.left) \n } else { \n current.left = new Node(element); \n return; \n } \n } else if (current.value < element) { \n if (current.right) { \n return searchTree(current.right) \n } else { \n current.right = new Node(element) \n return; \n } \n } else { \n return null; \n } \n } \n return searchTree(current); \n } \n } \n }"],
21812181
"challengeType": 1,
21822182
"translations": {},
21832183
"files": {
@@ -2228,6 +2228,19 @@
22282228
" return check;",
22292229
" };",
22302230
" }",
2231+
"};",
2232+
"BinarySearchTree.prototype = {",
2233+
" inOrder() {",
2234+
" if (!this.root) { return null; }",
2235+
" var result = new Array();",
2236+
" function traverseInOrder(node) {",
2237+
" node.left && traverseInOrder(node.left);",
2238+
" result.push(node.value);",
2239+
" node.right && traverseInOrder(node.right);",
2240+
" }",
2241+
" traverseInOrder(this.root);",
2242+
" return result;",
2243+
" }",
22312244
"};"
22322245
]
22332246
}
@@ -3826,4 +3839,4 @@
38263839
}
38273840
}
38283841
]
3829-
}
3842+
}

0 commit comments

Comments
 (0)