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.