6.9 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 587d8258367417b2b2512c81 | Eliminar un nodo con un Hijo en un árbol binario de búsqueda | 1 | 301638 | delete-a-node-with-one-child-in-a-binary-search-tree |
--description--
Ahora que podemos eliminar nodos hoja pasaremos el segundo caso: eliminar un nodo con un hijo. Para este caso, digamos que tenemos un árbol con los siguientes nodos 1 - 2 - 3 donde 1 es la raíz. Para eliminar 2, simplemente necesitamos hacer que la referencia derecha en 1 apunte a 3. Más generalmente para eliminar un nodo con sólo un hijo, hacemos que la referencia padre del nodo sea el siguiente nodo en el árbol.
--instructions--
Hemos proporcionado algún código en nuestro método remove que cumple con las tareas del último desafío. Encontramos el objetivo a eliminar y su padre y definimos el número de hijos que el nodo objetivo tiene. Vamos a añadir el siguiente caso aquí para los nodos objetivos con sólo un hijo. Aquí, tendremos que determinar si el hijo individual es un hijo izquierdo o derecho y luego establecer la referencia correcta en el padre para apuntar a este nodo. Adicionalmente, vamos a tener en cuenta el caso done el objetivo en el nodo raíz (esto significa que el padre será null). Sientéte libre de reemplazar el código inicial por el tuyo mientras pase las pruebas.
--hints--
La estructura de datos BinarySearchTree debe existir.
assert(
(function () {
var test = false;
if (typeof BinarySearchTree !== 'undefined') {
test = new BinarySearchTree();
}
return typeof test == 'object';
})()
);
El árbol binario de búsqueda debe tener un método llamado remove.
assert(
(function () {
var test = false;
if (typeof BinarySearchTree !== 'undefined') {
test = new BinarySearchTree();
} else {
return false;
}
return typeof test.remove == 'function';
})()
);
Tratar de eliminar un elemento que no existe debe devolver null.
assert(
(function () {
var test = false;
if (typeof BinarySearchTree !== 'undefined') {
test = new BinarySearchTree();
} else {
return false;
}
if (typeof test.remove !== 'function') {
return false;
}
return test.remove(100) == null;
})()
);
Si el nodo raíz no tiene hijos, eliminar debe establecer la raíz a null.
assert(
(function () {
var test = false;
if (typeof BinarySearchTree !== 'undefined') {
test = new BinarySearchTree();
} else {
return false;
}
if (typeof test.remove !== 'function') {
return false;
}
test.add(500);
test.remove(500);
return test.inorder() == null;
})()
);
El método remove debe nodos hojas del árbol.
assert(
(function () {
var test = false;
if (typeof BinarySearchTree !== 'undefined') {
test = new BinarySearchTree();
} else {
return false;
}
if (typeof test.remove !== 'function') {
return false;
}
test.add(5);
test.add(3);
test.add(7);
test.add(6);
test.add(10);
test.add(12);
test.remove(3);
test.remove(12);
test.remove(10);
return test.inorder().join('') == '567';
})()
);
El método remove debe eliminar nodos con un hijo.
assert(
(function () {
var test = false;
if (typeof BinarySearchTree !== 'undefined') {
test = new BinarySearchTree();
} else {
return false;
}
if (typeof test.remove !== 'function') {
return false;
}
test.add(1);
test.add(4);
test.add(3);
test.add(2);
test.add(6);
test.add(8);
test.remove(6);
test.remove(3);
return test.inorder().join('') == '1248';
})()
);
Eliminar la raíz en un árbol con dos nodos debe establecer el segundo para ser la raíz.
assert(
(function () {
var test = false;
if (typeof BinarySearchTree !== 'undefined') {
test = new BinarySearchTree();
} else {
return false;
}
if (typeof test.remove !== 'function') {
return false;
}
test.add(15);
test.add(27);
test.remove(15);
return test.inorder().join('') == '27';
})()
);
--seed--
--after-user-code--
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;
}
}
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;
}
}
}
);
--seed-contents--
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;
this.remove = function(value) {
if (this.root === null) {
return null;
}
var target;
var parent = null;
// Find the target value and its parent
(function findValue(node = this.root) {
if (value == node.value) {
target = node;
} else if (value < node.value && node.left !== null) {
parent = node;
return findValue(node.left);
} else if (value < node.value && node.left === null) {
return null;
} else if (value > node.value && node.right !== null) {
parent = node;
return findValue(node.right);
} else {
return null;
}
}.bind(this)());
if (target === null) {
return null;
}
// Count the children of the target to delete
var children =
(target.left !== null ? 1 : 0) + (target.right !== null ? 1 : 0);
// Case 1: Target has no children
if (children === 0) {
if (target == this.root) {
this.root = null;
} else {
if (parent.left == target) {
parent.left = null;
} else {
parent.right = null;
}
}
}
// Case 2: Target has one child
// Only change code below this line
};
}
--solutions--
// solution required