Abdiaziz.Haji-avatar

Design an O(n) algorithm whose input is a tree of n vertices. The algorithm should copy all these elements to an array A so that the resulting array will be sorted in the increasing order

Hint: Explore the tree using the preorder algorithm. The non-trivial aspect of this exercise is that if elements are copied directly to an array, the array index must be incremented every time a new element is copied. This problem can be addressed in at least two ways: 1. • Modify the recursive pre-order procedure so that it returns the index of the last element copied to the array.
2. • Use an intermediate data structure (which?) where elements can be added without care of the indices and then copy elements from that structure to the array.
0%

4 replies

about 6 years ago
ellen.robinson-avatar
Bucket Sort Algorithm works perfectly in this paradigm. All the values from tree nodes will be placed in increasing order.
about 6 years ago
Jepson_90-avatar
You have described everything needed to create algorithm. Modify the recursive pre order approach is more practical than IDS.