binary-search-tree: left() and right() return modifiable subtree
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Aptitud para principiantes
- 25/100
Línea de trabajo
Comienza con el ejercicio de binary-search-tree e inspecciona example.h, centrándote en binary_tree::left(), binary_tree::right() e insert(). Compara su ownership y constness con las pruebas actuales mostradas en el issue. Se considera terminado cuando el ejemplo y las pruebas coinciden en una API de subárbol const-safe que no puede invalidar el invariante del árbol.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
In the exercise "binary-search-tree" the methods left() and right() are hard to implement correctly. The example implementation itself is IMHO incorrect.
The tests look like this:
template<typename T>
using tree_ptr = typename std::unique_ptr<binary_tree::binary_tree<T>>;
template<typename T>
static void test_leaf(const tree_ptr<T> &tree, const T& data, bool has_left, bool has_right)
//...
test_leaf<uint32_t>(tested->left(), 2, false, false);
That forces implementations of binary_tree::left() (and binary_tree::right()) to return a tree_ptr reference or rvaue which points to a non-const subtree.
Remember: const std::unique_ptr<some_type> means that the unique_ptr itself is const, not the object it points to.
Somebody could take the example implementation (example.h) and write
auto tree = binary_tree::binary_tree<int>(100);
tree.insert(50);
tree.left()->insert(150);
and thus invalidate the invariant of tree.
I consider any implementation of a binary search tree that can be corrupted that way as faulty. I came up with three alternatives that avoid this issue and pass the current tests:
binary_treecould have a member variableallow_insertthat istruefor the root andfalsefor all subtreesbinary_treecould have a parent pointer andinsert()could check for each of its parents if the new value violates that parent's invariantleft()andright()could copy the subtree and return aunique_ptrto that copy.
But IMHO none of those alternatives feels right.
The easiest solution would be if left() and right() could return const raw pointers. But one could argue that raw pointers result in unclear ownership.
- Lenguaje dominante
- C++
- Estrellas
- 291
- Forks
- 244
- Merge medio
- 17 min
- PR fusionados (30 d)
- 1
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de exercism/cpp
-
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
-
Dificultad 4/5 3-5 días Aptitud para principiantes 48/100
-
Dificultad 4/5 3-5 días Aptitud para principiantes 45/100
-
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
-
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
Todos los issues de exercism/cpp
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
-
good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
ros2/message_filters#338 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
subsurface/subsurface#4984 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
flutter-webrtc/flutter-webrtc#2206 ·
-
litertlm-android AAR ships no consumer ProGuard rules → "mid == null" SIGABRT in minified apps Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
google-ai-edge/LiteRT-LM#3739 ·