Clean up binary tree challenges (#13603)

This commit is contained in:
Kenneth-LJS
2017-03-06 08:42:16 +08:00
committed by Quincy Larson
parent 217c47fae7
commit b951eb308b

View File

@ -1986,60 +1986,61 @@
"challengeSeed": [ "challengeSeed": [
"var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));", "var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));",
"function Node(value) {", "function Node(value) {",
" this.value = value;", " this.value = value;",
" this.left = null;", " this.left = null;",
" this.right = null;", " this.right = null;",
"}", "}",
"", "",
"function BinarySearchTree() {", "function BinarySearchTree() {",
" this.root = null;", " this.root = null;",
" this.remove = function(value) {", " this.remove = function(value) {",
" if (this.root == null) {", " if (this.root === null) {",
" return null;", " return null;",
" };", " }",
" var target;", " var target;",
" var parent = null;", " var parent = null;",
" // find the target value and its parent", " // find the target value and its parent",
" (function findValue(node = this.root) {", " (function findValue(node = this.root) {",
" if (value == node.value) {", " if (value == node.value) {",
" target = node;", " target = node;",
" } else if (value < node.value && node.left != null) {", " } else if (value < node.value && node.left !== null) {",
" parent = node;", " parent = node;",
" return findValue(node.left);", " return findValue(node.left);",
" } else if (value < node.value && node.left == null) {", " } else if (value < node.value && node.left === null) {",
" return null;", " return null;",
" } else if (value > node.value && node.right != null) {", " } else if (value > node.value && node.right !== null) {",
" parent = node;", " parent = node;",
" return findValue(node.right);", " return findValue(node.right);",
" } else {", " } else {",
" return null;", " return null;",
" };", " }",
" }).bind(this)();", " }).bind(this)();",
" if (target == null) {", " if (target === null) {",
" return null;", " return null;",
" }",
" // count the children of the target to delete",
" var children = (target.left !== null ? 1 : 0) + (target.right !== null ? 1 : 0);",
" // case 1: target has no children",
" if (children === 0) {",
" if (target == this.root) {",
" this.root = null;",
" }",
" else {",
" if (parent.left == target) {",
" parent.left = null;",
" } else {",
" parent.right = null;",
" }", " }",
" // count the children of the target to delete", " }",
" var children = (target.left != null ? 1 : 0) + (target.right != null ? 1 : 0);", " }",
" // case 1: target has no children", " // case 2: target has one child, change code below this line",
" if (children == 0) {", " };",
" if (target == this.root) {",
" this.root = null;",
" }",
" else {",
" if (parent.left == target) {",
" parent.left = null;",
" } else {",
" parent.right = null;",
" };",
" };",
" }",
" // case 2: target has one child, change code below this line",
"}" "}"
], ],
"tests": [ "tests": [
"assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() }; return (typeof test == 'object')})(), 'message: The BinarySearchTree data structure exists.');", "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() }; return (typeof test == 'object')})(), 'message: The BinarySearchTree data structure exists.');",
"assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.remove == 'function')})(), 'message: The binary search tree has a method called remove.');", "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.remove == 'function')})(), 'message: The binary search tree has a method called remove.');",
"assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; return (test.remove(100) == null); })(), 'message: Trying to remove an element that doesn\\'t exist returns null.');", "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; return (test.remove(100) == null); })(), 'message: Trying to remove an element that does not exist returns null.');",
"assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(500); test.remove(500); return (test.inorder() == null); })(), 'message: If the root node has no children, deleting it sets the root to null.');", "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(500); test.remove(500); return (test.inorder() == null); })(), 'message: If the root node has no children, deleting it sets the root to null.');",
"assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(5); test.add(3); test.add(7); test.add(6); test.add(10); test.add(12); test.remove(3); test.remove(12); test.remove(10); return (test.inorder().join('') == '567'); })(), 'message: The remove method removes leaf nodes from the tree');", "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(5); test.add(3); test.add(7); test.add(6); test.add(10); test.add(12); test.remove(3); test.remove(12); test.remove(10); return (test.inorder().join('') == '567'); })(), 'message: The remove method removes leaf nodes from the tree');",
"assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(-1); test.add(3); test.add(7); test.add(16); test.remove(16); test.remove(7); test.remove(3); return (test.inorder().join('') == '-1'); })(), 'message: The remove method removes nodes with one child.');", "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(-1); test.add(3); test.add(7); test.add(16); test.remove(16); test.remove(7); test.remove(3); return (test.inorder().join('') == '-1'); })(), 'message: The remove method removes nodes with one child.');",
@ -2139,75 +2140,75 @@
"challengeSeed": [ "challengeSeed": [
"var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));", "var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));",
"function Node(value) {", "function Node(value) {",
" this.value = value;", " this.value = value;",
" this.left = null;", " this.left = null;",
" this.right = null;", " this.right = null;",
"}", "}",
"", "",
"function BinarySearchTree() {", "function BinarySearchTree() {",
" this.root = null;", " this.root = null;",
" this.remove = function(value) {", " this.remove = function(value) {",
" if (this.root == null) {", " if (this.root === null) {",
" return null;", " return null;",
" };", " }",
" var target;", " var target;",
" var parent = null;", " var parent = null;",
" // find the target value and its parent", " // find the target value and its parent",
" (function findValue(node = this.root) {", " (function findValue(node = this.root) {",
" if (value == node.value) {", " if (value == node.value) {",
" target = node;", " target = node;",
" } else if (value < node.value && node.left != null) {", " } else if (value < node.value && node.left !== null) {",
" parent = node;", " parent = node;",
" return findValue(node.left);", " return findValue(node.left);",
" } else if (value < node.value && node.left == null) {", " } else if (value < node.value && node.left === null) {",
" return null;", " return null;",
" } else if (value > node.value && node.right != null) {", " } else if (value > node.value && node.right !== null) {",
" parent = node;", " parent = node;",
" return findValue(node.right);", " return findValue(node.right);",
" } else {", " } else {",
" return null;", " return null;",
" };", " }",
" }).bind(this)();", " }).bind(this)();",
" if (target == null) {", " if (target === null) {",
" return null;", " return null;",
" }",
" // count the children of the target to delete",
" var children = (target.left !== null ? 1 : 0) + (target.right !== null ? 1 : 0);",
" // case 1: target has no children",
" if (children === 0) {",
" if (target == this.root) {",
" this.root = null;",
" }",
" else {",
" if (parent.left == target) {",
" parent.left = null;",
" } else {",
" parent.right = null;",
" }", " }",
" // count the children of the target to delete", " }",
" var children = (target.left != null ? 1 : 0) + (target.right != null ? 1 : 0);", " }",
" // case 1: target has no children", " // case 2: target has one child",
" if (children == 0) {", " else if (children == 1) {",
" if (target == this.root) {", " var newChild = (target.left !== null) ? target.left : target.right;",
" this.root = null;", " if (parent === null) {",
" }", " target.value = newChild.value;",
" else {", " target.left = null;",
" if (parent.left == target) {", " target.right = null;",
" parent.left = null;", " } else if (newChild.value < parent.value) {",
" } else {", " parent.left = newChild;",
" parent.right = null;", " } else {",
" };", " parent.right = newChild;",
" };", " }",
" }", " target = null;",
" // case 2: target has one child", " }",
" else if (children == 1) {", " // case 3: target has two children, change code below this line",
" var newChild = (target.left != null) ? target.left : target.right;", " };",
" if (parent == null) {",
" target.value = newChild.value;",
" target.left = null;",
" target.right = null;",
" } else if (newChild.value < parent.value) {",
" parent.left = newChild;",
" } else {",
" parent.right = newChild;",
" }",
" target = null;",
" }",
" // case 3: target has two children, change code below this line",
" };",
"}" "}"
], ],
"tests": [ "tests": [
"assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() }; return (typeof test == 'object')})(), 'message: The BinarySearchTree data structure exists.');", "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() }; return (typeof test == 'object')})(), 'message: The BinarySearchTree data structure exists.');",
"assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.remove == 'function')})(), 'message: The binary search tree has a method called remove.');", "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.remove == 'function')})(), 'message: The binary search tree has a method called remove.');",
"assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.remove == 'function') ? (test.remove(100) == null) : false})(), 'message: Trying to remove an element that doesn\\'t exist returns null.');", "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.remove == 'function') ? (test.remove(100) == null) : false})(), 'message: Trying to remove an element that does not exist returns null.');",
"assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; test.add(500); test.remove(500); return (typeof test.remove == 'function') ? (test.inorder() == null) : false})(), 'message: If the root node has no children, deleting it sets the root to null.');", "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; test.add(500); test.remove(500); return (typeof test.remove == 'function') ? (test.inorder() == null) : false})(), 'message: If the root node has no children, deleting it sets the root to null.');",
"assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; test.add(5); test.add(3); test.add(7); test.add(6); test.add(10); test.add(12); test.remove(3); test.remove(12); test.remove(10); return (typeof test.remove == 'function') ? (test.inorder().join('') == '567') : false})(), 'message: The remove method removes leaf nodes from the tree');", "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; test.add(5); test.add(3); test.add(7); test.add(6); test.add(10); test.add(12); test.remove(3); test.remove(12); test.remove(10); return (typeof test.remove == 'function') ? (test.inorder().join('') == '567') : false})(), 'message: The remove method removes leaf nodes from the tree');",
"assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(-1); test.add(3); test.add(7); test.add(16); test.remove(16); test.remove(7); test.remove(3); return (test.inorder().join('') == '-1'); })(), 'message: The remove method removes nodes with one child.');", "assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(-1); test.add(3); test.add(7); test.add(16); test.remove(16); test.remove(7); test.remove(3); return (test.inorder().join('') == '-1'); })(), 'message: The remove method removes nodes with one child.');",