Ejemplo n.º 1
0
int main() {
	
	// Create LeftistHeap
	/*
	LeftistHeap LHeap;
	
	// Build LHeap using insert
	int v;
	ifstream dataFile ("data.txt");
	if(dataFile.is_open())
		while(dataFile >> v)
		{
			LHeap.insert(v);			
			cout << "At insert: " << v << endl;
			LHeap.print();
			//LHeap.print();
		}
	dataFile.close();
	
	// Test result of inserts
	LHeap.print();
	cout << endl;
	
	// Test findMin
	//cout << LHeap.findMin() << endl;
	// Test result of deleteMins
	cout << "DeleteMin: " << endl;	
	cout << LHeap.deleteMin() << endl;
	cout << LHeap.deleteMin() << endl;
	cout << LHeap.deleteMin() << endl;
	cout << endl;
	
	// Check final heap
	LHeap.print();*/
	
	cout << endl << "---" << endl << endl;
	
	// Create SkewHeap
	SkewHeap SHeap;
	int v;
	// Build SHeap using insert
	ifstream dataFile2 ("data.txt");
	if(dataFile2.is_open())
		while(dataFile2 >> v)
		{
			SHeap.insert(v);			
			cout << "At insert: " << v << endl;
			SHeap.print();
		}
	dataFile2.close();

	// Test result of inserts
	SHeap.findMin();
	SHeap.print();
	cout << endl;

	// Test findMin
	//cout << SHeap.findMin() << endl;
	// Test result of deleteMins
	//
	cout << "Skew deleteMin: " << endl;
	cout << SHeap.deleteMin() << endl;
	/*cout << SHeap.deleteMin() << endl;
	cout << SHeap.deleteMin() << endl;
	cout << endl;*/

	// Check final heap
	SHeap.print();
	
	return 0;
}
Ejemplo n.º 2
0
void heapTests(int seed, int s) {
    srand(seed);  // seed the random number generator
    int array[s]; // create the array to hold random numbers
    int deletes = 0, inserts = 0;
    for (int i = 0; i < s; i++)
        array[i] = rand() % (4 * s) + 1; // number between 1 to 4n + 1

    Timer t;
    
    // binary heap building
    cout << "Binary Heap\n";
    t.start();
    BinaryHeap bHeap(array, s);
    cout << "Build time: ";
    t.printTime(t.stop());
    
    // binary heap insert/deleteMin
    srand(seed);
    t.start();
    for (int j = 0; j < s * 0.1; j++) {
        float r = (float)rand() / (float)RAND_MAX;
        if (r < 0.5) {
            bHeap.deleteMin();
            deletes++;
        } else {
            bHeap.insert(rand() % (4 * s) + 1);
            inserts++;
        }
    }
    cout << "Insert/Delete Time: ";
    t.printTime(t.stop());
    cout << inserts << " inserts\n";
    cout << deletes << " deletes\n\n";
    
    // leftist heap building
    inserts = 0;
    deletes = 0;
    cout << "LeftistHeap Heap\n";
    t.start();
    LeftistHeap lHeap;
    for (int i = 0; i < s; i++) {
        lHeap.insert(array[i]);
    }
        
    cout << "Build time: ";
    t.printTime(t.stop());
    
    // leftist heap insert/deleteMin
    srand(seed);
    t.start();
    for (int j = 0; j < s * 0.1; j++) {
        float r = (float)rand() / (float)RAND_MAX;
        if (r < 0.5) {
            lHeap.deleteMin();
            deletes++;
        } else {
            lHeap.insert(rand() % (4 * s) + 1);
            inserts++;
        }
    }
    cout << "Insert/Delete Time: ";
    t.printTime(t.stop());
    cout << inserts << " inserts\n";
    cout << deletes << " deletes\n\n";

    // skew heap building
    inserts = 0;
    deletes = 0;
    cout << "Skew Heap\n";
    t.start();
    SkewHeap sHeap;
    for (int i = 0; i < s; i++) {
        sHeap.insert(array[i]);
    }
    cout << "Build time: ";
    t.printTime(t.stop());
    
    // skew heap insert/delete
    srand(seed);
    t.start();
    for (int j = 0; j < s * 0.1; j++) {
        float r = (float)rand() / (float)RAND_MAX;
        if (r < 0.5) {
            sHeap.deleteMin();
            deletes++;
        } else {
            sHeap.insert(rand() % (4 * s) + 1);
            inserts++;
        }
    }

    cout << "Insert/Delete Time: ";
    t.printTime(t.stop());
    cout << inserts << " inserts\n";
    cout << deletes << " deletes\n\n";
}