int main() {

    MaxHeap<int> maxheap = MaxHeap<int>(100);
    cout<<maxheap.size()<<endl;

    return 0;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
	if( nrhs!=3 )
		mexErrMsgTxt("This function requires 3 arguments\n");
	if( !mxIsNumeric(prhs[0]) )
		mexErrMsgTxt("parameter 1 missing!\n");
	if( !mxIsNumeric(prhs[1]) )
		mexErrMsgTxt("parameter 2 missing!\n");
	if( !mxIsNumeric(prhs[2]) )
		mexErrMsgTxt("parameter 3 missing!\n");


	// retrieve the heap
	MaxHeap<double>*  heap;
	retrieve_heap( prhs[0], heap);
	// retrieve the parameters
	int index;
	retrieve_index( prhs[1], index );
	double cost;
	retrieve_cost( prhs[2], cost);

	// push in the PQ
	try{
        heap->push( cost, index-1 );
    } 
    catch( InvalidKeyIncreaseException exc ){
        return;
    }
	// return control to matlab
	return;
}
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];
    }


}
Exemple #4
0
int main(int arg, char* argv[])
{
    int dummy;
    MaxHeap<int> mh;

    mh.Insert(9).Insert(3).Insert(8).Insert(2).Insert(7).Insert(6).Insert(4).Insert(5).Insert(1);
    cout << "MaxHeap elements by level: " << mh << endl;
    mh.PrintTreeVertically(cout, 60);

    mh.DeleteMax(dummy);
    cout << "MaxHeap elements by level: " << mh << endl;
    mh.PrintTreeVertically(cout, 60);

    mh.DeleteMax(dummy);
    cout << "MaxHeap elements by level: " << mh << endl;
    mh.PrintTreeVertically(cout, 40);

    mh.DeleteMax(dummy);
    cout << "MaxHeap elements by level: " << mh << endl;
    mh.PrintTreeVertically(cout, 40);

    // Sorting
    int a[] = { 0, 1, 4, 8, 9, 2, 1, 4, 3, 5, 7, 6, 3 };
#define ELEM_COUNT(a)   (sizeof(a) / sizeof(a[0]))

    MaxHeap<int>::Sort(a, ELEM_COUNT(a));
    cout << "Sorting with MaxHeap:" << endl;
    for (int i = 0; i < ELEM_COUNT(a); i++) {
        cout << a[i] << ", ";
    }
    cout << endl;

    return 0;
}
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();
}
int main()
{
    MaxHeap<int> heap;
    int n=5;
    cout<<"初始数组大小:"<<n<<endl;;
    //cin>>n;
    cout<<"输入的数组:";
    //a = new int[n];
    int a[6]={0,5,4,11,3,7};
    for(int i=1;i<=n;i++)
        //cin>>a[i];
        cout<<a[i]<<" ";
    cout<<endl;
    cout<<"初始化为最大堆:"<<endl;
    heap.Initialize(a,n,100);
    heap.Output();
    cout<<"\n插入10之后的最大堆:\n";
    heap.Insert(10);
    heap.Output();
    cout<<"当前堆的大小:"<<heap.Size()<<endl;
    cout<<"\n删除最大数之后的最大堆:\n";
    int b=0;
    heap.DeleteMax(b);
    heap.Output();
    cout<<"当前堆的大小:"<<heap.Size()<<endl;
	getchar();
    return 0;
}
void heapSortAsc(int array[], int arrLength) {

	MaxHeap h = buildMaxHeap(array, arrLength);
	int temp = h.size;
	for(int i = arrLength-1; i > 0; i--) {
		swap(&array[0], &array[i]);
		h.size -= 1;
		h.heapify(0);
	}
	h.size = temp;
	h.printHeap();
}
double greedyKnapsack(knapsack &k){
	MaxHeap<float, int>* heap = new MaxHeap<float, int>(&compare);
	for(int counter = 0; counter < k.getNumObjects(); counter++){
		double key = (double) k.getValue(counter) / k.getCost(counter);
		heap->add(key, counter);
	}
	while(! heap->empty()){
		int next = heap->extractMaxHeapMaximum();
		if(k.getCost(next) + k.getCurrentCost() <= k.getCostBound()){
			k.select(next);
		}
	}
	return k.getCurrentValue();
}
Exemple #9
0
int main()
{
	MinHeap<int> minHeap;
	MaxHeap<int> maxHeap;

	minHeap.push(10);
	minHeap.push(5);
	minHeap.push(12);
	minHeap.push(3);
	minHeap.push(4);

	maxHeap.push(10);
	maxHeap.push(5);
	maxHeap.push(12);
	maxHeap.push(3);
	maxHeap.push(4);

	while ( !minHeap.empty()) {
		std::cout << minHeap.top() << " ";
		minHeap.pop();
	}
	std::cout << std::endl;

	while ( !maxHeap.empty()) {
		std::cout << maxHeap.top() << " ";
		maxHeap.pop();
	}
	std::cout << std::endl;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
	if( nrhs!=1 )
		mexErrMsgTxt("This function requires 1 arguments\n");
	if( !mxIsNumeric(prhs[0]) )
		mexErrMsgTxt("parameter 1 missing!\n");

	// retrieve the heap
	MaxHeap<double>*  heap;
	retrieve_heap( prhs[0], heap);

	// pop top element in the PQ
	plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
	*mxGetPr(plhs[0]) = heap->size();
}
Exemple #11
0
int main()
{
	int num[] = {8,3,1,5,9,4,2,45,7,22,23,6,-1};
	int size = sizeof(num)/sizeof(num[0]);
	vector<int> arr1(num,num+size);
	vector<int> arr2;
    
    MaxHeap *heap = new MaxHeap(arr1);
    if(!heap)return 0;
    
	arr2 = heap->heapSort();
	for_each(arr2.begin(),arr2.end(),[](int a){cout<< a <<" ";});
	cout<<endl;
    delete heap;
	return 0;
}
 double findMedian() {
   /*
   Three cases: since abs(max_heap_.size() - min_heap_.size()) <= 1
   denote x as min(max_heap_.size() - min_heap_.size())
   1) size x and x means even elements so it just the average of max of first
   heap and min of second heap 2) size x + 1 and x means odd elements so the
   max of the first heap is the median element 3) size x and x + 1 means odd
   elements so the min of the second heap is the median element
   */
   if (max_heap_.size() == min_heap_.size()) {
     return (double)(max_heap_.top() + min_heap_.top()) / 2.0;
   } else if (max_heap_.size() > min_heap_.size()) {
     return max_heap_.top();
   } else {
     return min_heap_.top();
   }
 }
