2021-06-15 07:49:18 +00:00
---
id: 5cc0c1b32479e176caf3b422
2021-07-22 19:11:52 +00:00
title: Controlla se l'albero è un albero binario di ricerca
2021-06-15 07:49:18 +00:00
challengeType: 1
forumTopicId: 301624
dashedName: check-if-tree-is-binary-search-tree
---
# --description--
2021-07-22 19:11:52 +00:00
Dal momento che sai già cos'è un albero binario di ricerca, questa sfida stabilirà come è possibile stabilire se un albero è un albero binario di ricerca o no.
2021-06-15 07:49:18 +00:00
2021-07-22 19:11:52 +00:00
Quello che contraddistingue un albero binario di ricerca è che i nodi sono ordinati in modo organizzato. I nodi hanno al massimo 2 nodi figli (posizionati a destra e/o a sinistra) in base al fatto che il valore del nodo figlio sia maggiore o uguale a (destra) o inferiore a (sinistra) quello del nodo principale.
2021-06-15 07:49:18 +00:00
# --instructions--
2021-07-22 19:11:52 +00:00
In questa sfida, creerai un'utilità per il tuo albero. Scrivi un metodo JavaScript `isBinarySearchTree` che prende un albero come input e restituisce un valore booleano a seconda se l'albero è un albero binario di ricerca o no. Usa la ricorsione quando possibile.
2021-06-15 07:49:18 +00:00
# --hints--
2021-07-22 19:11:52 +00:00
Il tuo albero di ricerca binario dovrebbe restituire true quando controllato con `isBinarySearchTree()` .
2021-06-15 07:49:18 +00:00
```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;
})()
);
```
2022-02-01 20:32:53 +00:00
`isBinarySearchTree()` dovrebbe restituire false quando riceve un albero che non è un albero di ricerca binario.
```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;
})()
);
```
2021-06-15 07:49:18 +00:00
# --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
2021-07-10 04:23:54 +00:00
2021-06-15 07:49:18 +00:00
// 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;
2022-02-01 20:32:53 +00:00
if (left.value >= node.value) {
2021-06-15 07:49:18 +00:00
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;
}
};
```