Hacktoberfest 2026: die Issues, die Maintainer für den Oktober markiert haben – offen und einsteigerfreundlich. Hacktoberfest-Issues durchsuchen

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

Offen
#264 6 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Anfängerfreundlichkeit
25/100
Issue-Typ
Bug
Klarheit
Muss geklärt werden
Aktivitätsstatus
Veraltet
Tech-Stack
cpp
Bereich
backend

Rechercherichtung

Beginne mit der Übung zum binären Suchbaum und untersuche example.h, wobei du dich auf binary_tree::left(), binary_tree::right() und insert() konzentrierst. Vergleiche deren Ownership und Constness mit den aktuellen Tests, die im Issue gezeigt werden. Erledigt ist die Aufgabe, wenn Beispiel und Tests mit einer const-sicheren Subtree-API übereinstimmen, die die Invariante des Baums nicht verletzen kann.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.

Vorherrschende Sprache
C++
Sterne
291
Forks
244
Ø Merge
17 Min.
Gemergte PRs (30 T.)
1

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus exercism/cpp

Alle Issues in exercism/cpp

Ähnliche Issues

Weitere Issues zu C++

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.