⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testdriver.cpp

📁 MaxHeap operation like insert and delete
💻 CPP
字号:
#include <iomanip>
#include <string>
#include "MaxBHeap.h"
#include <iostream>

using namespace std;

void main(){
	string command;

	MaxBHeap<int>  maxHeap;

	int* ary;

	unsigned int size;

	cin >> command;

	while (command != "exit")
	{
		if(command == "build"  ){
			//cout << "Build";
			
			

			cin >> size;

			ary = new int[size];

			//cout <<"input";
			unsigned int count = 0;

			while (count < size){
				cin >> ary[count];
				count++;
			}
			cout << "Build max heap ... " << endl;
			maxHeap.Build(ary, size);

			cout << maxHeap << endl;

		}
		else if (command == "insert" ){
			//cout << "Insert";

			cin >> size;

			ary = new int[size];

			unsigned int count = 0;
			
			while (count < size){
				cin >> ary[count];
				count++;
			}

			count=0;
			cout << "Insert heap ... " << endl;
			while (count < size){
				cout <<" key : " << ary[count] << endl;
				maxHeap.Insert(ary[count]);
				cout << maxHeap << endl;
				count++;
			}

			

		}
		else if (command =="delete"){
			//cout << "delete";

			cin >> size;
			cout << "Delete heap ... " << endl;
			unsigned int count = 0;

			while (count < size){

				cout << "  key : "<< setw(5) <<maxHeap.DeleteMax() << endl;

				cout << maxHeap << endl;
			
				count++;
			}

		}
		else if (command =="inckey"){
			unsigned int* pos;

			int* value;
			
			cin >> size;

			pos = new unsigned int[size];
			value = new int[size];
			unsigned int count = 0;
			while (count < size){
				
				cin >> pos[count] >> value[count];
				count++;
			}
			count = 0;
			cout << "Increase key ... " << endl;
			while (count < size){
				cout << " pos : " << setw(5) << pos[count]<< " to " << value[count]<<endl;
				if(maxHeap.IncKey(pos[count],value[count]))
					cout << maxHeap << endl;
				count++;
			}
			
		}
		else if (command =="deckey"){
			/*unsigned int pos;

			int value;
			
			cin >> size;
			unsigned int count = 0;
			while (count < size){
				
				cin >> pos >> value;

				if(minHeap.DecKey(pos,value))
					cout << minHeap << endl;

			count++;
			}
			*/
			unsigned int* pos;

			int* value;
			
			cin >> size;

			pos = new unsigned int[size];
			value = new int[size];
			unsigned int count = 0;
			while (count < size){
				
				cin >> pos[count] >> value[count];
				count++;
			}
			count = 0;
			cout << "Decrease key ... " << endl;
			while (count < size){
				cout << " pos : " << setw(5) << pos[count]<< " to " << value[count] << endl;
				if(maxHeap.DecKey(pos[count],value[count]))
					cout << maxHeap << endl;
				count++;
			}
		}
		else 
			cout << "wrong input";

		cin >> command;
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -