mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-02-25 05:03:48 -05:00
155 lines
3.6 KiB
Markdown
155 lines
3.6 KiB
Markdown
---
|
|
id: 5cc0c1b32479e176caf3b422
|
|
title: Verificar si un árbol es un Árbol Binario de Búsqueda
|
|
challengeType: 1
|
|
forumTopicId: 301624
|
|
dashedName: check-if-tree-is-binary-search-tree
|
|
---
|
|
|
|
# --description--
|
|
|
|
Como ya sabes lo que es un árbol binario de búsqueda, este desafío establecerá como es que puedes decir que un árbol es un árbol binario de búsqueda o no lo es.
|
|
|
|
La principal distinción de un árbol de binario de búsqueda es que los nodos están ordenados de forma organizada. Los nodos tienen como máximo 2 nodos hijos (ubicados a la derecha y/o a la izquierda) basados en si el valor de los nodos hijos es mayor o igual que (derecha) o menor que (izquierda) el nodo padre.
|
|
|
|
# --instructions--
|
|
|
|
En este desafío, crearás una utilidad para tu árbol. Escribe un método en JavaScript `isBinarySearchTree` el cual tomará un árbol como entrada y devolverá un valor booleano si el árbol es un árbol binario de búsqueda o no. Usa recursividad cuando sea posible.
|
|
|
|
# --hints--
|
|
|
|
Tu árbol binario de búsqueda debe devolver true cuando se compruebe con `isBinarySearchTree()`.
|
|
|
|
```js
|
|
assert(
|
|
(function () {
|
|
var test = false;
|
|
if (typeof BinarySearchTree !== 'undefined') {
|
|
test = new BinarySearchTree();
|
|
} else {
|
|
return false;
|
|
}
|
|
test.push(1);
|
|
test.push(5);
|
|
test.push(3);
|
|
test.push(2);
|
|
test.push(4);
|
|
return isBinarySearchTree(test) == true;
|
|
})()
|
|
);
|
|
```
|
|
|
|
`isBinarySearchTree()` debe devolver false cuando compruebe que un árbol no es un árbol binario de búsqueda.
|
|
|
|
```js
|
|
assert(
|
|
(function () {
|
|
var test = false;
|
|
if (typeof BinarySearchTree !== 'undefined') {
|
|
test = new BinarySearchTree();
|
|
} else {
|
|
return false;
|
|
}
|
|
test.push(1);
|
|
test.root.left = new Node(1);
|
|
return isBinarySearchTree(test) == false;
|
|
})()
|
|
);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --after-user-code--
|
|
|
|
```js
|
|
BinarySearchTree.prototype.push = function(val) {
|
|
var root = this.root;
|
|
|
|
if (!root) {
|
|
this.root = new Node(val);
|
|
return;
|
|
}
|
|
|
|
var currentNode = root;
|
|
var newNode = new Node(val);
|
|
|
|
while (currentNode) {
|
|
if (val < currentNode.value) {
|
|
if (!currentNode.left) {
|
|
currentNode.left = newNode;
|
|
break;
|
|
} else {
|
|
currentNode = currentNode.left;
|
|
}
|
|
} else {
|
|
if (!currentNode.right) {
|
|
currentNode.right = newNode;
|
|
break;
|
|
} else {
|
|
currentNode = currentNode.right;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
```
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
|
function Node(value) {
|
|
this.value = value;
|
|
this.left = null;
|
|
this.right = null;
|
|
}
|
|
function BinarySearchTree() {
|
|
this.root = null;
|
|
}
|
|
function isBinarySearchTree(tree) {
|
|
// Only change code below this line
|
|
|
|
// Only change code above this line
|
|
}
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
|
function Node(value) {
|
|
this.value = value;
|
|
this.left = null;
|
|
this.right = null;
|
|
}
|
|
function BinarySearchTree() {
|
|
this.root = null;
|
|
}
|
|
function isBinarySearchTree(tree) {
|
|
if (tree.root == null) {
|
|
return null;
|
|
} else {
|
|
let isBST = true;
|
|
function checkTree(node) {
|
|
if (node.left != null) {
|
|
const left = node.left;
|
|
if (left.value >= node.value) {
|
|
isBST = false;
|
|
} else {
|
|
checkTree(left);
|
|
}
|
|
}
|
|
if (node.right != null) {
|
|
const right = node.right;
|
|
if (right.value < node.value) {
|
|
isBST = false;
|
|
} else {
|
|
checkTree(right);
|
|
}
|
|
}
|
|
}
|
|
checkTree(tree.root);
|
|
return isBST;
|
|
}
|
|
};
|
|
```
|