本文整理汇总了C++中MaxHeap::insert方法的典型用法代码示例。如果您正苦于以下问题:C++ MaxHeap::insert方法的具体用法?C++ MaxHeap::insert怎么用?C++ MaxHeap::insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MaxHeap
的用法示例。
在下文中一共展示了MaxHeap::insert方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: handleQuery
float handleQuery(float prev,int next,MaxHeap& maxheap,MinHeap& minheap){
if(next>prev){
if(minheap.n>maxheap.n){
maxheap.insert(minheap.extractMin());
}
minheap.insert(next);
}else{
if(maxheap.n>minheap.n){
minheap.insert(maxheap.extractMax());
}
maxheap.insert(next);
}
if((maxheap.n+minheap.n)%2==0){
float l = maxheap.a[0];
float r = minheap.a[0];
return (l+r)/2;
}else{
if(maxheap.n>minheap.n)
return maxheap.a[0];
return minheap.a[0];
}
}
示例2: main
int main()
{
int n;
cin >> n;
MaxHeap mHeap;
uint tmp;
for (int i = 0; i < n; i++){
cin >> tmp;
if (i <= n / 2){
mHeap.insert(tmp);
}
else{
mHeap.insert(tmp);
mHeap.extractMax();
}
}
uint num1, num2;
if (n % 2 != 0){
cout << mHeap.extractMax() << ".0" << endl;
}
else{
num1 = mHeap.extractMax();
num2 = mHeap.extractMax();
if ((num1 + num2) % 2 == 0){
cout << (num1 + num2) / 2 << ".0" << endl;
}
else{
cout << (num1 + num2) / 2 << ".5" << endl;
}
}
return 0;
}
示例3: MedianMaintenance
int MedianMaintenance(MaxHeap<int>& heapLow, MinHeap<int>& heapHigh, int elem)
{
if(heapLow.size() == heapHigh.size())
{
if(heapLow.size())
{
if(elem > heapHigh.get_min())
{
heapHigh.insert(elem);
heapLow.insert(heapHigh.extract_min());
}
else
heapLow.insert(elem);
}
else
heapLow.insert(elem);
}
else
{
if(elem < heapLow.get_max())
{
heapLow.insert(elem);
heapHigh.insert(heapLow.extract_max());
}
else
heapHigh.insert(elem);
}
return heapLow.get_max();
}
开发者ID:happyWinner,项目名称:Design_and_Analysis_of_Algorithms_Part_1_Stanford,代码行数:30,代码来源:MedianMaintenance.cpp
示例4: test_maxheap
bool test_maxheap()
{
MaxHeap<int> maxheap;
int lim = 1000;
for(int i = 0;i < lim; i++)
{
maxheap.insert(rand() % lim + (rand() < (RAND_MAX/8)?-lim/10:lim));
}
int *max_sort = new int[maxheap.size()];
int i = 0;
while(maxheap.size())
{
max_sort[i++] = maxheap.remove_max();
}
i = 1;
for(int i = 1; i < lim; i++)
{
if(max_sort[i] > max_sort[i-1])return false;
}
return true;
}
示例5: main
int main(int argc, const char * argv[])
{
PrefixTree tree;
ScrabbleLookup lookup;
string dictionaryFile;
// Parse the command line argument by looping through all
// of the command line arguments
for (int i = 0; i < argc; i++)
{
if (strcmp(argv[i],"-d") == 0) dictionaryFile = argv[i+1];
}
// Open all dictionary file, and import all the words into the
// trie.
ifstream myfile;
myfile.open(dictionaryFile.c_str());
while (!myfile.eof())
{
string word;
myfile >> word;
tree.insert(word);
}
// Print tree information.
cout << "Number of word in tree: " << tree.getNumWord() << endl;
cout << "Number of nodes in tree: " << tree.getNumNodes() << endl;
cout << "Maximum depth of the tree: " << tree.getMaxDepth() << endl;
cout << "Number of bytes in tree: " << tree.getNumBytes() << endl << endl;
// Continue looping the program until all of the words have been entered.
string word = "";
while (word != "!")
{
MaxHeap heap;
// Prompt the user for the word to solve.
cout << "Please enter a collection of letters (all lowercase) or questions marks to solve, or ! if you want to quit: ";
cin >> word;
if (word == "!") break;
cout << endl << "Any restrictions? Please enter the position number of the letter first, and then the character. An example is 2a for forcing the second letter to be a. Keep entering restrictions, and then enter ! when finished. ";
string restric = "";
while (restric != "!")
{
cin >> restric;
if (restric == "!") break;
RestrictionNode restrictNode;
restrictNode.letter = restric[1];
restrictNode.position = atoi(restric.substr(0,1).c_str());
restrictions.push_back(restrictNode);
}
// Generate all of the combinations of the collection of letters, and store in a vector.
for (int x = 0; x <= word.length(); x++)
generate_combinations(word, x);
// Change all of the question marks in the set to 26 different words per question mark
convertAllQuestionMarks();
// From the combinations, make permutations of all of the words
for (int x = 0; x < combinations.size(); x++)
generate_permutations(combinations[x]);
// Iterate through all of the permutations, and include known words into the heap.
for (set<string>::iterator it = permutation_set.begin();
it != permutation_set.end(); it++) {
if (tree.isStored(*it))
{
heap.insert(*it, lookup.pointValue(*it));
}
}
cout << endl << "The following are the top Scrabble words: " << endl;
// Output the words in order of the word score.
for (int i = 0; i < WORDS_TO_OUTPUT; i++)
{
string word = heap.dequeueMax();
int score = lookup.pointValue(word);
if (word != "")
cout << word << " " << score << endl;
}
cout << endl;
combinations.clear();
restrictions.clear();
combinations_pre.clear();
permutation_set.clear();
}
}