Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

binary-search-tree: left() and right() return modifiable subtree

Aperta
#264 6 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
cpp
Ambito
backend

Direzione di ricerca

Inizia con l’esercizio di binary-search-tree ed esamina example.h, concentrandoti su binary_tree::left(), binary_tree::right() e insert(). Confronta il loro ownership e la loro constness con i test attuali mostrati nell’issue. Il lavoro è completato quando l’esempio e i test concordano su un’API del sottoalbero const-safe che non può invalidare l’invariante dell’albero.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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_tree could have a member variable allow_insert that is true for the root and false for all subtrees
  • binary_tree could have a parent pointer and insert() could check for each of its parents if the new value violates that parent's invariant
  • left() and right() could copy the subtree and return a unique_ptr to 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.

Lingua principale
C++
Stelle
291
Fork
244
Merge medio
17m
PR unite (30g)
1

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di exercism/cpp

Tutte le issue di exercism/cpp

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.