155 lines
3.5 KiB
Markdown
155 lines
3.5 KiB
Markdown
---
|
|
id: 5cc0c1b32479e176caf3b422
|
|
title: Controlla se l'albero è un albero binario di ricerca
|
|
challengeType: 1
|
|
forumTopicId: 301624
|
|
dashedName: check-if-tree-is-binary-search-tree
|
|
---
|
|
|
|
# --description--
|
|
|
|
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.
|
|
|
|
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.
|
|
|
|
# --instructions--
|
|
|
|
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.
|
|
|
|
# --hints--
|
|
|
|
Il tuo albero di ricerca binario dovrebbe restituire true quando controllato 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()` 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;
|
|
})()
|
|
);
|
|
```
|
|
|
|
# --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;
|
|
}
|
|
};
|
|
```
|