QT实现minheap(简单图形界面掌握)
生活随笔
收集整理的這篇文章主要介紹了
QT实现minheap(简单图形界面掌握)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本次項(xiàng)目的文件結(jié)構(gòu)圖如下
在windows下的運(yùn)行效果如圖
堆本質(zhì)上就是平衡二叉樹(shù),這里采用的是線段樹(shù)來(lái)實(shí)現(xiàn)的。比較簡(jiǎn)單
但是在使用的QT自帶的QTTreeWidget來(lái)顯示的時(shí)候就實(shí)現(xiàn)了一個(gè)講線段樹(shù)還原成一般的平衡二叉樹(shù)的過(guò)程,這是很有趣的。
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow> #include "minheap.h" namespace Ui { class MainWindow; }class MainWindow : public QMainWindow {Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);~MainWindow();private slots:void on_pushButton_clicked();void on_pushButton_2_clicked();void on_pushButton_3_clicked();private:Ui::MainWindow *ui;MinHeap_ minheap;void buildTheTree(); };#endif // MAINWINDOW_Hminheap.h
#ifndef MINHEAP_H #define MINHEAP_H#ifndef DefaultSize #define DefaultSize 10 #endif // class MinHeap_ { public:MinHeap_(int sz = DefaultSize);MinHeap_(int arr[], int m);~MinHeap_() { delete[] heap; }bool Insert(const int& x);bool RemoveMin(int &x);bool IsEmpty()const {return currentsize == 0;}bool IsFull()const {return currentsize == maxHeapSize;}void makeEmpty(){currentsize = 0;}void ReSet(int arr[], int m);const int* getHeap();int getHeapSize(); private:int currentsize;int maxHeapSize;int * heap;void shiftDown(int start, int m);void shiftUp(int start); };#endif // MINHEAP_Hmain.cpp
#include "mainwindow.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);MainWindow w;w.setWindowTitle("MinHeap(最小堆)");w.show();return a.exec(); }mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <iostream> using namespace std; #include <queue> MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow) {ui->setupUi(this);ui->treeWidget->setHeaderLabel("最小堆"); }MainWindow::~MainWindow() {delete ui; } void MainWindow::buildTheTree(){if (ui->treeWidget->topLevelItemCount() > 0) ui->treeWidget->clear();const int * heap = minheap.getHeap();int heapSize = minheap.getHeapSize();if (heap == NULL || heapSize <= 0){return;}QTreeWidgetItem * Top = new QTreeWidgetItem(QStringList(QString::number(heap[0])));ui->treeWidget->addTopLevelItem(Top);queue<QTreeWidgetItem*> q;q.push(Top);int pp = 1;while(!q.empty() && pp < heapSize) {QTreeWidgetItem* now = q.front();q.pop();for (int i = 0; i < 2 && pp < heapSize; ++i, ++pp) {QTreeWidgetItem* newI = new QTreeWidgetItem(QStringList(QString::number(heap[pp])));now->addChild(newI);q.push(newI);}} }void MainWindow::on_pushButton_clicked() {QString Ims = ui->lineEdit->text();ui->lineEdit->clear();if (Ims.isEmpty()) return;QStringList numStr = Ims.split(' ',QString::SkipEmptyParts);int *nums = new int [numStr.size()];for (int i = 0; i < numStr.size(); ++i) {nums[i] = numStr[i].toInt();}minheap.ReSet(nums, numStr.size());buildTheTree();delete[]nums; }void MainWindow::on_pushButton_2_clicked() {int text = 0;if (minheap.RemoveMin(text)) {buildTheTree();} }void MainWindow::on_pushButton_3_clicked() {QString ans;while (!minheap.IsEmpty()) {int text; minheap.RemoveMin(text);ans += QString::number(text) + " ";}ui->lineEdit->setText(ans); }minheap.cpp
#include "minheap.h" #include <iostream> using namespace std;MinHeap_::MinHeap_(int sz) {currentsize = 0;maxHeapSize = (DefaultSize > sz)? DefaultSize : sz;heap = new int [maxHeapSize];if (!heap) {std::cout << "Memery build up error!\n";} } MinHeap_::MinHeap_(int arr[], int m){currentsize = (m > 0)? m : 0;maxHeapSize = (DefaultSize > m)? DefaultSize : m;heap = new int [maxHeapSize];if (arr && heap) {for (int i = 0; i < m; ++i) {heap[i] = arr[i];}int currentPos = currentsize / 2 - 1;while (currentPos >= 0) {shiftDown(currentPos, currentsize-1);currentPos--;}}else if (heap){for (int i = 0; i < m; ++i) heap[i] = 0;}else {std::cout << "Memery build up error!\n";} } bool MinHeap_::Insert(const int &x) {if(IsFull()) return false;heap[currentsize] = x;shiftUp(currentsize);currentsize++;return true; } bool MinHeap_::RemoveMin(int &x){if (currentsize <= 0) return false;x = heap[0]; heap[0] = heap[currentsize-1];currentsize --;shiftDown(0,currentsize-1);return true; } const int* MinHeap_:: getHeap(){return heap; }int MinHeap_::getHeapSize(){return currentsize; }void MinHeap_::ReSet(int arr[], int m){delete[] heap; heap = NULL;currentsize = (m > 0)? m : 0;maxHeapSize = (DefaultSize > m)? DefaultSize : m;heap = new int [maxHeapSize];if (arr && heap) {for (int i = 0; i < m; ++i) {heap[i] = arr[i];}int currentPos = currentsize / 2 - 1;while (currentPos >= 0) {shiftDown(currentPos, currentsize-1);currentPos--;}}else if (heap){for (int i = 0; i < m; ++i) heap[i] = 0;}else {std::cout << "Memery build up error!\n";} }void MinHeap_::shiftDown(int start, int m){if (start < 0 || m >= currentsize || start >= currentsize) return;int i = start, j = 2*i+1; // j point the left son of the heap[i];int temp = heap[start];while (j <= m) {if (j < m && heap[j] > heap[j+1]) j++; // make sure j point the smaller one in the two sonsif (temp <= heap[j]) break;else {heap[i] = heap[j]; i = j; j = 2*j+1;}}heap[i] = temp; } void MinHeap_::shiftUp(int start){if (start < 0 || start >= currentsize) return;int j = start, i = ( j - 1 ) / 2;int temp = heap[j];while (i >= 0) {if (heap[i] <= temp)break;else {heap[j] = heap[i];j = i;i = (j - 1) / 2;}}heap[j] = temp; }總結(jié)
以上是生活随笔為你收集整理的QT实现minheap(简单图形界面掌握)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 简析并查集[C/C++]
- 下一篇: C++多线程简单入门(Windows版本