Exemple #13
0
void printLocalMax(const int *arr, size_t len, size_t window) {
  MaxHeap<ValueWrapper> h;
  for (int i = 0; i < window - 1; ++i) {
    h.add(ValueWrapper(arr[i], i));
  }
  for (int i = window - 1; i < len; ++i) {
    h.add(ValueWrapper(arr[i], i));
    for(;;) {
      ValueWrapper *r = h.top();
      if (i - r->pos < window) {
	cout << r->value << endl;
	break;
      }
      h.pop();
    }
  }
}
Exemple #14
0
int main()
{
/*    MaxHeap<int> h = {10,8,7,7,6,3,2,0,1};
    h.Add(11);
    h.print();
    h.RemoveTop();
    h.print(); */

    MaxHeap<int> h = {4,6,2,8,9,2,10};

    std::vector<int> s_vec;
    s_vec = h.Sort();

    for(int v : s_vec)
        std::cout<<v<<" ";
    std::cout<<"\n";

    return 0;
}
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;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
	if( nrhs!=1 )
		mexErrMsgTxt("This function requires 3 arguments\n");
	if( !mxIsNumeric(prhs[0]) )
		mexErrMsgTxt("parameter 1 missing!\n");

	// retrieve the heap
	MaxHeap<double>*  heap;
	retrieve_heap( prhs[0], heap);


	// extract head before popping
	pair<double, int> curr = heap->top();
	plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
	*mxGetPr(plhs[0]) = curr.second+1;
	plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
	*mxGetPr(plhs[1]) = curr.first;

	// pop top element in the PQ
	heap->pop();
}
Exemple #17
0
	void AddLiveNode(float up, float cp, int cw, bool ch, int level){
		bbnode * b=new bbnode;
		b->parent=E;
		b->LChild=ch;
		HeapNode N;
		N.uprofit=up;
		N.profit=cp;
		N.weight=cw;
		N.level=level;
		N.ptr=b;
		H->Insert(N);
	}
