From 85e7fe45d500fec1098d6c3ec76a7f12ba5584ed Mon Sep 17 00:00:00 2001 From: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Date: Fri, 13 Dec 2019 13:04:05 -0800 Subject: [PATCH] fix(curriculum): use object.assign to prevent overwriting prototypes if user uses class instead of function (#37648) * fix: use object.assign to prevent overwriting when using class * fix: remove non-used isBinarySearchTree method --- ...element-to-a-binary-search-tree.english.md | 57 ++----- ...present-in-a-binary-search-tree.english.md | 53 +++---- .../create-a-doubly-linked-list.english.md | 10 +- ...af-node-in-a-binary-search-tree.english.md | 114 ++++++-------- ...e-child-in-a-binary-search-tree.english.md | 114 ++++++-------- ...hildren-in-a-binary-search-tree.english.md | 141 +++++++++--------- ...-height-of-a-binary-search-tree.english.md | 22 +-- ...m-value-in-a-binary-search-tree.english.md | 22 +-- .../invert-a-binary-tree.english.md | 88 +++++------ .../reverse-a-doubly-linked-list.english.md | 83 ++++++----- ...-search-in-a-binary-search-tree.english.md | 21 +-- ...-search-in-a-binary-search-tree.english.md | 23 +-- 12 files changed, 353 insertions(+), 395 deletions(-) diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/add-a-new-element-to-a-binary-search-tree.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/add-a-new-element-to-a-binary-search-tree.english.md index 82cb596635..cafec3f7de 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/add-a-new-element-to-a-binary-search-tree.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/add-a-new-element-to-a-binary-search-tree.english.md @@ -64,50 +64,24 @@ function BinarySearchTree() {
```js -BinarySearchTree.prototype = { - isBinarySearchTree() { - if (this.root == null) { - return null; - } else { - var check = true; - function checkTree(node) { - if (node.left != null) { - var left = node.left; - if (left.value > node.value) { - check = false; - } else { - checkTree(left); - } - } - if (node.right != null) { - var right = node.right; - if (right.value < node.value) { - check = false; - } else { - checkTree(right); - } - } +BinarySearchTree.prototype = Object.assign( + BinarySearchTree.prototype, + { + inOrder() { + if (!this.root) { + return null; } - checkTree(this.root); - return check; + 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; } } -}; -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; - } -}; +); ```
@@ -133,7 +107,6 @@ function BinarySearchTree() { const searchTree = function(current) { if (current.value > element) { if (current.left) { - //si existe return searchTree(current.left); } else { current.left = new Node(element); diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/check-if-an-element-is-present-in-a-binary-search-tree.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/check-if-an-element-is-present-in-a-binary-search-tree.english.md index 13315c5f2a..246689953b 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/check-if-an-element-is-present-in-a-binary-search-tree.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/check-if-an-element-is-present-in-a-binary-search-tree.english.md @@ -61,36 +61,39 @@ function BinarySearchTree() {
```js -BinarySearchTree.prototype = { - add: function(value) { - var node = this.root; - if (node == null) { - this.root = new Node(value); - return; - } else { - function searchTree(node) { - if (value < node.value) { - if (node.left == null) { - node.left = new Node(value); - return; - } else if (node.left != null) { - return searchTree(node.left); +BinarySearchTree.prototype = Object.assign( + BinarySearchTree.prototype, + { + add: function(value) { + var node = this.root; + if (node == null) { + this.root = new Node(value); + return; + } else { + function searchTree(node) { + if (value < node.value) { + if (node.left == null) { + node.left = new Node(value); + return; + } else if (node.left != null) { + return searchTree(node.left); + } + } else if (value > node.value) { + if (node.right == null) { + node.right = new Node(value); + return; + } else if (node.right != null) { + return searchTree(node.right); + } + } else { + return null; } - } else if (value > node.value) { - if (node.right == null) { - node.right = new Node(value); - return; - } else if (node.right != null) { - return searchTree(node.right); - } - } else { - return null; } + return searchTree(node); } - return searchTree(node); } } -}; +); ```
diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-doubly-linked-list.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-doubly-linked-list.english.md index 15813dfc2f..c3bc59a6a9 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-doubly-linked-list.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-doubly-linked-list.english.md @@ -68,7 +68,11 @@ var DoublyLinkedList = function() {
```js -DoublyLinkedList.prototype = { + +DoublyLinkedList.prototype = Object.assign( + DoublyLinkedList.prototype, + { + print() { if (this.head == null) { return null; @@ -96,8 +100,8 @@ DoublyLinkedList.prototype = { result.push(node.data); return result; }; - } -}; + } +}); ```
diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/delete-a-leaf-node-in-a-binary-search-tree.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/delete-a-leaf-node-in-a-binary-search-tree.english.md index 5f1288a3bb..e54507d00d 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/delete-a-leaf-node-in-a-binary-search-tree.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/delete-a-leaf-node-in-a-binary-search-tree.english.md @@ -68,81 +68,57 @@ function BinarySearchTree() {
```js -BinarySearchTree.prototype = { - add: function(value) { - var node = this.root; - if (node == null) { - this.root = new Node(value); - return; - } else { - function searchTree(node) { - if (value < node.value) { - if (node.left == null) { - node.left = new Node(value); - return; - } else if (node.left != null) { - return searchTree(node.left); - } - } else if (value > node.value) { - if (node.right == null) { - node.right = new Node(value); - return; - } else if (node.right != null) { - return searchTree(node.right); - } - } else { - return null; - } - } - return searchTree(node); - } - }, - inorder: function() { - if (this.root == null) { - return null; - } else { - var result = new Array(); - function traverseInOrder(node) { - if (node.left != null) { - traverseInOrder(node.left); - } - result.push(node.value); - if (node.right != null) { - traverseInOrder(node.right); - } - } - traverseInOrder(this.root); - return result; - } - }, - isBinarySearchTree() { - if (this.root == null) { - return null; - } else { - var check = true; - function checkTree(node) { - if (node.left != null) { - var left = node.left; - if (left.value > node.value) { - check = false; +BinarySearchTree.prototype = Object.assign( + BinarySearchTree.prototype, + { + add: function(value) { + var node = this.root; + if (node == null) { + this.root = new Node(value); + return; + } else { + function searchTree(node) { + if (value < node.value) { + if (node.left == null) { + node.left = new Node(value); + return; + } else if (node.left != null) { + return searchTree(node.left); + } + } else if (value > node.value) { + if (node.right == null) { + node.right = new Node(value); + return; + } else if (node.right != null) { + return searchTree(node.right); + } } else { - checkTree(left); - } - } - if (node.right != null) { - var right = node.right; - if (right.value < node.value) { - check = false; - } else { - checkTree(right); + return null; } } + return searchTree(node); + } + }, + inorder: function() { + if (this.root == null) { + return null; + } else { + var result = new Array(); + function traverseInOrder(node) { + if (node.left != null) { + traverseInOrder(node.left); + } + result.push(node.value); + if (node.right != null) { + traverseInOrder(node.right); + } + } + traverseInOrder(this.root); + return result; } - checkTree(this.root); - return check; } } -}; +); ```
diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/delete-a-node-with-one-child-in-a-binary-search-tree.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/delete-a-node-with-one-child-in-a-binary-search-tree.english.md index 45fc931b75..1d20c1e688 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/delete-a-node-with-one-child-in-a-binary-search-tree.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/delete-a-node-with-one-child-in-a-binary-search-tree.english.md @@ -106,81 +106,57 @@ function BinarySearchTree() {
```js -BinarySearchTree.prototype = { - add: function(value) { - var node = this.root; - if (node == null) { - this.root = new Node(value); - return; - } else { - function searchTree(node) { - if (value < node.value) { - if (node.left == null) { - node.left = new Node(value); - return; - } else if (node.left != null) { - return searchTree(node.left); - } - } else if (value > node.value) { - if (node.right == null) { - node.right = new Node(value); - return; - } else if (node.right != null) { - return searchTree(node.right); - } - } else { - return null; - } - } - return searchTree(node); - } - }, - inorder: function() { - if (this.root == null) { - return null; - } else { - var result = new Array(); - function traverseInOrder(node) { - if (node.left != null) { - traverseInOrder(node.left); - } - result.push(node.value); - if (node.right != null) { - traverseInOrder(node.right); - } - } - traverseInOrder(this.root); - return result; - } - }, - isBinarySearchTree() { - if (this.root == null) { - return null; - } else { - var check = true; - function checkTree(node) { - if (node.left != null) { - var left = node.left; - if (left.value > node.value) { - check = false; +BinarySearchTree.prototype = Object.assign( + BinarySearchTree.prototype, + { + add: function(value) { + var node = this.root; + if (node == null) { + this.root = new Node(value); + return; + } else { + function searchTree(node) { + if (value < node.value) { + if (node.left == null) { + node.left = new Node(value); + return; + } else if (node.left != null) { + return searchTree(node.left); + } + } else if (value > node.value) { + if (node.right == null) { + node.right = new Node(value); + return; + } else if (node.right != null) { + return searchTree(node.right); + } } else { - checkTree(left); - } - } - if (node.right != null) { - var right = node.right; - if (right.value < node.value) { - check = false; - } else { - checkTree(right); + return null; } } + return searchTree(node); + } + }, + inorder: function() { + if (this.root == null) { + return null; + } else { + var result = new Array(); + function traverseInOrder(node) { + if (node.left != null) { + traverseInOrder(node.left); + } + result.push(node.value); + if (node.right != null) { + traverseInOrder(node.right); + } + } + traverseInOrder(this.root); + return result; } - checkTree(this.root); - return check; } } -}; +); ```
diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/delete-a-node-with-two-children-in-a-binary-search-tree.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/delete-a-node-with-two-children-in-a-binary-search-tree.english.md index cd6bd7ea81..a7e48c8dc4 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/delete-a-node-with-two-children-in-a-binary-search-tree.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/delete-a-node-with-two-children-in-a-binary-search-tree.english.md @@ -125,81 +125,84 @@ function BinarySearchTree() {
```js -BinarySearchTree.prototype = { - add: function(value) { - var node = this.root; - if (node == null) { - this.root = new Node(value); - return; - } else { - function searchTree(node) { - if (value < node.value) { - if (node.left == null) { - node.left = new Node(value); - return; - } else if (node.left != null) { - return searchTree(node.left); - } - } else if (value > node.value) { - if (node.right == null) { - node.right = new Node(value); - return; - } else if (node.right != null) { - return searchTree(node.right); - } - } else { - return null; - } - } - return searchTree(node); - } - }, - inorder: function() { - if (this.root == null) { - return null; - } else { - var result = new Array(); - function traverseInOrder(node) { - if (node.left != null) { - traverseInOrder(node.left); - } - result.push(node.value); - if (node.right != null) { - traverseInOrder(node.right); - } - } - traverseInOrder(this.root); - return result; - } - }, - isBinarySearchTree() { - if (this.root == null) { - return null; - } else { - var check = true; - function checkTree(node) { - if (node.left != null) { - var left = node.left; - if (left.value > node.value) { - check = false; +BinarySearchTree.prototype = Object.assign( + BinarySearchTree.prototype, + { + add: function(value) { + var node = this.root; + if (node == null) { + this.root = new Node(value); + return; + } else { + function searchTree(node) { + if (value < node.value) { + if (node.left == null) { + node.left = new Node(value); + return; + } else if (node.left != null) { + return searchTree(node.left); + } + } else if (value > node.value) { + if (node.right == null) { + node.right = new Node(value); + return; + } else if (node.right != null) { + return searchTree(node.right); + } } else { - checkTree(left); - } - } - if (node.right != null) { - var right = node.right; - if (right.value < node.value) { - check = false; - } else { - checkTree(right); + return null; } } + return searchTree(node); + } + }, + inorder: function() { + if (this.root == null) { + return null; + } else { + var result = new Array(); + function traverseInOrder(node) { + if (node.left != null) { + traverseInOrder(node.left); + } + result.push(node.value); + if (node.right != null) { + traverseInOrder(node.right); + } + } + traverseInOrder(this.root); + return result; + } + }, + isBinarySearchTree() { + if (this.root == null) { + return null; + } else { + var check = true; + function checkTree(node) { + if (node.left != null) { + var left = node.left; + if (left.value > node.value) { + check = false; + } else { + checkTree(left); + } + } + if (node.right != null) { + var right = node.right; + if (right.value < node.value) { + check = false; + } else { + checkTree(right); + } + } + } + checkTree(this.root); + return check; } - checkTree(this.root); - return check; } } -}; +); ```
diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/find-the-minimum-and-maximum-height-of-a-binary-search-tree.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/find-the-minimum-and-maximum-height-of-a-binary-search-tree.english.md index dfe2d6244a..41021e359c 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/find-the-minimum-and-maximum-height-of-a-binary-search-tree.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/find-the-minimum-and-maximum-height-of-a-binary-search-tree.english.md @@ -66,13 +66,10 @@ function BinarySearchTree() {
```js -BinarySearchTree.prototype = { - add: function(value) { - var node = this.root; - if (node == null) { - this.root = new Node(value); - return; - } else { +BinarySearchTree.prototype = Object.assign( + BinarySearchTree.prototype, + { + add: function(value) { function searchTree(node) { if (value < node.value) { if (node.left == null) { @@ -92,10 +89,17 @@ BinarySearchTree.prototype = { return null; } } - return searchTree(node); + + var node = this.root; + if (node == null) { + this.root = new Node(value); + return; + } else { + return searchTree(node); + } } } -}; +); ```
diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/find-the-minimum-and-maximum-value-in-a-binary-search-tree.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/find-the-minimum-and-maximum-value-in-a-binary-search-tree.english.md index e2cb2364a4..2141c693e7 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/find-the-minimum-and-maximum-value-in-a-binary-search-tree.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/find-the-minimum-and-maximum-value-in-a-binary-search-tree.english.md @@ -61,13 +61,10 @@ function BinarySearchTree() {
```js -BinarySearchTree.prototype = { - add: function(value) { - var node = this.root; - if (node == null) { - this.root = new Node(value); - return; - } else { +BinarySearchTree.prototype = Object.assign( + BinarySearchTree.prototype, + { + add: function(value) { function searchTree(node) { if (value < node.value) { if (node.left == null) { @@ -87,10 +84,17 @@ BinarySearchTree.prototype = { return null; } } - return searchTree(node); + + var node = this.root; + if (node == null) { + this.root = new Node(value); + return; + } else { + return searchTree(node); + } } } -}; +); ```
diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/invert-a-binary-tree.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/invert-a-binary-tree.english.md index 9d3d8f1d30..318f55e2ea 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/invert-a-binary-tree.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/invert-a-binary-tree.english.md @@ -58,54 +58,58 @@ function BinarySearchTree() {
```js -BinarySearchTree.prototype = { +BinarySearchTree.prototype = Object.assign( + BinarySearchTree.prototype, + { add: function(value) { - var node = this.root; - if (node == null) { - this.root = new Node(value); - return; + function searchTree(node) { + if (value < node.value) { + if (node.left == null) { + node.left = new Node(value); + return; + } else if (node.left != null) { + return searchTree(node.left) + }; + } else if (value > node.value) { + if (node.right == null) { + node.right = new Node(value); + return; + } else if (node.right != null) { + return searchTree(node.right); + }; } else { - function searchTree(node) { - if (value < node.value) { - if (node.left == null) { - node.left = new Node(value); - return; - } else if (node.left != null) { - return searchTree(node.left) - }; - } else if (value > node.value) { - if (node.right == null) { - node.right = new Node(value); - return; - } else if (node.right != null) { - return searchTree(node.right); - }; - } else { - return null; - }; - }; - return searchTree(node); + return null; }; + } + + var node = this.root; + if (node == null) { + this.root = new Node(value); + return; + } else { + return searchTree(node); + }; }, inorder: function() { - if (this.root == null) { - return null; - } else { - var result = new Array(); - function traverseInOrder(node) { - if (node.left != null) { - traverseInOrder(node.left); - }; - result.push(node.value); - if (node.right != null) { - traverseInOrder(node.right); - }; - } - traverseInOrder(this.root); - return result; - }; + if (this.root == null) { + return null; + } else { + var result = new Array(); + function traverseInOrder(node) { + if (node.left != null) { + traverseInOrder(node.left); + }; + result.push(node.value); + if (node.right != null) { + traverseInOrder(node.right); + }; + } + traverseInOrder(this.root); + return result; + }; } -}; + } +); ```
diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/reverse-a-doubly-linked-list.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/reverse-a-doubly-linked-list.english.md index c634f9fe67..08a4650c9e 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/reverse-a-doubly-linked-list.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/reverse-a-doubly-linked-list.english.md @@ -63,52 +63,55 @@ var DoublyLinkedList = function() {
```js -DoublyLinkedList.prototype = { - add(data) { - if (this.head == null) { - this.head = new Node(data, null); - this.tail = this.head; - } else { - var node = this.head; - var prev = null; - while (node.next != null) { - prev = node; - node = node.next; +DoublyLinkedList.prototype = Object.assign( + DoublyLinkedList.prototype, + { + add(data) { + if (this.head == null) { + this.head = new Node(data, null); + this.tail = this.head; + } else { + var node = this.head; + var prev = null; + while (node.next != null) { + prev = node; + node = node.next; + }; + var newNode = new Node(data, node); + node.next = newNode; + this.tail = newNode; }; - var newNode = new Node(data, node); - node.next = newNode; - this.tail = newNode; - }; - }, - print() { - if (this.head == null) { - return null; - } else { - var result = new Array(); - var node = this.head; - while (node.next != null) { + }, + print() { + if (this.head == null) { + return null; + } else { + var result = new Array(); + var node = this.head; + while (node.next != null) { + result.push(node.data); + node = node.next; + }; result.push(node.data); - node = node.next; + return result; }; - result.push(node.data); - return result; - }; - }, - printReverse() { - if (this.tail == null) { - return null; - } else { - var result = new Array(); - var node = this.tail; - while (node.prev != null) { + }, + printReverse() { + if (this.tail == null) { + return null; + } else { + var result = new Array(); + var node = this.tail; + while (node.prev != null) { + result.push(node.data); + node = node.prev; + }; result.push(node.data); - node = node.prev; + return result; }; - result.push(node.data); - return result; - }; + } } -}; +); ```
diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-breadth-first-search-in-a-binary-search-tree.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-breadth-first-search-in-a-binary-search-tree.english.md index e15df85f49..2c5bbc1bef 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-breadth-first-search-in-a-binary-search-tree.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-breadth-first-search-in-a-binary-search-tree.english.md @@ -64,13 +64,10 @@ function BinarySearchTree() {
```js -BinarySearchTree.prototype = { - add: function(value) { - var node = this.root; - if (node == null) { - this.root = new Node(value); - return; - } else { +BinarySearchTree.prototype = Object.assign( + BinarySearchTree.prototype, + { + add: function(value) { function searchTree(node) { if (value < node.value) { if (node.left == null) { @@ -90,10 +87,16 @@ BinarySearchTree.prototype = { return null; } } - return searchTree(node); + var node = this.root; + if (node == null) { + this.root = new Node(value); + return; + } else { + return searchTree(node); + } } } -}; +); ```
diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-depth-first-search-in-a-binary-search-tree.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-depth-first-search-in-a-binary-search-tree.english.md index bb35aadb2a..914799aab3 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-depth-first-search-in-a-binary-search-tree.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-depth-first-search-in-a-binary-search-tree.english.md @@ -76,13 +76,10 @@ function BinarySearchTree() {
```js -BinarySearchTree.prototype = { - add: function(value) { - var node = this.root; - if (node == null) { - this.root = new Node(value); - return; - } else { +BinarySearchTree.prototype = Object.assign( + BinarySearchTree.prototype, + { + add: function(value) { function searchTree(node) { if (value < node.value) { if (node.left == null) { @@ -102,10 +99,18 @@ BinarySearchTree.prototype = { return null; } } - return searchTree(node); + + var node = this.root; + if (node == null) { + this.root = new Node(value); + return; + } else { + return searchTree(node); + } } } -}; +); + ```