This repository was archived by the owner on Oct 9, 2026. It is now read-only.
Repository navigation
Conversation
The two examples from the assignment handout, as input files for b_tree. They are the first inputs the tests and the README will use. Made by AI agent: Claude Sonnet 5.5 (claude-sonnet-5-5).
Pin down what the 2019 program does before any change, so every later commit can show that valid inputs still give the same output. The Makefile builds b_tree and runs the tests with `make test`. The runner tests/run.sh follows the one in cpp-deck-game: each case tests/cases/NAME.txt is run with the file as the argument, and the output is compared with NAME.out (exit status with NAME.code, stderr with NAME.err when these files exist). A .gitignore keeps the built binary out of the repository. Every .out file was produced by running the unchanged original program, not written by hand. The cases stay inside defined behaviour: a file argument, at most 2047 nodes, values that cannot overflow. They cover both figure examples, one node, no path, a path on one side only, negative and duplicate values, several paths per side (preorder wins), a path ending at an inner node and a tree of 1000 nodes. root_alone records a known bug on purpose: a target equal to the root's value prints the root alone as the left path. The fix commit will change this output. Made by AI agent: Claude Sonnet 5.5 (claude-sonnet-5-5).
No change in behaviour. The tests are untouched and still pass, and the output matched the 2019 build on 3000 random valid inputs. The loose structs and the create() initialiser become a Tree class: the constructor sets the defaults, the destructor frees all nodes, and copying is disabled because the tree owns its nodes. The left/right side is now passed down the search as an enum. This replaces the pow/log2 formula that guessed the side from the node number. The formula counted the root as left, so the root is still counted as left; this known bug is fixed in a later commit. Also: clearer names, short comments, 4-space indentation, no `using namespace std`, the author header lines like the sibling repos, and -Werror in the Makefile, since the code builds without warnings. Made by AI agent: Claude Sonnet 5.5 (claude-sonnet-5-5).
The 2019 program found the place of a new node with a route array of 10 entries. A tree of 2048 nodes or more needs 11 steps, so it wrote past the end of the array (undefined behaviour). The limit was never checked or documented. Keep a vector of node pointers where entry k-1 is node number k. The parent of node k is node k/2, so adding a node needs no route at all. The vector also owns the nodes, so the destructor is a plain loop. New case large_5000: 5000 nodes, with a left path of 13 nodes and a right path of 12 nodes, both ending below the old limit. Its expected output was cross-checked with an independent Python version of the search. Up to 2047 nodes the output still matches the 2019 build (3000 random inputs); above that the 2019 build is undefined, so a Python reference was used instead (400 random trees of 2048 to 30000 nodes). Made by AI agent: Claude Sonnet 5.5 (claude-sonnet-5-5).
The task asks for one path in the left subtree and one in the right
subtree. The root is in neither, so a path needs at least one node below
it. The 2019 program checked the root's own sum too, and treated the root
as part of the left subtree. A target equal to the root's value printed
the root alone as the left path, even when no real left path existed.
The search now starts at the root's two children, one for each side, and
the root only adds its value to the sum.
Output for the case root_alone (`10 5 7`, target 10):
before: Path Found: 10 after: No Path Found
No Path Found No Path Found
Its .out file is updated. New case root_and_longer_path (`10 5 -10 -5 1
10 1`, target 10): the 2019 program printed the root alone for the left
path, now it prints `Path Found: 10 5 -5`.
On 3000 random inputs, the output is the same as the 2019 build except
where the old left line was the root alone. A Python reference (also
checked on trees of 2048 to 30000 nodes) agrees with the new program on
every input.
Made by AI agent: Claude Sonnet 5.5 (claude-sonnet-5-5).
The 2019 program trusted the file. A missing file, a word like "abc" or an empty line gave silent wrong output, and a value that did not fit in an int was cut or misread. Now the input must follow these rules. Line 1 has one or more whole numbers in the int range, separated by spaces or tabs. Line 2 has exactly one whole number in the int range. Only whitespace may follow. A missing final newline and Windows line endings are fine. A broken rule prints `error: line N: <what is wrong>` on stderr and exits with status 1 (the offending word is quoted, long ones are cut). The errors are: empty input, no values on line 1, a word that is not a whole number, a number out of int range, a missing target, a second number on line 2 and extra data after line 2. A missing file prints `error: cannot open FILE`. The program checks the whole input before it prints anything. A wrong number of arguments prints a usage line and exits with status 2, instead of crashing with no argument. Sums are now long long. A path of big values could pass the int range on the way down, which is undefined behaviour and could even give a false match. The case int_overflow_no_match (2147483647 twice, target -2) printed a path before this change and prints "No Path Found" now. New cases: values near the int limits, the format variations that must still work (CRLF, no final newline, tabs and spaces, blank lines at the end) and one case for each error above. For the valid cases the 2019 build gives the same output. On 3000 random inputs the new program still matches the 2019 build (apart from the root-alone fix). Made by AI agent: Claude Sonnet 5.5 (claude-sonnet-5-5).
The online playground can only pass the input as standard input, and piping a file in is handy anyway. With no argument the program now reads stdin; with one argument it reads that file, as before. More than one argument is still a usage error (exit status 2); the usage line now shows the optional file: "usage: b_tree [INPUT_FILE]". The same code and the same checks run for both sources, so the error messages are identical. In the tests, the runner now also feeds every valid case on stdin with no arguments and expects the same output. A new case, stdin_value_text, checks an error message on the stdin path. Made by AI agent: Claude Sonnet 5.5 (claude-sonnet-5-5).
The old README showed the task with three images from the assignment handout and described the old build and the pow/log2 trick. Delete img/ and draw the same examples as text: the node numbering 1 to 7 and both figure examples with their output. The new README follows the sibling repos: a note that this is 2019 coursework restored in 2026 (original code at tag original-2019), a "Run it online" link, the task and the input rules, how to build, run and test, how the program works, the changes made in 2026 and the license (MIT, the LICENSE file is unchanged). The online link opens b_tree.cpp in Compiler Explorer with examples/figure-2.txt as stdin. It holds a snapshot of the code, so it must be regenerated if b_tree.cpp changes. Made by AI agent: Claude Sonnet 5.5 (claude-sonnet-5-5).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Restores this 2019 ITU data structures coursework. The original code is kept at tag
original-2019.What changes
examples/figure-1.txtandexamples/figure-2.txt.make testwith g++ and clang++.Treeclass that frees its nodes; the left/right side is tracked during the search instead of thepow/log2formula; builds with-Werror.error: line N: ...(exit status 1). Path sums uselong long, so large values no longer produce false paths.Checks
make testpasses at every commit (55 checks on the last one, including stdin re-runs).-Wpedantic) with no warnings; it prints the handout's expected paths.🤖 Generated with Claude Code — changes by AI agent Claude Sonnet 5.5, reviewed by Claude Opus 5.5.