Exemple #18
0
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;
}
int main() {

	int N = 7;
	int a[N];
	for (int i = 0; i < N; ++i)
		a[i] = N-(i);

	KnuthShuffle(a, N);

	cout << "***Before Heap Sort***" << endl;
	for (int i = 0; i < N; ++i)
		cout << a[i] << " ";
	cout << endl;

	MaxHeap<int> maxHeap;
	maxHeap.Sort(a, N);

	cout << "***After Heap Sort***" << endl;
	maxHeap.PrintHeap(N);

	return 0;
}
double testHeap(T testData[], int n, bool isHeapify) {
    clock_t startTime = clock();
    MaxHeap<T> *maxHeap;
    if (isHeapify) {
        maxHeap = new MaxHeap<T>(testData, n);
    } else {
        maxHeap = new MaxHeap<T>();
        for (int i = 0; i < n; ++i) {
            maxHeap->add(testData[i]);
        }
    }

    T *arr = new T[n];
    for (int j = 0; j < n; ++j) {
        arr[j] = maxHeap->extractMax();
    }

    for (int k = 1; k < n; ++k) {
        assert(arr[k - 1] >= arr[k]);
    }
    std::cout << "Test MaxHeap completed." << std::endl;
    clock_t endTime = clock();
    return double(endTime - startTime) / CLOCKS_PER_SEC;
}
  void addNum(int num) {
    if (max_heap_.empty()) {
      max_heap_.push(num);
    } else {
      if (num > max_heap_.top()) {
        min_heap_.push(num);
      } else {
        max_heap_.push(num);
      }

      // Maintain abs(max_heap_.size() - min_heap_.size()) <= 1
      if (max_heap_.size() > min_heap_.size() + 1) {  // max_heap_ too large
        min_heap_.push(max_heap_.top());
        max_heap_.pop();
      } else if (min_heap_.size() >
                 max_heap_.size() + 1) {  // min_heap_ too large
        max_heap_.push(min_heap_.top());
        min_heap_.pop();
      }
    }
  }
