Exemple #1
0
// 测试堆
void TestHeap()
{
	Heap<int, greater<int>> heap;
	heap.Push(3);
	heap.Push(5);
	heap.Push(1);
	heap.Push(4);
	heap.Push(5);
	heap.Push(1);
	heap.Push(8);

	while (!heap.Empty())
	{
		cout<<heap.Top()<<" ";
		heap.Pop();
	}
	cout<<endl;

	//int array[10] = {9,1,3,5,6,7,8,0,2,4};
	int array[10] = {10,16,18,12,11,13,15,17,14,19};
	Heap<int> heap1(array, 10);

	while (!heap1.Empty())
	{
		cout<<heap1.Top()<<" ";
		heap1.Pop();
	}
	cout<<endl;
}
Exemple #2
0
	HuffmanTree(const T *a, size_t n)
	{
		struct NodeCompare
		{
			bool operator() (const Node *left, const Node *right)
			{
				return (left->_weight) <( right->_weight);
			}
		};

		Heap<Node*, NodeCompare> minHeap;

		for (size_t i = 0; i < n; ++i)
		{
			minHeap.Push(new Node(a[i]));
		}

		while (minHeap.Size() > 1)
		{
			Node *left = minHeap.Top();
			minHeap.Pop();
			Node *right = minHeap.Top();
			minHeap.Pop();

			Node *parent = new Node(left->_weight + right->_weight);
			parent->_left = left;
			parent->_right = right;

			minHeap.Push(parent);
		}

		_root = minHeap.Top();
	}
Exemple #3
0
void Dijkstra::Solve_distance()
{
    weighted_vertex wt = {0,0,0};
    heap.Push(wt);
    s1 = "0" + s1;
    s2 = "0" + s2;
    while (!heap.Empty())
    {
        weighted_vertex top = heap.top();
        vertex now_top = top.v;
 
        if (now_top.y + 1 < s2.length() && s1[now_top.x] != s2[now_top.y + 1])
        {
            vertex temp = now_top;
            temp.y++;
            weighted_vertex wt;
            wt.w = Add_potential(now_top, temp, top.w + 1);
            wt.v = temp;
            heap.Push(wt);
        }
       
        if (now_top.x + 1 < s1.length() && s1[now_top.x + 1] != s2[now_top.y])
        {
            vertex temp = now_top;
            temp.x++;
            weighted_vertex wt;
            wt.w = Add_potential(now_top, temp, top.w + 1);
            wt.v = temp;
            heap.Push(wt);
        }
       
        if (now_top.x + 1 < s1.length() && now_top.y + 1 < s2.length())
        {
            int weight = 0;
            if (s1[now_top.x + 1] != s2[now_top.y + 1]) weight = 1;
            vertex temp = now_top;
            temp.x++;
            temp.y++;
            weighted_vertex wt;
            wt.w = Add_potential(now_top, temp, top.w + 1);
            wt.v = temp;
            heap.Push(wt);
        }
   
        if (now_top.x == (int)s1.length() - 1 && now_top.y == (int)s2.length() - 1)
        {
            vertex t = {0,0};
            answer = Delete_potentials(t, now_top, top.w);
            break;
        }
    }
}
Exemple #4
0
	Node* CreateTree(const T*a, size_t size, const T&invalid)
	{
		Heap<Node*, Less<Node*>> minHeap;

		for (size_t i = 0; i < size; ++i)
		{
			if (a[i] != invalid)
			{
				Node* tmp = new Node(a[i]);
				minHeap.Push(tmp);
			}
		}

		while (!minHeap.empty())
		{
			Node* left = minHeap.Top();
			minHeap.Pop();
			Node* right = NULL;

			if (!minHeap.empty())
			{
				right = minHeap.Top();
				minHeap.Pop();
			}

			Node* parent = NULL;

			if (right)
			{
				parent = new Node(left->_weight + right->_weight);
			}
			else
			{
				parent = new Node(left->_weight);
			}

			parent->_left = left;
			parent->_right = right;

			if (minHeap.empty())
			{
				return parent;
			}

			minHeap.Push(parent);
		}

		return NULL;
	}
Exemple #5
0
		HuffmanTree(T *arr, int len)
		{
			Heap<HTNode* > hp;

			for(int i = 0; i < len; ++i)
			{
				if(arr[i]._count == 0)    //arr[i]->_weight._ch 该字符没有出现过
					continue;

				hp.Push(new HTNode(arr[i]));       //创建一个新的树节点,插入到堆中
			}

			_root = CreateTree(hp);
		}
