Algorithms and Data Structures
Algorithms and Data Structures
Algorithms and Data Structures
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
N.Wirth. <strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong>. Oberon version 198<br />
occupies k words <strong>and</strong> that each pointer occupies one word of storage. What is the gain in storage when<br />
using a binary tree versus an n-ary tree?<br />
4.15. Assume that a tree is built upon the following definition of a recursive data structure (see Exercise<br />
4.1). Formulate a procedure to find an element with a given key x <strong>and</strong> to perform an operation P on this<br />
element.<br />
RECTYPE Tree = RECORD x: INTEGER;<br />
left, right: Tree<br />
END<br />
4.16. In a certain file system a directory of all files is organized as an ordered binary tree. Each node<br />
denotes a file <strong>and</strong> specifies the file name <strong>and</strong>, among other things the date of its last access, encoded as an<br />
integer. Write a program that traverses the tree <strong>and</strong> deletes all files whose last access was before a certain<br />
date.<br />
4.17. In a tree structure the frequency of access of each element is measured empirically by attributing to<br />
each node an access count. At certain intervals of time, the tree organization is updated by traversing the<br />
tree <strong>and</strong> generating a new tree by using the program of straight search <strong>and</strong> insertion, <strong>and</strong> inserting the keys<br />
in the order of decreasing frequency count. Write a program that performs this reorganization. Is the<br />
average path length of this tree equal to, worse, or much worse than that of an optimal tree?<br />
4.18. The method of analyzing the tree insertion algorithm described in Sect. 4.5 can also be used to<br />
compute the expected numbers C n of comparisons <strong>and</strong> M n of moves (exchanges) which are performed by<br />
Quicksort, sorting n elements of an array, assuming that all n! permutations of the n keys 1, 2, ... n are<br />
equally likely. Find the analogy <strong>and</strong> determine C n <strong>and</strong> M n .<br />
4.19. Draw the balanced tree with 12 nodes which has the maximum height of all 12-node balanced trees.<br />
In which sequence do the nodes have to be inserted so that the AVL-insertion procedure generates this<br />
tree?<br />
4.20. Find a sequence of n insertion keys so that the procedure for insertion in an AVL-tree performs<br />
each of the four rebalancing acts (LL, LR, RR, RL) at least once. What is the minimal length n for such a<br />
sequence?<br />
4.21. Find a balanced tree with keys 1 ... n <strong>and</strong> a permutation of these keys so that, when applied to the<br />
deletion procedure for AVL-trees, it performs each of the four rebalancing routines at least once. What is<br />
the sequence with minimal length n?<br />
4.22. What is the average path length of the Fibonacci-tree T n ?<br />
4.23. Write a program that generates a nearly optimal tree according to the algorithm based on the<br />
selection of a centroid as root.<br />
4.24. Assume that the keys 1, 2, 3, ... are inserted into an empty B-tree of order 2. Which keys cause<br />
page splits to occur? Which keys cause the height of the tree to increase? If the keys are deleted in the<br />
same order, which keys cause pages to be merged (<strong>and</strong> disposed) <strong>and</strong> which keys cause the height to<br />
decrease? Answer the question for (a) a deletion scheme using balancing, <strong>and</strong> (b) a scheme whithout<br />
balancing (upon underflow, a single item only is fetched from a neighboring page).<br />
4.25. Write a program for the search, insertion, <strong>and</strong> deletion of keys in a binary B-tree. Use the node<br />
type <strong>and</strong> the insertion scheme shown above for the binary B-tree.<br />
4.26. Find a sequence of insertion keys which, starting from the empty symmetric binary B-tree, causes