Skip to content

Commit

Permalink
fix(challenges): update test and add solution for DS challenge
Browse files Browse the repository at this point in the history
ISSUES CLOSED: freeCodeCamp#164
  • Loading branch information
HugoLiconV authored and scissorsneedfoodtoo committed Aug 2, 2018
1 parent 7707b18 commit d1b2075
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions challenges/08-coding-interview-prep/data-structures.json
Original file line number Diff line number Diff line change
Expand Up @@ -2169,15 +2169,15 @@
},
{
"text": "The add method adds elements according to the binary search tree rules.",
"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.');"
"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.');"
},
{
"text": "Adding an element that already exists returns <code>null</code>",
"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>');"
}
],
"releasedOn": "Feb 17, 2017",
"solutions": [],
"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 }"],
"challengeType": 1,
"translations": {},
"files": {
Expand Down Expand Up @@ -2228,6 +2228,19 @@
" return check;",
" };",
" }",
"};",
"BinarySearchTree.prototype = {",
" inOrder() {",
" if (!this.root) { return null; }",
" var result = new Array();",
" function traverseInOrder(node) {",
" node.left && traverseInOrder(node.left);",
" result.push(node.value);",
" node.right && traverseInOrder(node.right);",
" }",
" traverseInOrder(this.root);",
" return result;",
" }",
"};"
]
}
Expand Down Expand Up @@ -3826,4 +3839,4 @@
}
}
]
}
}

0 comments on commit d1b2075

Please sign in to comment.