Exemple #6
0
void RollModel::Roll(int min, int max, int times)
{
    struct occurrence_t{
        int value;
        uint count;
        bool operator < (const occurrence_t &o) const{
            return count > o.count ||
                    (count == o.count && value < o.value);
        }
    };

    beginResetModel();
    m_data.resize(times);
    m_total = 0;
    m_min = GINT32_MAX;
    m_max = GINT32_MIN;
    m_mode.clear();
    m_modeCount = 0;
    m_mean = 0.0;
    m_median = 0.0;
    QHash<int, uint> occurrences;

    for(int i = 0; i < times; i++){
        int X = m_rng.U_Discrete(min, max);
        m_data[i] = X;
        m_total += X;

        if(X < m_min)
            m_min = X;
        if(X > m_max)
            m_max = X;

        if(occurrences.contains(X))
            occurrences[X]++;
        else
            occurrences.insert(X, 1);
    }
    m_mean = (double)m_total/times;
    
    // Compute the median
    vector<int> sorted_data = m_data;
    std::sort(sorted_data.begin(), sorted_data.end());
    int median_index = sorted_data.size() / 2;
    int val1 = sorted_data[median_index];
    int val2 = (sorted_data.size() & 1) ? val1 : sorted_data[median_index - 1];
    m_median = ((double)val1 + val2) / 2;

    // Compute the mode (using heap sort)
    Heap<occurrence_t> heap;
    for(int k : occurrences.keys())
        heap.Push({k, occurrences[k]});

    if(heap.Count()){
        occurrence_t *t = heap.Top();
        uint mode_count = t->count;
        int cnt = 0;
        vector<int> tmp_mode;
        while(t && (mode_count == t->count)){
            tmp_mode.push_back(t->value);
            heap.Pop();
            t = heap.Top();
            cnt++;
        }

        // If every item is the mode, then there is no mode
        if(cnt < occurrences.keys().count()){
            m_modeCount = mode_count;
            m_mode = tmp_mode;
        }
    }
    endResetModel();
}
std::vector<Aresta *> Grafo::Kruskal() {
	std::vector<Aresta *> retorno;
	//so faz se nao eh direcionado
	if (!this->Direcionado()) {
		//conjunto de vertices e ranks
		std::vector<int> ranks;
		//inicializa conjunto de vertices e ranks
		Heap * nHeap;
		int tamHeapMax = 0;
		conjKruskal.clear();
		ranks.clear();
		for (int i = 0; i < tamGrafo; i++) {
			this->conjKruskal.push_back(i);
			ranks.push_back(1);
			for (int j =0; j < tamGrafo; j++)
				if (this->Conexao(i,j))
					tamHeapMax++;
		}
		nHeap = new Heap(tamHeapMax);
		nHeap->SetTipo(MINIMO);
		for (int i = 0; i < tamGrafo; i++) {
			for (int j =i; j < tamGrafo; j++) {
				if (this->Conexao(i,j)) {
					Aresta * nAresta = new Aresta(*this->GetVertice(i).GetAresta(j));
					nHeap->Push(nAresta);
				}
			}
		}
		
		//nHeap->ListaHeap();
		int id1;
		int id2;
		int id1tmp;
		int id2tmp;

		std::vector<Aresta *> lArestas;

		while(nHeap->GetTamHeap() > 0 ) {
			Aresta * cAresta = nHeap->Pop();
			id1 = cAresta->GetId1();
			id2 = cAresta->GetId2();
			//cout << "id1: " << id1 << " id2:" << id2 << endl;
			
			if ((id1tmp = this->BuscaKruskal(id1)) != (id2tmp = BuscaKruskal(id2))) {
				//cout << "conjuntos diferentes: " << "id1: " << this->BuscaKruskal(id1) << " id2: " << this->BuscaKruskal(id2) << endl;
				retorno.push_back(cAresta);
				if (ranks[id1tmp] != ranks[id2tmp]) {
					int maior = (ranks[id1tmp] > ranks[id2tmp] ? id1tmp : id2tmp);
					int menor = (ranks[id2tmp] > ranks[id1tmp] ? id1tmp : id2tmp);
					conjKruskal[menor] = maior;
					//cout << "ranks: " << " menor: " << ranks[menor] << "maior: " << ranks[maior] << endl;
				} else {
					conjKruskal[id2tmp] = id1tmp;
					ranks[id1tmp] += 1;
					//cout << "aumentou rank" << endl;
				}
			}
		}
	}
	
	return retorno;
}