Exemple #22
0
int main()
{
	MaxHeap<int> mh;
	mh.Push(3);
	mh.Push(4);
	mh.Push(8);
	mh.Push(1);
	mh.Push(9);
	for (int i = 0; i < mh.Size(); ++i) {
		std::cout << mh[i] << " ";
	}
	std::cout << std::endl;
	std::cout << "==========" << std::endl;
	std::cout << mh.Pop() << std::endl;
	for (int i = 0; i < mh.Size(); ++i) {
		std::cout << mh[i] << " ";
	}
	std::cout << std::endl;
	std::cout << "==========" << std::endl;
	MinHeap<int> minH;
	minH.Push(3);
	minH.Push(4);
	minH.Push(8);
	minH.Push(1);
	minH.Push(9);
	for (int i = 0; i < minH.Size(); ++i) {
		std::cout << minH[i] << " ";
	}
	std::cout << std::endl;
	std::cout << minH.Pop() << std::endl;
	for (int i = 0; i < minH.Size(); ++i) {
		std::cout << minH[i] << " ";
	}
	std::cout << std::endl;
	std::cout << "---" << std::endl;
	std::cout << re(3, 4) << std::endl;
	std::vector<int> A{3, 6, 5};
	A.pop_back();
	for (auto &it : A) {
		std::cout << it << std::endl;
	}
	std::cout << A.front() << "," << A.size() << std::endl;
	return 0;
}
int main(void) {
	MaxHeap *heap = new MaxHeap();
	
	int data;
	int listLen;
	TreeNode *nodeList[10];

	cin >> listLen;
	for (int i=0; i<listLen; i++) {
		cin >> data;
		nodeList[i] = new TreeNode(data);
		heap->pushTreeNode(nodeList[i]);
	}

	heap->printAllNode();
	heap->popTreeNode();
	heap->printAllNode();

	heap->popTreeNode();
	heap->printAllNode();

	heap->sortData();
	return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
	//Binary Search Tree
	int bstArraySize = 11;		
	int bstArray[] = { 15, 6, 18, 3, 7, 17, 20, 2, 13, 4, 9 };
	cout << "BST Input Array: " ;
	PrintArray(bstArray, bstArraySize);

	BST myBST;
	for (int i = 0; i < bstArraySize; i++)
		myBST.Insert(myBST.root, bstArray[i]);

	cout << "InOrderTreeWalk: ";
	myBST.InOrderTreeWalk(myBST.root);
	cout << endl;

	cout << "PreOrderTreeWalk: ";
	myBST.PreOrderTreeWalk(myBST.root);
	cout << endl;

	cout << "PostOrderTreeWalk: ";
	myBST.PostOrderTreeWalk(myBST.root);
	cout << endl;

	myBST.Search(myBST.root, 100);
	myBST.Search(myBST.root, 20);
	myBST.Maximum(myBST.root);
	myBST.Minimum(myBST.root);
	myBST.SearchParent(myBST.root, 9);

	cout << "Delete Node 15: ";
	myBST.Delete(myBST.root, 15);	
	myBST.InOrderTreeWalk(myBST.root);
	cout << endl;
	cout << endl;

	//Max Heap
	int table[] = {16, 4, 10, 14, 7, 9, 3, 2, 8, 1, 0, 0, 0, 0};
	int heapSize = 10;
	int* maxHeapArray = new int[15];

	for (int i = 0; i < heapSize; i++)
		*(maxHeapArray + i) = table[i];
	cout << "Max-Heap: ";
	PrintArray(maxHeapArray, heapSize);
	MaxHeap myMaxHeap;
	myMaxHeap.MaxHeapify(maxHeapArray, heapSize, 1);
	cout << "Max-Heapified 1: ";
	PrintArray(maxHeapArray, heapSize);

	for (int i = 0; i < heapSize; i++)
		*(maxHeapArray + i) = table[i];
	cout << "Max-Heap: ";
	PrintArray(maxHeapArray, heapSize);
	myMaxHeap.BuildHeap(maxHeapArray, heapSize);
	cout << "BuildHeap: ";
	PrintArray(maxHeapArray, heapSize);
	cout << "HeapExtractMax: " << myMaxHeap.HeapExtractMax(maxHeapArray, heapSize) << endl;
	cout << "Remaining: ";		
	PrintArray(maxHeapArray, heapSize - 1);
	cout << "HeapInsert 32: \n";
	myMaxHeap.HeapInsert(maxHeapArray, heapSize - 1, 32);
	cout << "New Heap: ";
	PrintArray(maxHeapArray, heapSize);

	for (int i = 0; i < heapSize; i++)
		*(maxHeapArray + i) = table[i];
	cout << "Max-Heap: ";
	PrintArray(maxHeapArray, heapSize);
	myMaxHeap.HeapSort(maxHeapArray, heapSize);
	cout << "HeapSort: ";
	PrintArray(maxHeapArray, heapSize);
	cout << endl;

	//AVL tree
	int avlArraySize =11;
	//int avlArray[] = { 15, 6, 18, 3, 7, 17, 20, 2, 13, 4, 9 };
	int avlArray[] = { 30, 50, 40, 60, 70, 17, 20, 2, 13, 4, 9 };
	cout << "AVL Input Array: ";
	PrintArray(avlArray, avlArraySize);
	AVL myAVL;
	for (int i = 0; i < avlArraySize; i++)
	{
		myAVL.Insert(myAVL.root, avlArray[i]);
		myAVL.InOrderTreeWalk(myAVL.root);
		cout << endl;
	}

	cout << "Delete 30: \n";
	myAVL.Delete(myAVL.root, 30);
	myAVL.InOrderTreeWalk(myAVL.root);
	cout << endl;

	cout << "Delete 13: \n";
	myAVL.Delete(myAVL.root, 13);
	myAVL.InOrderTreeWalk(myAVL.root);
	cout << endl;

	cout << "Delete 20: \n";
	myAVL.Delete(myAVL.root, 20);
	myAVL.InOrderTreeWalk(myAVL.root);
	cout << endl;

	//RB tree
	int rbArraySize = 11;
	//int rbArray[] = { 1, 3, 2, 4, 5, 6, 8, 7, 9, 10, 11 };
	int rbArray[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
	cout << "RB Input Array: ";
	PrintArray(rbArray, rbArraySize);

	RB myRB;
	for (int i = 0; i < rbArraySize; i++)
	{
		myRB.Insert(myRB.root, myRB.root, rbArray[i]);
		myRB.InOrderTreeWalk(myRB.root);
		cout << endl;
	}

	RB myRB2;
	myRB2.Insert(myRB2.root, myRB2.root, 50);
	myRB2.Insert(myRB2.root, myRB2.root, 20);
	myRB2.Insert(myRB2.root, myRB2.root, 30);
	myRB2.Insert(myRB2.root, myRB2.root, 40);
	myRB2.Insert(myRB2.root, myRB2.root, 45);
	myRB2.Insert(myRB2.root, myRB2.root, 43);
	myRB2.Insert(myRB2.root, myRB2.root, 44);
	myRB2.Insert(myRB2.root, myRB2.root, 42);
	myRB2.InOrderTreeWalk(myRB2.root);
	cout << endl;
	myRB2.Delete(myRB2.root, 50);
	myRB2.InOrderTreeWalk(myRB2.root);
	cout << endl;




	//4.2 Minimal Tree : Given a sorted(increasing order) array with unique integer elements, write an
	//	algorithm to create a binary search tree with minimal height.
	int minTree[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	TreeNode* n = MinTreeInsert(minTree, 0, 9);

	//4.5 Validate BST: Implement a function to  check if a  binary tree is a binary  search  tree.
	int* valArray = new int[10];
	CopyBST(n, valArray, 10, 0);
	for (int i = 0; i < 10; i++)
		cout << i << " ";
	cout << endl;

	cout << ValidateBST(n, -1000000)<<endl;

	//4.8 First Common Ancestor: Design an algorithm and write code to find the first common ancestor 
	// of two nodes in a binary tree.Avoid storing additional nodes in a data structure.NOTE: This is not
	//necessarily a binary search tree.

	int binaryTree[] = { 3, 5, 2, 6, 9, 7, 1, 4, 8, 10};
	TreeNode* bt = MinTreeInsert(binaryTree, 0, 9);



	//4.9 BST Sequences : A binary search tree was created by traversing through an array from left to right
	//	and inserting each element.Given a binary search tree with distinct elements, print all possible
	//	arrays that could have led to this tree.
	 

	//4.1O Check Subtree : Tl and T2 are two very large binary trees, with Tl much bigger than T2.Create an
	//	algorithm to determine ifT2 is a subtree of Tl.
	//	A tree T2 is a subtree of Tl if there exists a node n in Tl such that the subtree of n is identical to T2.
	//	That is, if you cut off the tree at node n, the two trees would be identical.


	//4.11 Random Node : You are implementing a binary search tree class from scratch, which, in addition
	//	to insert, find, and delete, has a method getRandomNode() which returns a random node
	//	from the tree.All nodes should be equally likely to be chosen.Design and implement an algorithm
	//	for getRandomNode, and explain how you would implement the rest of the methods.


	getchar();

	return 0;
}
Exemple #25
0
#include "../../data_structures/max_heap.hpp"

namespace max_heap_test {

  TEST_CASE( "Basic FIFO operations maintains max ordering", "[min_heap]" ) {
    MaxHeap<int> h;

    REQUIRE( h.size() == 0 );

    h.insert(5);
    REQUIRE( h.peek() == 5 );

    h.insert(4);
    REQUIRE( h.peek() == 5 );

    h.insert(6);
    REQUIRE( h.peek() == 6 );

    REQUIRE( h.pop() == 6 );
    REQUIRE( h.pop() == 5 );
    REQUIRE( h.pop() == 4 );

    for(int i=0; i<500; i++){
      h.insert(rand() % 100);
    }
    for(int i=0; i<h.size(); i++){
      REQUIRE( h.pop() >= h.peek() );
    }
  }

}
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();
    }
}
int _tmain(int argc, _TCHAR* argv[]){
	MaxHeap<int> myHeap;
	const int number = 10;
	int s;
	int key[10] = { 1, 4, 7, 8, 5, 2, 3, 6, 9, 0 };
	////若要使用键盘输入,把这些注释取消即可
	//cout << "请输入10个数:" << endl;
	//for (int i = 0; i < 10; i++)
	//{
	//	cin >> s;
	//	key[i] = s;
	//}
	cout << "原数组:" << endl;
	for (int i = 0; i < 10; i++)
		cout << key[i] << " ";
	cout << endl << endl;

	int myArray[11];////////
	for (int i = 0; i < 10; i++)
	{
		myArray[i + 1] = key[i];
	}
	myArray[0] = -1;
	myHeap.Initialize(myArray, number, 20);
	cout << "最大堆(层序):" << endl;
	cout << myHeap;
	HeapSort(myArray, number);
	cout << "堆排序:" << endl;
	for (int j = 1; j <= number; j++)
	{
		cout << myArray[j] << " ";
	}
	cout << endl << endl;

	cout << "Huffman编码:" << endl;
	int i;
	struct node huffmantree;
	memset(mark, 0, sizeof(mark));
	memset(huffman, 0, sizeof(huffman));
	for (i = 0; i < 10; i++)
	{
		huffman[i].key = key[i];
	}
	huffmantree = HuffmanTree(mark, huffman, 10);
	cout << "前序:";
	PreOrder(&huffmantree);
	cout << "\n中序:";
	MidOrder(&huffmantree);
	cout << endl << endl;

	cout << "二叉搜索树:" << endl;
	struct node *bs;
	for (int i = 0; i < 10; i++)
	{
		bstree[i].key = key[i];
	}
	bs = bstreeinsert(bstree);
	cout << "前序:";
	PreOrder(bs);
	cout << "\n中序:";
	MidOrder(bs);////////中序遍历(二叉搜索树的中序遍历是有序的)
	cout << endl;
	system("pause");
	return 0;
}