基于QT Creator的红黑树

采用QT的按钮实现插入删除等控制,采用QT的文本框输出红黑树。

main.cpp

#include "mainwindow.h"
#include <QApplication>
#include "rbtree.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.setWindowTitle("RedBlackTree");
    w.setObjectName("MainWindow");
    w.setStyleSheet("#MainWindow{border-image:url(C://Users//zhangyq//Desktop//bg.png);}");
    w.show();

    return a.exec();
}

mainwiindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::GetNum(int &i)   //获得键盘输入的数据
{
    bool ok;
    i = QInputDialog::getInt(this, tr("Prossing..."),
        tr("Number: "), 0, 0, 1000, 1, &ok);
    if(!ok) return false;
    return true;
}

void MainWindow::on_AddBtn_clicked()   //Add槽函数
{
    int num;

    if(!GetNum(num)) return;   //未输入数据,则直接结束

    if(t.find(num))  //如果数据已经存在,根据红黑树的定义,树中不能出现相同节点,此时向用户反馈错误信息
        QMessageBox::warning(this, tr("Warning"), tr("Number Existing!"),QMessageBox::Abort);

    t.insert(num);  //插入

    QString str;

    t.print(str);   //显示

    ui->DisplyEdit->setText(str);

}

void MainWindow::on_DelBtn_clicked()  //Deleet槽函数
{
    int num;

    if(!GetNum(num)) return;  //未输入数据,则直接结束

    if(!t.find(num))     //如果树中无此节点,向用户反馈错误信息
        QMessageBox::warning(this, tr("Warning"), tr("Can't Find!"),QMessageBox::Abort);

    t.erase(num);   //删除

    QString str;

    t.print(str);  //显示

    ui->DisplyEdit->setText(str);

}

void MainWindow::on_ResetBtn_clicked()   //Clear槽函数
{
    t.destroy(t.root());   //销毁红黑红树,释放节点
    t.root() = nullptr;
    ui->DisplyEdit->setText("");
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QInputDialog>
#include <rbtree.h>
#include <QString>
#include <QMessageBox>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    bool GetNum(int &);  //获取输入数据

private slots:
    void on_AddBtn_clicked();    //Add槽函数

    void on_DelBtn_clicked();    //Delete槽函数

    void on_ResetBtn_clicked();  //Clear槽函数

private:
    RBTree t;
    Ui::MainWindow *ui;

};

#endif // MAINWINDOW_H

rbtree.cpp

#include "rbtree.h"

void RBTree::rotate_left(Node * 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值