10
, 12
, 17
, 25
. Siguiendo nuestras reglas para un árbol de búsqueda binario, agregaremos 12
a la derecha de 10
, 17
a la derecha de este, y 25
a la derecha de este. Ahora nuestro árbol se asemeja a una lista enlazada y recorrerla para encontrar 25
requeriría que recorriéramos todos los elementos de manera lineal. Por lo tanto, el tiempo lineal en el peor de los casos. El problema aquí es que el árbol está desequilibrado. Veremos un poco más sobre lo que esto significa en los siguientes desafíos. Instrucciones: En este desafío, crearemos una utilidad para nuestro árbol. Escriba un método isPresent
que toma un valor entero como entrada y devuelve un valor booleano para la presencia o ausencia de ese valor en el árbol de búsqueda binario. BinarySearchTree
existe.
testString: 'assert((function() { var test = false; if (typeof BinarySearchTree !== "undefined") { test = new BinarySearchTree() }; return (typeof test == "object")})(), "The BinarySearchTree
data structure exists.");'
- text: El árbol de búsqueda binario tiene un método llamado isPresent
.
testString: 'assert((function() { var test = false; if (typeof BinarySearchTree !== "undefined") { test = new BinarySearchTree() } else { return false; }; return (typeof test.isPresent == "function")})(), "The binary search tree has a method called isPresent
.");'
- text: El método isPresent
verifica correctamente la presencia o ausencia de elementos agregados al árbol.
testString: 'assert((function() { var test = false; if (typeof BinarySearchTree !== "undefined") { test = new BinarySearchTree() } else { return false; }; if (typeof test.isPresent !== "function") { return false; }; test.add(4); test.add(7); test.add(411); test.add(452); return ( test.isPresent(452) && test.isPresent(411) && test.isPresent(7) && !test.isPresent(100) ); })(), "The isPresent
method correctly checks for the presence or absence of elements added to the tree.");'
- text: isPresent
maneja casos donde el árbol está vacío.
testString: 'assert((function() { var test = false; if (typeof BinarySearchTree !== "undefined") { test = new BinarySearchTree() } else { return false; }; if (typeof test.isPresent !== "function") { return false; }; return test.isPresent(5) == false; })(), "isPresent
handles cases where the tree is empty.");'
```