From b951eb308b75e41dc8c12cb1a7c670f0f08646e4 Mon Sep 17 00:00:00 2001 From: Kenneth-LJS Date: Mon, 6 Mar 2017 08:42:16 +0800 Subject: [PATCH] Clean up binary tree challenges (#13603) --- ...ng-interview-data-structure-questions.json | 211 +++++++++--------- 1 file changed, 106 insertions(+), 105 deletions(-) diff --git a/seed/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json b/seed/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json index 89cfb002db..c0b3bd47e7 100644 --- a/seed/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json +++ b/seed/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json @@ -1986,60 +1986,61 @@ "challengeSeed": [ "var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));", "function Node(value) {", - " this.value = value;", - " this.left = null;", - " this.right = null;", + " this.value = value;", + " this.left = null;", + " this.right = null;", "}", "", "function BinarySearchTree() {", - " this.root = null;", - " this.remove = function(value) {", - " if (this.root == null) {", - " return null;", - " };", - " var target;", - " var parent = null;", - " // find the target value and its parent", - " (function findValue(node = this.root) {", - " if (value == node.value) {", - " target = node;", - " } else if (value < node.value && node.left != null) {", - " parent = node;", - " return findValue(node.left);", - " } else if (value < node.value && node.left == null) {", - " return null;", - " } else if (value > node.value && node.right != null) {", - " parent = node;", - " return findValue(node.right);", - " } else {", - " return null;", - " };", - " }).bind(this)();", - " if (target == null) {", - " return null;", + " this.root = null;", + " this.remove = function(value) {", + " if (this.root === null) {", + " return null;", + " }", + " var target;", + " var parent = null;", + " // find the target value and its parent", + " (function findValue(node = this.root) {", + " if (value == node.value) {", + " target = node;", + " } else if (value < node.value && node.left !== null) {", + " parent = node;", + " return findValue(node.left);", + " } else if (value < node.value && node.left === null) {", + " return null;", + " } else if (value > node.value && node.right !== null) {", + " parent = node;", + " return findValue(node.right);", + " } else {", + " return null;", + " }", + " }).bind(this)();", + " if (target === 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", - " 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", + " }", + " }", + " // case 2: target has one child, change code below this line", + " };", "}" ], "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() } 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(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.');", @@ -2139,75 +2140,75 @@ "challengeSeed": [ "var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));", "function Node(value) {", - " this.value = value;", - " this.left = null;", - " this.right = null;", + " this.value = value;", + " this.left = null;", + " this.right = null;", "}", "", "function BinarySearchTree() {", - " this.root = null;", - " this.remove = function(value) {", - " if (this.root == null) {", - " return null;", - " };", - " var target;", - " var parent = null;", - " // find the target value and its parent", - " (function findValue(node = this.root) {", - " if (value == node.value) {", - " target = node;", - " } else if (value < node.value && node.left != null) {", - " parent = node;", - " return findValue(node.left);", - " } else if (value < node.value && node.left == null) {", - " return null;", - " } else if (value > node.value && node.right != null) {", - " parent = node;", - " return findValue(node.right);", - " } else {", - " return null;", - " };", - " }).bind(this)();", - " if (target == null) {", - " return null;", + " this.root = null;", + " this.remove = function(value) {", + " if (this.root === null) {", + " return null;", + " }", + " var target;", + " var parent = null;", + " // find the target value and its parent", + " (function findValue(node = this.root) {", + " if (value == node.value) {", + " target = node;", + " } else if (value < node.value && node.left !== null) {", + " parent = node;", + " return findValue(node.left);", + " } else if (value < node.value && node.left === null) {", + " return null;", + " } else if (value > node.value && node.right !== null) {", + " parent = node;", + " return findValue(node.right);", + " } else {", + " return null;", + " }", + " }).bind(this)();", + " if (target === 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", - " 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", - " else if (children == 1) {", - " 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", - " };", + " }", + " }", + " // case 2: target has one child", + " else if (children == 1) {", + " 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": [ "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') ? (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(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.');",