Implemented the Binary Search Tree Exercise#182
Conversation
rmonnet
commented
Feb 3, 2026
For some reason, the problem spec shows tests verifying the structure of the tree nodes and other tests verifying the tree content is sorted. Since we leave it to the student to implement the Node data structure we can't really check the structure of the tree itself. The set of tests checking the structure of the tree after various combinations of `insert()` is redundant with the tests checking the tree is properly sorted since the only way to get the values sorted is to have the tree structure correct so we skiped these redundant tests (and marked them so in tests.toml). The alternative would be to provide the structure of the Node to the student to ensure we can test it. This doesn't seem as interesting an exercise.
|
I disagree. There's really only one way to implement a binary search tree. I fear if we don't enforce some structure, then students will just fall back to a dynamic array sorted. Maybe instead of having to spit out a string representation of the whole tree, if the tests are written like the ruby track with chained "left" and "right" accessors, it might be a bit more interesting. |
|
Yes, I thought about somebody just using a dynamic array and the sort function, but being an optimist, I thought "surely nobody would do that!". Anyway :-) I looked at the Ruby track and I think it is easier for them because Ruby is an interpreted language and the test fail, not really because a compile error but with a "NoMethodError: undefined method `left' for an instance of Bst" message. That's kind of the same but it is more graceful and we have been trying not to show the student compile errors in the Odin exercises (not on a solution stub anyway). I can definitively go ahead and add the definition of the Node. If I do that, I can definitively implement the tests I skipped and if they were to switch to a dynamic array then they would get compile errors (but not on a solution stub which was our goal). If I don't see any objection, I'll work that out tomorrow. |
Included the definition of Node in the solution stub, this is acceptable since "there is only one way to implement a binary search tree". With the Node definition, we can re-include the five skipped tests that just check that the constructed tree has the expected structure for all constructed nodes. Remove the include/comment from tests.toml that documented the skipped tests. Note that the resulting additional tests are way messier than in a language like Ruby. The main reasons are: 1. Odin tests are more verbose than ruby tests (`testing.expect...()`) 2. We need to deal with null nodes in the tree. Each node we want to check needs to be non-nil or this will crash the test.
|
@glennj, see if you like this version better. I added the Node definition in the solution stub and implemented the 5 tests dealing with the tree structure. |
|
I think the |
|
@glennj, all done! |