Computer programming - Lab 13

For problems 1 to 4, define a structure type which holds a pair of two lists.

1. Implement a function that takes a list (e.g. of integers) and an unsigned length len, and returns a pair of two lists: the list of the first len elements, and the list of any remaining ones. Return the pair of empty lists if len exceeds the length of the list.

2. Implement a function that takes a list and an element (e.g. integer), and returns a pair of two lists: the prefix before the first occurrence of the element, and the tail, after that occurrence. Return the pair of empty lists if the element is not found.

3. A binary tree with integer nodes is either the empty tree, or a node (root) with a left and a right subtree.
A binary tree with different values in each node may be uniquely reconstructed if the lists of its nodes in both preorder and inorder traversals are given.
Preorder traversal means visiting the root followed by the left and right subtree. Inorder traversal means visiting the left subtree, the root and then the right subtree. In both cases, subtrees are traversed in the same order as specified.
For example, given the lists:

preorder: [2; 7; 1; 6; 5; 11; 8; 9; 4]
inorder: [1; 7; 5; 6; 11; 2; 8; 4; 9]
the first element in the preorder list is the root. Searching for it in the inorder list splits the list into [1; 7; 5; 6; 11] for the left subtree and [8; 4; 9] for the right subtree (both inorder). Splitting the tail of the preorder list in pieces of the corresponding lengths produces the (preorder) lists [7; 1; 6; 5; 11] and [8; 9; 4]. Thus the problem is reduced to solving it recursively for the two subtrees.
Implement a function that takes two lists and returns the tree reconstructed from these lists. Do not destroy the original lists, but free any intermediate memory allocated. Report inconsistent input.
Simpler version: Solve the problem for arrays instead of lists. Avoid any copying in this case.

4. Implement a function that takes a list of integers and a function f from int to int and returns a pair of two lists: the list of those elements for which f is true (nonzero), and the list of the elements for which f is false (zero).

5. Implement a datastructure for lists of integer lists. Use it to store integers read until end of input (separated by whitespace), each input line resulting in a list. Use the address of the (link) pointer to append each new integer to the end of the list for a line.

6. Implement and test a library for sets of bounded unsigned integers, using one bit per element. The maximum bound for a set element is given at set creation time.

7. Read a text file and print its lines sorted by increasing length, with lines of equal length ordered alphabetically. Avoid recomputing lengths. Assume lines have at most 255 characters and split longer lines.


Marius Minea
Last modified: Thu Dec 18 12:55:00 EET 2014