void CContactsRamTest::RamTest2L() { test.Printf(_L("RemoteView is populated\n")); PrintHeap(); CloseView(); DELETE_SET_NULL( iContactsDatabase ); test.Printf(_L("Full Database is re-opened\n")); iContactsDatabase = CContactDatabase::OpenL(); PrintHeap(); DELETE_SET_NULL( iContactsDatabase ); }
void TestHeapOperations (void) { static const int c_Values [31] = { // 31 values make a full 4-layer tree 93, 92, 90, 86, 83, 86, 77, 40, 72, 36, 68, 82, 62, 67, 63, 15, 26, 26, 49, 21, 11, 62, 67, 27, 29, 30, 35, 23, 59, 35, 29 }; vector<int> v; v.reserve (VectorSize(c_Values)); for (uoff_t i = 0; i < VectorSize(c_Values); ++ i) { v.push_back (c_Values[i]); push_heap (v.begin(), v.end()); cout << "------------------------------------------------\n"; if (!is_heap (v.begin(), v.end())) cout << "Is NOT a heap\n"; PrintHeap (v); } cout << "------------------------------------------------\n"; cout << "make_heap on the full range:\n"; v.resize (VectorSize (c_Values)); copy (VectorRange(c_Values), v.begin()); make_heap (v.begin(), v.end()); PrintHeap (v); if (!is_heap (v.begin(), v.end())) cout << "Is NOT a heap\n"; cout << "------------------------------------------------\n"; cout << "pop_heap:\n"; pop_heap (v.begin(), v.end()); v.pop_back(); PrintHeap (v); if (!is_heap (v.begin(), v.end())) cout << "Is NOT a heap\n"; cout << "------------------------------------------------\n"; cout << "sort_heap:\n"; v.resize (VectorSize (c_Values)); copy (VectorRange(c_Values), v.begin()); make_heap (v.begin(), v.end()); sort_heap (v.begin(), v.end()); foreach (vector<int>::const_iterator, i, v) cout << *i; cout << endl; cout << "------------------------------------------------\n"; cout << "priority_queue push and pop:\n"; priority_queue<int> q; for (uoff_t i = 0; i < VectorSize(c_Values); ++ i) q.push (c_Values[i]); while (!q.empty()) { cout << q.top(); q.pop(); } cout << endl; }
void HeapSort(int num[] ,int size) { int i; int iLength=size; PrintHeap("Befor Sort:",num,iLength); BuildHeap(num,size);// 建立小顶堆 for (i = iLength - 1; i >= 1; i--) { Swap(num, 0, i);// 交换 size--;// 每交换一次让规模减少一次 PercolateDown(num, 0,size);// 将新的首元素下滤操作 PrintHeap("Sort Heap:",num,iLength); } }
/* 建堆方法,只需线性时间建好; 建堆的结果:数组的第一个元素(即树根)是所有元素中的最小值,索引小于等于size/2-1的其它元素(即其它非叶子节点)的值都是其所在子树的最小值 */ void BuildHeap(int num[] ,int size) { int i; //从最后一个非叶子节点开始,对每个非叶子节点进型最小根调整,保证每个根节点都是其子树中的最小值 for (i = size / 2 - 1; i >= 0; i--) { PercolateDown(num, i,size);// 进行下滤操作 PrintHeap("Build heap:",num,size); } }
void CContactsRamTest::RamTestL() { test.Next(_L("Initial ram test")); test.Printf( _L("Ram test for %d contacts\n"), manycontacts); DELETE_SET_NULL( iContactsDatabase ); test.Printf(_L("Empty Database is opened\n")); iContactsDatabase = CContactDatabase::ReplaceL(); PrintHeap(); AddContactsL( manycontacts ); test.Printf(_L("Database is opened and contacts added\n")); PrintHeap(); test.Printf(_L("RemoteView with first/surname order is created\n")); iRemoteView = CContactRemoteView::NewL( *this, *iContactsDatabase, iViewSortOrder, EContactsOnly); PrintHeap(); }
int main() { /* Question 1. */ puts(KBLU "\n1. Read in numbers in data 100 one by one and insert them into an initially empty heap. Print out the elements in the array (you should check the output to see if the heap order is satisfied)." RESET); // open file FILE * fp = fopen(DATA100, "rb+"); if (!fp) { puts(KRED "ERROR: Failed to open file." RESET); exit(-1); } // create binary heap PriorityQueue H = Initialize( 100 ); // insert data int i, cur; for ( i = 0; i < 100; ++i ) { fscanf(fp, "%d", &cur); Insert(cur, H); } // print data in order PrintHeap( H ); // empty the heap MakeEmpty( H ); // destroy the heap Destroy( H ); /* Question 2. */ puts(KBLU "\n2. Write a function to print out elements that are smaller than 15000 (not necessary in sorted order). The function should run in O(K), where K is the number of the elements you print out. " RESET); // open file fp = fopen(DATA100, "rb+"); if (!fp) { puts("ERROR: Failed to open file."); exit(-1); } // create binary heap H = Initialize( 100 ); // insert data for ( i = 0; i < 100; ++i ) { fscanf(fp, "%d", &cur); Insert(cur, H); } // print data in order PrintSmaller( 15000, H ); // empty the heap MakeEmpty( H ); // destroy the heap Destroy( H ); /* Question 3. */ puts(KBLU "\n3. Read in numbers in data 100 into an array. Implement BuildHeap and use it to convert the array into a heap.\n" RESET); // open file fp = fopen(DATA100, "rb+"); if (!fp) { puts("ERROR: Failed to open file."); exit(-1); } // create binary heap H = Initialize( 100 ); // insert data for ( i = 0; i < 100; ++i ) { fscanf(fp, "%d", &cur); Append(cur, H); } // random array puts("before:"); PrintHeap( H ); // build heap BuildHeap( H ); // print the heap puts("\nafter:"); PrintHeap( H ); // empty the heap MakeEmpty( H ); // destroy the heap Destroy( H ); /* Question 4. */ puts(KBLU "\n4. Find the 30th smallest element by repeatly using DeleteMin. \n" RESET); // open file fp = fopen(DATA100, "rb+"); if (!fp) { puts("ERROR: Failed to open file."); exit(-1); } // create binary heap H = Initialize( 100 ); // build heap for ( i = 0; i < 100; ++i ) { fscanf(fp, "%d", &cur); Append(cur, H); } BuildHeap( H ); // first 30 smallest number int tmp; for ( i = 0; i < 30; ++i ) { tmp = DeleteMin( H ); printf("%d ", tmp); } printf("\n\n\t30th smallest number is" KYEL " %d\n" RESET, tmp); // empty the heap MakeEmpty( H ); // destroy the heap Destroy( H ); /* Question 5. */ puts(KBLU "\n5. Implement Algorithm 6B and use it to find the 71st largest element in file data 100. \n" RESET); // open file fp = fopen(DATA100, "rb+"); if (!fp) { puts("ERROR: Failed to open file."); exit(-1); } // create binary heap H = Initialize( 100 ); // build heap for ( i = 0; i < 71; ++i ) { fscanf(fp, "%d", &cur); Append(cur, H); } BuildHeap( H ); // replace the smallest element for ( i = 71; i < 100; ++i ) { fscanf(fp, "%d", &cur); if ( cur > FindMin( H ) ) { DeleteMin( H ); Insert( cur, H ); } } printf("\t71st largest number is" KYEL " %d\n" RESET, FindMin( H )); // empty the heap MakeEmpty( H ); // destroy the heap Destroy( H ); /* EXPERIMENT */ puts(KBLU "\n6. Read in numbers in data 100 into an array. Implement BuildHeap and use it to convert the array into a 4-heap.\n" RESET); // open file fp = fopen(DATA100, "rb+"); if (!fp) { puts("ERROR: Failed to open file."); exit(-1); } // create binary heap H = Initialize4Heap( 100 ); // build heap for ( i = 0; i < 100; ++i ) { fscanf(fp, "%d", &cur); Append(cur, H); } // random array puts("before:"); PrintHeap( H ); // build heap BuildHeap( H ); // print the heap puts("\nafter:"); PrintHeap( H ); /* Question 7. */ puts(KBLU "\n7. Find the 30th smallest element by repeatly using DeleteMin. (4-heap)\n" RESET); // first 30 smallest number for ( i = 0; i < 30; ++i ) { tmp = DeleteMin( H ); printf("%d ", tmp); } printf("\n\n\t30th smallest number is" KYEL " %d\n" RESET, tmp); // empty the heap MakeEmpty( H ); // destroy the heap Destroy( H ); /* Question 8. */ puts(KBLU "\n8. Implement Algorithm 6B and use it to find the 71st largest element in file data 100. (4-heap)\n" RESET); // open file fp = fopen(DATA100, "rb+"); if (!fp) { puts(KRED "ERROR: Failed to open file." RESET); exit(-1); } // create binary heap H = Initialize4Heap( 100 ); // build heap for ( i = 0; i < 71; ++i ) { fscanf(fp, "%d", &cur); Append(cur, H); } BuildHeap( H ); // replace the smallest element for ( i = 71; i < 100; ++i ) { fscanf(fp, "%d", &cur); if ( cur > FindMin( H ) ) { DeleteMin( H ); Append( cur, H ); BuildHeap( H ); } } printf("\t71st largest number is" KYEL " %d\n" RESET, FindMin( H )); puts(""); // empty the heap MakeEmpty( H ); // destroy the heap Destroy( H ); return 0; }