Objectives
- Know how to create and use a library of integer trees
- Know how to implement some basic tree algorithms
Context and Preparation
You will find several files to help you get started with the lab:
-
https://www.dequidt.me/uploads/se/trees.candhttps://www.dequidt.me/uploads/se/trees.hcontaining basic tree processing functions -
https://www.dequidt.me/uploads/se/main.ca file that demonstrates the use of these functions -
https://www.dequidt.me/uploads/se/tree2pdf.ca second file that converts a tree toPDF -
https://www.dequidt.me/uploads/se/samples/a directory containing data files:balanced.dotbalanced.pdfbalanced.txtdegenerated.dotdegenerated.pdfdegenerated.txtempty.dotempty.pdfempty.txtleaf.dotleaf.pdfleaf.txtunspecified.dotunspecified.pdfunspecified.txt
Lab Questions
Difficulty: Rx-
Write the
cons_tree(...)function that constructs a BST (done in lecture). -
Write the
mk_empty_tree(...)function -
Write the
is_empty(...)function -
Write the
is_leaf(...)function — these are all auxiliary functions. (Done in lecture) -
Write the
add(...)function that inserts an element into a BST (binary search tree, therefore ordered). (Done in lecture) -
Write the
print_tree(...)function that prints a BST using recursive traversal, in ascending order (inorder). -
Verify that the previous traversal function produces the same sequence of values for the three files
unspecified.txt,balanced.txt, anddegenerated.txt, even though they are different trees (see the diagrams in thesamples/directory). -
Write a
load_tree(...)function that builds an integer tree from a file. -
Write a
free_tree(...)function that frees the memory used by a binary tree. -
Write a
print_rec_edges(...)function that prints the pairs(parent =L=> child)for left children, and(parent =R=> child)for right children, of a given tree. Do not print empty subtrees. Apart from the =L/R=> arrow, the output matches the trees in the.dotfiles provided in thesamples/directory. -
The
.pdffiles were generated from the.dotfiles (provided insamples/). The.dotformat is a simple textual representation format for trees [^1]. View the format of these files in your preferred text editor or using thelesscommand. From a.dotfile, thedotcommand generates a viewable version (.pdf,.png, …) as follows (inpdffor example):
dot -Tpdf samples/balanced.dot -o samples/balanced.pdf
The goal is then to generate such files for trees built from data files (such
as those provided in samples/*.txt).
Preparation: A more complete
tree2pdf.cfile is provided which: Generates, by calling thegenerate_dotfunction, the.dotfiles corresponding to the data files whose names were passed as parameters (e.g., fromsamples/balanced.txtit generatessamples/balanced.dot). Converts these.dotfiles todotcommand.
-
Implement the
recursive_dotfunction that handles the general case. -
Then test with the command:
./tree2pdf samples/*.txt. You should recover the.dotand.pdffiles.