binary-search-tree: left() and right() return modifiable subtree
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
Start with the binary-search-tree exercise and inspect example.h, focusing on binary_tree::left(), binary_tree::right(), and insert(). Compare their ownership and constness with the current tests shown in the issue. Done means the example and tests agree on a const-safe subtree API that cannot invalidate the tree invariant.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- C++
- Stars
- 291
- Forks
- 244
- Avg merge
- 17m
- Merged PRs (30d)
- 1
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from exercism/cpp
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
ros2/message_filters#338 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
subsurface/subsurface#4984 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
flutter-webrtc/flutter-webrtc#2206 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
google-ai-edge/LiteRT-LM#3739 ·