Multi-channel merge program using the loser tree

Source: Internet
Author: User

 

1. This program currently only takes into account the full binary tree leaf node.

 

2. A MAX_BIG is added at the end of each ordered sequence to indicate the final end. This is done to simplify the program design. Otherwise, when the last element of a sequence is merged into the target sequence, I don't know what to add to the loser tree.

 

0 1 2 3 4 5 6 7 8 999999999

1 2 3 4 5 6 7 8 9 999999999

4 5 6 7 8 9 10 11 12 999999999

9 10 11 12 13 14 15 16 17 999999999

0 1 1 2 2 3 4 4 4 5 5 6 6 6 7 7 8 8 8 9 9 10 10 11 12 12 13 14 15 16 17

 

 

 

$ $

 

# Include <stdio. h>

# Include <stdlib. h>

 

Typedef struct wrap_data

{

Int offset;

Int path;

Int * data;

} Wrap_data;

 

 

Int choosevec (int path)

{

If (path <= 4)

{

Return 4;

}

Else if (path <= 8)

{

Return 8;

}

Else if (path <= 16)

{

Return 16;

}

Else

{

Return 32;

}

}

 

Wrap_data ** vec;

Int vecsize;

 

Wrap_data * up (int num)

{

Int I, j, k;

Wrap_data * first, * second;

I = num;

Second = vec [I];

While (I)

{

J = I/2;

First = vec [j];

 

If (! First)

{

Vec [j] = second;

If (! J)

{

Return second;

}

Else

{

Return NULL;

}

}

If (first-> path = second-> path)

{

I = j;

}

Else if (* (second-> data + second-> offset)> * (first-> data + first-> offset ))

{

Vec [j] = second;

Second = first;

I = j;

 

}

Else

{

I = j;

}

}

Return second;

}

 

Int main ()

{

# Define PATH 4

# Define LENGTH 10

# Define MAX_BIG 999999999

 

Wrap_data * result;

Int I = 0, j = 0, k = 0;

Wrap_data a [PATH] = {0 };

 

Vecsize = 2 * choosevec (PATH );

Vec = (wrap_data **) calloc (vecsize, sizeof (wrap_data *));

 

For (I = 0; I <PATH; I ++)

{

A [I]. data = (int *) calloc (LENGTH, sizeof (int ));

A [I]. offset = 0;

A [I]. path = I;

For (j = 0; j <LENGTH; j ++)

{

 

* (A [I]. data + j) = (I * I + j) % 40;

 

}

* (A [I]. data + LENGTH-1) = MAX_BIG;

}

For (I = 0; I <PATH; I ++)

{

For (j = 0; j <LENGTH; j ++)

{

Printf ("% d", * (a [I]. data + j ));

}

Printf ("\ n ");

}

K = vecsize/2;

For (I = 0; I <PATH; I ++)

{

Vec [k + I] = & a [I];

}

For (I = 0; I <PATH; I ++)

{

Result = up (I + k );

If (! Result)

{

}

Else

{

Break;

}

}

While (result)

{

If (MAX_BIG = * (result-> data + result-> offset ))

{

Break;

}

Printf ("% d", * (result-> data + result-> offset ));

Result-> offset ++;

Result = up (result-> path + k );

}

Printf ("\ n ");

 

}

 

 

From chenbingchenbing's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.