Clean up binary tree challenges (#13603)
This commit is contained in:
		
				
					committed by
					
						
						Quincy Larson
					
				
			
			
				
	
			
			
			
						parent
						
							217c47fae7
						
					
				
				
					commit
					b951eb308b
				
			@@ -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.');",
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user