80 likes | 193 Views
SELECTION TREES. Introduction. Suppose we have k ordered sequences, called runs , that are to be merged into a single ordered sequence.
E N D
Introduction • Suppose we have k ordered sequences, called runs, that are to be merged into a single ordered sequence. • Each run consists of some records and is in non-decreasing order of a designated field called key. Let n be the number of records in all k runs together. • The merging task can be accomplished by repeatedly outputting the record with the smaller key. The smallest has to be found from k possibilities, and it could be the leading record in any of the k runs.
Cont… • The most direct way to merge k runs is to make k-1 comparisons to determine the next record to output. • For k >2, we can achieve a reduction in the number of comparisons needed to find the next smallest element by using the selection tree data structure. • Ther are two kind of selection trees:
Winner tree • Winner tree is a complete binary tree in which each node represents the smaller of its two children. Thus the root node represents the smallest node in the tree.
cont… • The construction of the winner tree can be compared to the playing of a tournament in which the winner is the record with the smaller key. Then the each non-leaf node in the tree represents the winner of the tournament and the root node represents the overall winner.
Cont… • Each leaf node represents the first record in the corresponding run. Since the records being merged are generally large, each node will contain only a pointer to the record it represents.
Analysis of merging runs using winner trees • The number of levels in the tree is [log2(k+1)]. So the time to restructure the tree is O(log2k). • The tree has to be restructured each time a record is merged in to the output file. Hence the time required to merge all the n records is O(n log2k). • The time required to set up the selection tree the first time is o(k). Thus total time needed to merge the k runs is O(n log2k)
Loser tree • A selection tree in which each non-leaf node retains a pointer to the loser is called a loser tree.