Use the red/black tree to find the minimum n numbers, suitable for big data processing and the number of red/Black

Source: Internet
Author: User

Use the red/black tree to find the minimum n numbers, suitable for big data processing and the number of red/Black

Use STL container multiset

Code:

# Include <iostream> # include <vector> # include <set> using namespace std; typedef multiset <int, greater <int> intSet; typedef multiset <int, greater <int >>:: iterator intIter; void findMaxNum (const vector <int> & data, int k) {intSet Max; Max. clear (); if (k <1 | data. size () <k) return; vector <int>: const_iterator iter = data. begin (); for (; iter! = Data. end (); ++ iter) {if (Max. size () <k) {Max. insert (* iter);} else {intIter least = Max. begin (); if (* iter) <* (Max. begin () {// only begin () can be used to delete Max. erase (least); Max. insert (* iter) ;}}for (intIter it = Max. begin (); it! = Max. end (); it ++) {cout <* it <"" ;}} int main () {vector <int> data; for (int I = 0; I <1000; I ++) {data. push_back (I);} findMaxNum (data, 10); return 0 ;}

Running result:



Minimum and maximum number of nodes in the Red Tree

I have just written the application of the red/black tree. But I did not think about the number of red nodes.
It seems that there may be at least one zero red node,
The number of up to half nodes is 5000/2
For the number of red nodes in n nodes, the minimum value is 0, and the maximum value is "n Division 2"
 
Find the minimum N number in the array

# Include <iostream>
Using namespace std;
Void PX (int count, int data [], int n)
{
Int I, j, t, now (0 );
T = data [n-1];
For (I = 1; I <= (n-count); I ++)
{
T = data [n-I];
Now = n-1;
For (j = n-i-1; j> = 0; j --)
{
If (t <data [j]) {t = data [j]; now = j ;}
}
Cout <"tt" <t <now <"";
For (j = now; j <n-I; j ++)
Data [j] = data [j + 1];
Data [n-I] = t;
For (int x = 0; x <5; x ++)
Cout <data [x] <"";
Cout <endl;
}
}

Void main ()
{
Int a [] = {9, 8, 3, 7, 5 };
PX (3, a, 5 );
For (int I = 0; I <5; I ++)
Cout <a [I] <"";
}

Place (n-count) large numbers to the back, and fill in the front positions in order.

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.