Summary
This program uses the treesort algorithm of Section 11.2 except that it sorts a list rather than an array.
The program first reads in a sequence of positive integers terminated by the value zero (or a negative value) and builds it into a list which is printed in a tabular format. This unsorted list is then converted into a binary tree which is printed in the usual representation. The binary tree is then converted back into an ordered list and finally the sorted values are printed. The program repeats until it encounters an empty sequence (just a zero or negative number).
The two library packages Lists and Trees contain the key data structures Cell and Node and associated operations. Although we have not yet discussed packages, private types and compilation units in depth this is much better than putting everything inside the main subprogram which is the style which would have to be adopted using just the algorithmic facilities described in Part 2. The types List and Tree are private types. Their full type reveals that they are access types referring to Cell and Node. Thus the inner structure of the types Cell and Node are not used outside their defining packages. The small package Page declares a constant defining the page width and a subtype defining a line of text.
There are then a number of library subprograms for performing the main activities, namely Read_List, Print_List, Print_Tree, Convert_List_To_Tree and Convert_Tree_To_List. Finally, the main subprogram calls these within an outer loop.
- Type
- Chapter
- Information
- Programming in Ada 2012 , pp. 223 - 226Publisher: Cambridge University PressPrint publication year: 2014