采用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 *