binary-search-tree: left() and right() return modifiable subtree
まだ誰も着手していません。
評価
調査の方向性
binary-search-tree の演習から始め、example.h を調べて、binary_tree::left()、binary_tree::right()、insert() に注目してください。それらの所有権と const 性を、issue に示されている現在のテストと比較してください。完了とは、例とテストが、木の不変条件を無効にできない const-safe な部分木 API で一致している状態です。
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- C++
- スター
- 291
- フォーク
- 244
- 平均マージ
- 17分
- マージ済み PR(30日)
- 1
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
exercism/cpp のほかの issue
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
ros2/message_filters#338 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
subsurface/subsurface#4984 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
flutter-webrtc/flutter-webrtc#2206 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
google-ai-edge/LiteRT-LM#3739 ·