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
This commit is contained in:
@ -64,36 +64,9 @@ function BinarySearchTree() {
|
|||||||
<div id='js-teardown'>
|
<div id='js-teardown'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
BinarySearchTree.prototype = {
|
BinarySearchTree.prototype = Object.assign(
|
||||||
isBinarySearchTree() {
|
BinarySearchTree.prototype,
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
BinarySearchTree.prototype = {
|
|
||||||
inOrder() {
|
inOrder() {
|
||||||
if (!this.root) {
|
if (!this.root) {
|
||||||
return null;
|
return null;
|
||||||
@ -107,7 +80,8 @@ BinarySearchTree.prototype = {
|
|||||||
traverseInOrder(this.root);
|
traverseInOrder(this.root);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
@ -133,7 +107,6 @@ function BinarySearchTree() {
|
|||||||
const searchTree = function(current) {
|
const searchTree = function(current) {
|
||||||
if (current.value > element) {
|
if (current.value > element) {
|
||||||
if (current.left) {
|
if (current.left) {
|
||||||
//si existe
|
|
||||||
return searchTree(current.left);
|
return searchTree(current.left);
|
||||||
} else {
|
} else {
|
||||||
current.left = new Node(element);
|
current.left = new Node(element);
|
||||||
|
@ -61,7 +61,9 @@ function BinarySearchTree() {
|
|||||||
<div id='js-teardown'>
|
<div id='js-teardown'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
BinarySearchTree.prototype = {
|
BinarySearchTree.prototype = Object.assign(
|
||||||
|
BinarySearchTree.prototype,
|
||||||
|
{
|
||||||
add: function(value) {
|
add: function(value) {
|
||||||
var node = this.root;
|
var node = this.root;
|
||||||
if (node == null) {
|
if (node == null) {
|
||||||
@ -90,7 +92,8 @@ BinarySearchTree.prototype = {
|
|||||||
return searchTree(node);
|
return searchTree(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -68,7 +68,11 @@ var DoublyLinkedList = function() {
|
|||||||
<div id='js-teardown'>
|
<div id='js-teardown'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
DoublyLinkedList.prototype = {
|
|
||||||
|
DoublyLinkedList.prototype = Object.assign(
|
||||||
|
DoublyLinkedList.prototype,
|
||||||
|
{
|
||||||
|
|
||||||
print() {
|
print() {
|
||||||
if (this.head == null) {
|
if (this.head == null) {
|
||||||
return null;
|
return null;
|
||||||
@ -97,7 +101,7 @@ DoublyLinkedList.prototype = {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -68,7 +68,9 @@ function BinarySearchTree() {
|
|||||||
<div id='js-teardown'>
|
<div id='js-teardown'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
BinarySearchTree.prototype = {
|
BinarySearchTree.prototype = Object.assign(
|
||||||
|
BinarySearchTree.prototype,
|
||||||
|
{
|
||||||
add: function(value) {
|
add: function(value) {
|
||||||
var node = this.root;
|
var node = this.root;
|
||||||
if (node == null) {
|
if (node == null) {
|
||||||
@ -114,35 +116,9 @@ BinarySearchTree.prototype = {
|
|||||||
traverseInOrder(this.root);
|
traverseInOrder(this.root);
|
||||||
return result;
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
```
|
```
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -106,7 +106,9 @@ function BinarySearchTree() {
|
|||||||
<div id='js-teardown'>
|
<div id='js-teardown'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
BinarySearchTree.prototype = {
|
BinarySearchTree.prototype = Object.assign(
|
||||||
|
BinarySearchTree.prototype,
|
||||||
|
{
|
||||||
add: function(value) {
|
add: function(value) {
|
||||||
var node = this.root;
|
var node = this.root;
|
||||||
if (node == null) {
|
if (node == null) {
|
||||||
@ -152,35 +154,9 @@ BinarySearchTree.prototype = {
|
|||||||
traverseInOrder(this.root);
|
traverseInOrder(this.root);
|
||||||
return result;
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
```
|
```
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -125,7 +125,9 @@ function BinarySearchTree() {
|
|||||||
<div id='js-teardown'>
|
<div id='js-teardown'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
BinarySearchTree.prototype = {
|
BinarySearchTree.prototype = Object.assign(
|
||||||
|
BinarySearchTree.prototype,
|
||||||
|
{
|
||||||
add: function(value) {
|
add: function(value) {
|
||||||
var node = this.root;
|
var node = this.root;
|
||||||
if (node == null) {
|
if (node == null) {
|
||||||
@ -199,7 +201,8 @@ BinarySearchTree.prototype = {
|
|||||||
return check;
|
return check;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -66,13 +66,10 @@ function BinarySearchTree() {
|
|||||||
<div id='js-teardown'>
|
<div id='js-teardown'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
BinarySearchTree.prototype = {
|
BinarySearchTree.prototype = Object.assign(
|
||||||
|
BinarySearchTree.prototype,
|
||||||
|
{
|
||||||
add: function(value) {
|
add: function(value) {
|
||||||
var node = this.root;
|
|
||||||
if (node == null) {
|
|
||||||
this.root = new Node(value);
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
function searchTree(node) {
|
function searchTree(node) {
|
||||||
if (value < node.value) {
|
if (value < node.value) {
|
||||||
if (node.left == null) {
|
if (node.left == null) {
|
||||||
@ -92,10 +89,17 @@ BinarySearchTree.prototype = {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var node = this.root;
|
||||||
|
if (node == null) {
|
||||||
|
this.root = new Node(value);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
return searchTree(node);
|
return searchTree(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -61,13 +61,10 @@ function BinarySearchTree() {
|
|||||||
<div id='js-teardown'>
|
<div id='js-teardown'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
BinarySearchTree.prototype = {
|
BinarySearchTree.prototype = Object.assign(
|
||||||
|
BinarySearchTree.prototype,
|
||||||
|
{
|
||||||
add: function(value) {
|
add: function(value) {
|
||||||
var node = this.root;
|
|
||||||
if (node == null) {
|
|
||||||
this.root = new Node(value);
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
function searchTree(node) {
|
function searchTree(node) {
|
||||||
if (value < node.value) {
|
if (value < node.value) {
|
||||||
if (node.left == null) {
|
if (node.left == null) {
|
||||||
@ -87,10 +84,17 @@ BinarySearchTree.prototype = {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var node = this.root;
|
||||||
|
if (node == null) {
|
||||||
|
this.root = new Node(value);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
return searchTree(node);
|
return searchTree(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -58,13 +58,10 @@ function BinarySearchTree() {
|
|||||||
<div id='js-teardown'>
|
<div id='js-teardown'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
BinarySearchTree.prototype = {
|
BinarySearchTree.prototype = Object.assign(
|
||||||
|
BinarySearchTree.prototype,
|
||||||
|
{
|
||||||
add: function(value) {
|
add: function(value) {
|
||||||
var node = this.root;
|
|
||||||
if (node == null) {
|
|
||||||
this.root = new Node(value);
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
function searchTree(node) {
|
function searchTree(node) {
|
||||||
if (value < node.value) {
|
if (value < node.value) {
|
||||||
if (node.left == null) {
|
if (node.left == null) {
|
||||||
@ -83,7 +80,13 @@ BinarySearchTree.prototype = {
|
|||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
|
var node = this.root;
|
||||||
|
if (node == null) {
|
||||||
|
this.root = new Node(value);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
return searchTree(node);
|
return searchTree(node);
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
@ -105,7 +108,8 @@ BinarySearchTree.prototype = {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -63,7 +63,9 @@ var DoublyLinkedList = function() {
|
|||||||
<div id='js-teardown'>
|
<div id='js-teardown'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
DoublyLinkedList.prototype = {
|
DoublyLinkedList.prototype = Object.assign(
|
||||||
|
DoublyLinkedList.prototype,
|
||||||
|
{
|
||||||
add(data) {
|
add(data) {
|
||||||
if (this.head == null) {
|
if (this.head == null) {
|
||||||
this.head = new Node(data, null);
|
this.head = new Node(data, null);
|
||||||
@ -108,7 +110,8 @@ DoublyLinkedList.prototype = {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -64,13 +64,10 @@ function BinarySearchTree() {
|
|||||||
<div id='js-teardown'>
|
<div id='js-teardown'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
BinarySearchTree.prototype = {
|
BinarySearchTree.prototype = Object.assign(
|
||||||
|
BinarySearchTree.prototype,
|
||||||
|
{
|
||||||
add: function(value) {
|
add: function(value) {
|
||||||
var node = this.root;
|
|
||||||
if (node == null) {
|
|
||||||
this.root = new Node(value);
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
function searchTree(node) {
|
function searchTree(node) {
|
||||||
if (value < node.value) {
|
if (value < node.value) {
|
||||||
if (node.left == null) {
|
if (node.left == null) {
|
||||||
@ -90,10 +87,16 @@ BinarySearchTree.prototype = {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
var node = this.root;
|
||||||
|
if (node == null) {
|
||||||
|
this.root = new Node(value);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
return searchTree(node);
|
return searchTree(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -76,13 +76,10 @@ function BinarySearchTree() {
|
|||||||
<div id='js-teardown'>
|
<div id='js-teardown'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
BinarySearchTree.prototype = {
|
BinarySearchTree.prototype = Object.assign(
|
||||||
|
BinarySearchTree.prototype,
|
||||||
|
{
|
||||||
add: function(value) {
|
add: function(value) {
|
||||||
var node = this.root;
|
|
||||||
if (node == null) {
|
|
||||||
this.root = new Node(value);
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
function searchTree(node) {
|
function searchTree(node) {
|
||||||
if (value < node.value) {
|
if (value < node.value) {
|
||||||
if (node.left == null) {
|
if (node.left == null) {
|
||||||
@ -102,10 +99,18 @@ BinarySearchTree.prototype = {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var node = this.root;
|
||||||
|
if (node == null) {
|
||||||
|
this.root = new Node(value);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
return searchTree(node);
|
return searchTree(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
);
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
Reference in New Issue
Block a user