├── bitree.cpp ├── bitree.h ├── bitreemainwindow.cpp ├── bitreemainwindow.h ├── bitreethreadwindow.cpp ├── bitreethreadwindow.h ├── bitreetravelwindow.cpp ├── bitreetravelwindow.h ├── createwindow.cpp ├── createwindow.h ├── main.cpp ├── paint.cpp ├── paint.h ├── ui_bitreemainwindow.h ├── ui_bitreepaint.h ├── ui_bitreethreadwindow.h ├── ui_bitreetravelwindow.h └── ui_createwindow.h /bitree.cpp: -------------------------------------------------------------------------------- 1 | #include "bitree.h" 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | BiTNode::BiTNode() 8 | { 9 | lchild = nullptr; 10 | rchild = nullptr; 11 | lTag = 0; 12 | rTag = 0; 13 | leafNum = 0; 14 | } 15 | 16 | Status BiTNode::root(QString s) 17 | { 18 | lchild=new BiTNode; 19 | rchild=lchild; 20 | lchild->createBiTree(s); 21 | return OK; 22 | } 23 | 24 | Status BiTNode::isEmpty() 25 | { 26 | if(lchild==rchild&&lchild==nullptr) 27 | return OK; 28 | else 29 | return FALSE; 30 | } 31 | 32 | Status BiTNode::createBiTree(QString s) 33 | { 34 | static int i = 0,l = 0; 35 | 36 | if (l > i) 37 | l = 0; 38 | 39 | 40 | if (s[i] == '#') 41 | return OK; 42 | data = s[i]; 43 | 44 | //qDebug()<createBiTree(s); 53 | } 54 | i++; 55 | if (s[i] == '#') 56 | { 57 | rchild = nullptr; 58 | if (this->lchild == nullptr&&this->rchild == nullptr) 59 | l++; 60 | leafNum = l; 61 | if (i == s.length() - 1) 62 | i = 0; 63 | return OK; 64 | } 65 | else 66 | { 67 | rchild = new BiTNode; 68 | rchild->createBiTree(s); 69 | }//abc##d##ef### 70 | if(this->lchild==nullptr&&this->rchild==nullptr) 71 | l++; 72 | leafNum=l; 73 | if (i == s.length() - 1) 74 | i = 0; 75 | return OK; 76 | } 77 | 78 | Status BiTNode::preOrder(QString *preString) 79 | { 80 | preString->append(data); 81 | 82 | if (lchild!=nullptr) 83 | lchild->preOrder(preString); 84 | if (rchild!=nullptr) 85 | rchild->preOrder(preString); 86 | return OK; 87 | } 88 | 89 | Status BiTNode::inOrder(QString *inString) 90 | { 91 | if (lchild!=nullptr) 92 | lchild->inOrder(inString); 93 | inString->append(data); 94 | if (rchild!=nullptr) 95 | rchild->inOrder(inString); 96 | return OK; 97 | } 98 | 99 | Status BiTNode::postOrder(QString *postString) 100 | { 101 | if (lchild!=nullptr) 102 | lchild->postOrder(postString); 103 | if (rchild!=nullptr) 104 | rchild->postOrder(postString); 105 | postString->append(data); 106 | return OK; 107 | } 108 | 109 | Status BiTNode::levelOrderTraverse() 110 | { 111 | int i = 0, j = 1; 112 | BiTNode *queue[1000]; 113 | queue[i] = this; 114 | while (1) 115 | { 116 | if (queue[i]->lchild) 117 | { 118 | queue[j] = queue[i]->lchild; 119 | j++; 120 | } 121 | 122 | if (queue[i]->rchild) 123 | { 124 | queue[j] = queue[i]->rchild; 125 | j++; 126 | } 127 | 128 | i++; 129 | if (i == j) 130 | break; 131 | } 132 | return OK; 133 | } 134 | 135 | Status BiTNode::preOrderTraverse() 136 | { 137 | //qDebug()<lchild==nullptr&&lchild->rchild==nullptr) 159 | { 160 | delete lchild; 161 | lchild=nullptr; 162 | } 163 | 164 | if(rchild != nullptr&&rchild->lchild==nullptr&&rchild->rchild==nullptr) 165 | { 166 | delete rchild; 167 | rchild=nullptr; 168 | } 169 | 170 | 171 | if (lchild!=nullptr) 172 | lchild->~BiTNode(); 173 | if (rchild!=nullptr) 174 | rchild->~BiTNode(); 175 | 176 | } 177 | 178 | Status BiTNode::destroy() 179 | { 180 | 181 | if(lchild!=nullptr&&lchild->lchild==nullptr&&lchild->rchild==nullptr) 182 | { 183 | delete lchild; 184 | lchild=nullptr; 185 | } 186 | 187 | if(rchild != nullptr&&rchild->lchild==nullptr&&rchild->rchild==nullptr) 188 | { 189 | delete rchild; 190 | rchild=nullptr; 191 | } 192 | 193 | 194 | if (lchild!=nullptr) 195 | lchild->destroy(); 196 | if (rchild!=nullptr) 197 | rchild->destroy(); 198 | 199 | return OK; 200 | } 201 | 202 | int BiTNode::Leaves() 203 | { 204 | return this->leafNum; 205 | } 206 | 207 | Status BiTNode::preOrderThreading() 208 | { 209 | static BiTNode *pre=nullptr; 210 | if(pre!=nullptr&&pre->rTag) 211 | pre->rchild=this; 212 | 213 | if(lchild==nullptr) 214 | { 215 | lTag=1; 216 | lchild=pre; 217 | } 218 | if(rchild==nullptr) 219 | rTag=1; 220 | 221 | pre=this; 222 | if(lTag==false) 223 | lchild->preOrderThreading(); 224 | if(rTag==false) 225 | rchild->preOrderThreading(); 226 | return OK; 227 | 228 | } 229 | 230 | Status BiTNode::inOrderThreading() 231 | { 232 | static BiTNode *pre = nullptr; 233 | if (pre != nullptr&&pre->rTag) 234 | pre->rchild = this; 235 | 236 | if (lchild == nullptr) 237 | { 238 | lTag = 1; 239 | lchild = pre; 240 | } 241 | if (rchild == nullptr) 242 | rTag = 1; 243 | 244 | if (lTag == false) 245 | lchild->preOrderThreading(); 246 | pre = this; 247 | if (rTag == false) 248 | rchild->preOrderThreading(); 249 | return OK; 250 | } 251 | 252 | Status BiTNode::postOrderThreading() 253 | { 254 | static BiTNode *pre = nullptr; 255 | if (pre != nullptr&&pre->rTag) 256 | pre->rchild = this; 257 | 258 | if (lchild == nullptr) 259 | { 260 | lTag = 1; 261 | lchild = pre; 262 | } 263 | if (rchild == nullptr) 264 | rTag = 1; 265 | 266 | if (lTag == false) 267 | lchild->preOrderThreading(); 268 | 269 | if (rTag == false) 270 | rchild->preOrderThreading(); 271 | pre = this; 272 | return OK; 273 | } 274 | 275 | int BiTNode::getHeight() 276 | { 277 | int num=this->getHeight(this);return num; 278 | } 279 | 280 | int BiTNode::getHeight(BiTNode*r) 281 | { 282 | if (r == nullptr) 283 | return 0; 284 | int i = getHeight(r->lchild); 285 | int j = getHeight(r->rchild); 286 | return (i < j) ? j + 1 : i + 1; 287 | } 288 | 289 | -------------------------------------------------------------------------------- /bitree.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define TRUE 1 6 | #define FALSE 0 7 | #define OK 1 8 | #define ERROR 0 9 | #define INFEASIBLE -1 10 | #define Overflow -2 11 | 12 | typedef int Status; 13 | 14 | typedef QChar ElemType; 15 | 16 | typedef class BiTNode { 17 | protected: 18 | ElemType data; 19 | bool lTag,rTag; 20 | BiTNode *lchild, *rchild; 21 | int leafNum; 22 | public: 23 | BiTNode(); 24 | ~BiTNode(); 25 | Status root(QString s); 26 | Status isEmpty(); 27 | Status createBiTree(QString s); 28 | Status preOrder(QString *preString); 29 | Status inOrder(QString *inString); 30 | Status postOrder(QString *postString); 31 | Status preOrderTraverse(); 32 | Status inOrderTraverse(); 33 | Status postOrderTraverse(); 34 | Status levelOrderTraverse(); 35 | Status preOrderThreading(); 36 | Status inOrderThreading(); 37 | Status postOrderThreading(); 38 | int Leaves(); 39 | int getHeight(); 40 | int getHeight(BiTNode *r); 41 | Status destroy(); 42 | friend class Paint; 43 | }*BiTreeP; 44 | -------------------------------------------------------------------------------- /bitreemainwindow.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ALynns/QTBitree/f3e1b9c8bfa9da8be335bb36b198989769e59102/bitreemainwindow.cpp -------------------------------------------------------------------------------- /bitreemainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef BitreeMainWindow_H 2 | #define BitreeMainWindow_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "createwindow.h" 16 | #include "bitreetravelwindow.h" 17 | #include "bitreethreadwindow.h" 18 | #include "paint.h" 19 | 20 | 21 | namespace Ui 22 | { 23 | class BitreeMainWindow; 24 | } 25 | 26 | 27 | class BitreeMainWindow : public QMainWindow 28 | { 29 | Q_OBJECT 30 | 31 | public: 32 | explicit BitreeMainWindow(QWidget *parent = nullptr); 33 | ~BitreeMainWindow(); 34 | 35 | friend class CreateWindow; 36 | 37 | private slots: 38 | void createWindowButton_clicked(); 39 | void biTreeLeafCountButton_clicked(); 40 | void bitreeTravelButton_clicked(); 41 | void biTreeTreadButton_clicked(); 42 | void bitreeDisplayButton_clicked(); 43 | void actionHelpButton_clicked(); 44 | 45 | private: 46 | Ui::BitreeMainWindow *ui; 47 | 48 | QMenu *menuBarHelp; 49 | QAction *actionHelpOpen; 50 | 51 | protected: 52 | QString biTreeString,*preString,*inString,*postString; 53 | BiTreeP bt; 54 | BiTreeP tbt; 55 | }; 56 | 57 | 58 | #endif // BitreeMainWindow_H 59 | -------------------------------------------------------------------------------- /bitreethreadwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "bitreethreadwindow.h" 2 | #include "ui_bitreethreadwindow.h" 3 | 4 | BiTreeThreadWindow::BiTreeThreadWindow(QWidget *parent) : 5 | QDialog(parent), 6 | ui(new Ui::BiTreeThreadWindow) 7 | { 8 | ui->setupUi(this); 9 | connect(ui->threadWindowButton_OK,SIGNAL(clicked()),this,SLOT(biTreeThreadWindowButtonOK_click())); 10 | } 11 | 12 | BiTreeThreadWindow::~BiTreeThreadWindow() 13 | { 14 | delete ui; 15 | } 16 | 17 | void BiTreeThreadWindow::biTreeThreadWindowButtonOK_click() 18 | { 19 | if(ui->preOrderThreadRadioButton->isChecked()) 20 | type=1; 21 | else 22 | { 23 | if(ui->inOrderThreadRadioButton) 24 | type=2; 25 | else 26 | { 27 | type=3; 28 | } 29 | } 30 | QMessageBox *biTreeThreadSuccessMessageBox; 31 | biTreeThreadSuccessMessageBox=new QMessageBox("Success","Thread success",QMessageBox::Information,QMessageBox::Ok,0,0); 32 | biTreeThreadSuccessMessageBox->exec(); 33 | close(); 34 | } 35 | 36 | int BiTreeThreadWindow::getType() 37 | { 38 | return type; 39 | } 40 | -------------------------------------------------------------------------------- /bitreethreadwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef BITREETHREADWINDOW_H 2 | #define BITREETHREADWINDOW_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | namespace Ui { 9 | class BiTreeThreadWindow; 10 | } 11 | 12 | class BiTreeThreadWindow : public QDialog 13 | { 14 | Q_OBJECT 15 | 16 | public: 17 | explicit BiTreeThreadWindow(QWidget *parent = nullptr); 18 | ~BiTreeThreadWindow(); 19 | int getType(); 20 | private slots: 21 | void biTreeThreadWindowButtonOK_click(); 22 | private: 23 | Ui::BiTreeThreadWindow *ui; 24 | int type; 25 | 26 | }; 27 | 28 | #endif // BITREETHREADWINDOW_H 29 | -------------------------------------------------------------------------------- /bitreetravelwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "bitreetravelwindow.h" 2 | #include "ui_bitreetravelwindow.h" 3 | 4 | biTreeTravelWindow::biTreeTravelWindow(QWidget *parent) : 5 | QDialog(parent), 6 | ui(new Ui::biTreeTravelWindow) 7 | { 8 | ui->setupUi(this); 9 | connect(ui->preTravelButton,SIGNAL(clicked()),this,SLOT(preTravelButton_clicked())); 10 | connect(ui->inTravelButton,SIGNAL(clicked()),this,SLOT(inTravelButton_clicked())); 11 | connect(ui->postTravelButton,SIGNAL(clicked()),this,SLOT(postTravelButton_clicked())); 12 | } 13 | 14 | biTreeTravelWindow::~biTreeTravelWindow() 15 | { 16 | delete ui; 17 | } 18 | 19 | 20 | void biTreeTravelWindow::preTravelButton_clicked() 21 | { 22 | QMessageBox *preTravelButtonMessageBox; 23 | preTravelButtonMessageBox=new QMessageBox("PreTravelButton",preString,QMessageBox::Information,QMessageBox::Ok,0,0); 24 | preTravelButtonMessageBox->exec(); 25 | } 26 | 27 | void biTreeTravelWindow::inTravelButton_clicked() 28 | { 29 | QMessageBox *inTravelButtonMessageBox; 30 | inTravelButtonMessageBox=new QMessageBox("InTravelButton",inString,QMessageBox::Information,QMessageBox::Ok,0,0); 31 | inTravelButtonMessageBox->exec(); 32 | } 33 | 34 | void biTreeTravelWindow::postTravelButton_clicked() 35 | { 36 | QMessageBox *postTravelButtonMessageBox; 37 | postTravelButtonMessageBox=new QMessageBox("PostTravelButton",postString,QMessageBox::Information,QMessageBox::Ok,0,0); 38 | postTravelButtonMessageBox->exec(); 39 | } 40 | 41 | void biTreeTravelWindow::getBiTreeString(QString pre,QString in,QString post) 42 | { 43 | preString=pre; 44 | inString=in; 45 | postString=post; 46 | return; 47 | } 48 | -------------------------------------------------------------------------------- /bitreetravelwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef BITREETRAVELWINDOW_H 2 | #define BITREETRAVELWINDOW_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | namespace Ui { 9 | class biTreeTravelWindow; 10 | } 11 | 12 | class biTreeTravelWindow : public QDialog 13 | { 14 | Q_OBJECT 15 | 16 | public: 17 | explicit biTreeTravelWindow(QWidget *parent = nullptr); 18 | ~biTreeTravelWindow(); 19 | void getBiTreeString(QString pre,QString in,QString post); 20 | 21 | private slots: 22 | void preTravelButton_clicked(); 23 | void inTravelButton_clicked(); 24 | void postTravelButton_clicked(); 25 | 26 | private: 27 | Ui::biTreeTravelWindow *ui; 28 | QString preString,inString,postString; 29 | }; 30 | 31 | #endif // BITREETRAVELWINDOW_H 32 | -------------------------------------------------------------------------------- /createwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "createwindow.h" 2 | #include "ui_createwindow.h" 3 | #include 4 | 5 | #include 6 | 7 | CreateWindow::CreateWindow(QWidget *parent) : 8 | QDialog(parent), 9 | ui(new Ui::CreateWindow) 10 | { 11 | ui->setupUi(this); 12 | connect(ui->createBitreeWindowButton_OK,SIGNAL(clicked()),this,SLOT(createWindow_OKButton_clicked())); 13 | connect(ui->createBitreeWindowButton_Cancel,SIGNAL(clicked()),this,SLOT(createWindow_CancelButton_clicked())); 14 | } 15 | 16 | CreateWindow::~CreateWindow() 17 | { 18 | delete ui; 19 | } 20 | 21 | void CreateWindow::createWindow_OKButton_clicked() 22 | { 23 | createWindowTextEditString=ui->createWindowTextEdit->toPlainText(); 24 | QMessageBox *biTreeCreateSuccessMessageBox; 25 | biTreeCreateSuccessMessageBox=new QMessageBox("Success","Create success",QMessageBox::Information,QMessageBox::Ok,0,0); 26 | biTreeCreateSuccessMessageBox->exec(); 27 | close(); 28 | } 29 | 30 | 31 | void CreateWindow::createWindow_CancelButton_clicked() 32 | { 33 | this->close(); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /createwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef CREATEWINDOW_H 2 | #define CREATEWINDOW_H 3 | 4 | #include 5 | #include 6 | 7 | namespace Ui { 8 | class CreateWindow; 9 | } 10 | 11 | class CreateWindow : public QDialog 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | explicit CreateWindow(QWidget *parent = nullptr); 17 | ~CreateWindow(); 18 | 19 | friend class BitreeMainWindow; 20 | 21 | private slots: 22 | void createWindow_OKButton_clicked(); 23 | void createWindow_CancelButton_clicked(); 24 | 25 | private: 26 | Ui::CreateWindow *ui; 27 | 28 | protected: 29 | QString createWindowTextEditString; 30 | }; 31 | 32 | #endif // CREATEWINDOW_H 33 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "bitreemainwindow.h" 2 | #include 3 | 4 | #include 5 | 6 | int main(int argc, char *argv[]) 7 | { 8 | QApplication a(argc, argv); 9 | BitreeMainWindow w; 10 | w.show(); 11 | 12 | return a.exec(); 13 | } 14 | -------------------------------------------------------------------------------- /paint.cpp: -------------------------------------------------------------------------------- 1 | #include"paint.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | Paint::Paint(QWidget *parent) : QWidget(parent) 11 | { 12 | resize(600, 400); 13 | } 14 | 15 | void Paint::setBiTree(BiTreeP root) 16 | { 17 | beTree=root; 18 | return; 19 | } 20 | 21 | /*void Paint::draw(BiTreeP node, int x, int y, int angle, bool isLeft, int depth, QPainter *p) 22 | { 23 | int leftAngle, rightAngle; 24 | int dx,dy,nx,ny; 25 | if (node==nullptr) 26 | return; 27 | p->drawText(x,y,QChar(node->data)); 28 | if (node->lchild!=nullptr) 29 | { 30 | if (depth<2) 31 | { 32 | leftAngle = angle + rand()%15; 33 | } else 34 | { 35 | if (!isLeft) { 36 | leftAngle = angle + rand()%5 + 10; 37 | } else { 38 | leftAngle = rand()%45; 39 | } 40 | } 41 | int lenEdge = rootLengt-depth*35; 42 | dx = int(-cos(leftAngle*PI/180)*lenEdge); 43 | dy = int(sin(leftAngle*PI/180)*lenEdge); 44 | nx = x+dx; 45 | ny = y+dy; 46 | p->drawLine(x,y,nx,ny); 47 | draw(node->lchild,nx,ny,leftAngle,true,depth+1,p); 48 | } 49 | if (node->rchild!=nullptr) 50 | { 51 | if (depth<2) 52 | { 53 | rightAngle = angle + rand()%15; 54 | } else 55 | { 56 | if (isLeft) 57 | { 58 | rightAngle = angle + rand()%5 + 10; 59 | } 60 | else 61 | { 62 | rightAngle = rand()%45; 63 | } 64 | } 65 | int lenEdge = rootLengt-depth*15; 66 | dx = int(cos(rightAngle*PI/180)*lenEdge); 67 | dy = int(sin(rightAngle*PI/180)*lenEdge); 68 | nx = x+dx; 69 | ny = y+dy; 70 | p->drawLine(x,y,nx,ny); 71 | draw(node->rchild,nx,ny,rightAngle,false,depth+1,p); 72 | 73 | } 74 | if (node->lchild==nullptr && node->rchild==nullptr) {return ; } 75 | 76 | }*/ 77 | 78 | 79 | 80 | 81 | 82 | void Paint::paintEvent(QPaintEvent *) 83 | { 84 | QPainter painter(this); 85 | BiTreeP bt=beTree; 86 | struct stackNode 87 | { 88 | BiTNode * treeNode; 89 | int layer; // 标记该节点属于第几层 90 | }; 91 | // 反锯齿 92 | painter.setRenderHint(QPainter::Antialiasing); 93 | painter.setRenderHint(QPainter::TextAntialiasing); 94 | 95 | 96 | //设置字体 97 | QFont font; 98 | font.setPointSize(12); 99 | font.setBold(true); 100 | painter.setFont(font); 101 | 102 | //设置画笔 103 | QPen penLine; 104 | penLine.setWidth(2); //线宽 105 | penLine.setColor(Qt::blue); //划线颜色 106 | penLine.setStyle(Qt::SolidLine);//线的类型,实线、虚线等 107 | penLine.setCapStyle(Qt::FlatCap);//线端点样式 108 | penLine.setJoinStyle(Qt::BevelJoin);//线的连接点样式 109 | painter.setPen(penLine); 110 | 111 | qreal W = this->width(); // 画布的宽 112 | qreal H = this->height(); // 画布的高 113 | int treeHeight = bt->getHeight(); // 树的高度 114 | qreal R = W / (2 * std::pow(2, treeHeight) + 2); // 节点的半径 115 | 116 | const int layerHeight = int((H-4*R) / (treeHeight-1)); // 层高,即垂直偏移量 117 | 118 | // 初始化 119 | // 节点的定义 120 | QRectF node(QPointF(-R, -R), QPointF(R, R)); 121 | std::stack stack; // 存放右孩子节点 122 | stackNode qNode; 123 | 124 | std::stack points; // 存放右孩子节点相对于当前坐标系统原点的位置 125 | QPointF point; 126 | 127 | qreal Hoffset; // 水平偏移量 128 | const qreal Pi = 3.14159; 129 | int curLayer = 1; 130 | int curAngle; // 当前角度 131 | qreal deg; // 当前弧度 132 | 133 | // 将坐标系统的原点(下文简称原点)移动到初始位置 134 | painter.translate(W/2, 2*R); 135 | 136 | while (1) 137 | { 138 | painter.drawEllipse(node); 139 | painter.drawText(node, Qt::AlignCenter, QString(bt->data)); 140 | 141 | // 设置孩子节点相对于父节点的水平偏移量 142 | Hoffset = std::pow(2, (treeHeight - curLayer)) * R; 143 | deg = std::atan(Hoffset / layerHeight); // 返回的是弧度 144 | curAngle = int(180 / Pi * deg); // 将弧度转化为角度 145 | 146 | if (bt->rchild != nullptr) 147 | { 148 | // 坐标轴逆时针旋转 149 | painter.rotate(-curAngle); 150 | 151 | //绘制图形路径 152 | painter.drawLine(0, int(R), 0, int(layerHeight / std::cos(deg) - R)); 153 | 154 | // 旋转复原 155 | painter.rotate(curAngle); 156 | 157 | // 右孩子节点压栈 158 | qNode.treeNode = bt->rchild; 159 | qNode.layer = curLayer + 1; 160 | stack.push(qNode); 161 | 162 | // 右孩子相对于当前坐标系统原点的位置压栈 163 | points.push(QPointF(QPointF(0, 0) + QPointF(Hoffset, layerHeight))); 164 | painter.save(); 165 | } 166 | if (bt->lchild != nullptr) 167 | { 168 | // 坐标轴顺时针旋转 169 | painter.rotate(curAngle); 170 | // 绘制边 171 | painter.drawLine(0, int(R), 0, int(layerHeight / std::cos(deg) - R)); 172 | // 旋转复原 173 | painter.rotate(-curAngle); 174 | // 原点移动到左孩子节点的位置 175 | painter.translate(QPointF(QPointF(0, 0) + QPointF(-Hoffset, layerHeight))); 176 | bt = bt->lchild; 177 | // 层次加1 178 | curLayer++; 179 | } 180 | else { 181 | if(stack.empty()) 182 | { 183 | painter.resetTransform(); 184 | return; 185 | } 186 | qNode=stack.top(); 187 | stack.pop(); 188 | bt = qNode.treeNode; 189 | curLayer = qNode.layer; 190 | 191 | painter.restore(); 192 | point=points.top(); 193 | points.pop(); 194 | painter.translate(point); 195 | } 196 | 197 | } 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | /* 207 | QPainter p(this); 208 | 209 | p.setRenderHint(QPainter::Antialiasing); 210 | p.setRenderHint(QPainter::TextAntialiasing); 211 | 212 | QFont font; 213 | font.setPointSize(12); 214 | font.setBold(true); 215 | p.setFont(font); 216 | 217 | 218 | draw(bt, width()/2, height()/2, 10, true, 0, &p);*/ 219 | } 220 | -------------------------------------------------------------------------------- /paint.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "bitree.h" 3 | 4 | 5 | class Paint : public QWidget 6 | { 7 | Q_OBJECT 8 | 9 | public: 10 | explicit Paint(QWidget *parent = nullptr); 11 | void setBiTree(BiTreeP root); 12 | protected: 13 | void paintEvent(QPaintEvent *); 14 | //void draw(BiTreeP node, int x, int y, int angle, bool isLeft, int depth, QPainter *p); 15 | private: 16 | BiTreeP beTree; 17 | const int rootLengt=160; 18 | const double PI=3.1415926; 19 | }; 20 | -------------------------------------------------------------------------------- /ui_bitreemainwindow.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | ** Form generated from reading UI file 'bitreemainwindow.ui' 3 | ** 4 | ** Created by: Qt User Interface Compiler version 5.9.8 5 | ** 6 | ** WARNING! All changes made in this file will be lost when recompiling UI file! 7 | ********************************************************************************/ 8 | 9 | #ifndef UI_BITREEMAINWINDOW_H 10 | #define UI_BITREEMAINWINDOW_H 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | QT_BEGIN_NAMESPACE 26 | 27 | class Ui_BitreeMainWindow 28 | { 29 | public: 30 | QWidget *centralWidget; 31 | QVBoxLayout *verticalLayout; 32 | QPushButton *createBitreeButton; 33 | QPushButton *bitreeLeafCountButton; 34 | QPushButton *bitreeThreadButton; 35 | QPushButton *bitreeTravelButton; 36 | QPushButton *bitreeDisplayButton; 37 | QMenuBar *menuBar; 38 | QToolBar *mainToolBar; 39 | QStatusBar *statusBar; 40 | 41 | void setupUi(QMainWindow *BitreeMainWindow) 42 | { 43 | if (BitreeMainWindow->objectName().isEmpty()) 44 | BitreeMainWindow->setObjectName(QStringLiteral("BitreeMainWindow")); 45 | BitreeMainWindow->resize(352, 302); 46 | centralWidget = new QWidget(BitreeMainWindow); 47 | centralWidget->setObjectName(QStringLiteral("centralWidget")); 48 | verticalLayout = new QVBoxLayout(centralWidget); 49 | verticalLayout->setSpacing(6); 50 | verticalLayout->setContentsMargins(11, 11, 11, 11); 51 | verticalLayout->setObjectName(QStringLiteral("verticalLayout")); 52 | createBitreeButton = new QPushButton(centralWidget); 53 | createBitreeButton->setObjectName(QStringLiteral("createBitreeButton")); 54 | 55 | verticalLayout->addWidget(createBitreeButton); 56 | 57 | bitreeLeafCountButton = new QPushButton(centralWidget); 58 | bitreeLeafCountButton->setObjectName(QStringLiteral("bitreeLeafCountButton")); 59 | 60 | verticalLayout->addWidget(bitreeLeafCountButton); 61 | 62 | bitreeThreadButton = new QPushButton(centralWidget); 63 | bitreeThreadButton->setObjectName(QStringLiteral("bitreeThreadButton")); 64 | 65 | verticalLayout->addWidget(bitreeThreadButton); 66 | 67 | bitreeTravelButton = new QPushButton(centralWidget); 68 | bitreeTravelButton->setObjectName(QStringLiteral("bitreeTravelButton")); 69 | 70 | verticalLayout->addWidget(bitreeTravelButton); 71 | 72 | bitreeDisplayButton = new QPushButton(centralWidget); 73 | bitreeDisplayButton->setObjectName(QStringLiteral("bitreeDisplayButton")); 74 | 75 | verticalLayout->addWidget(bitreeDisplayButton); 76 | 77 | BitreeMainWindow->setCentralWidget(centralWidget); 78 | menuBar = new QMenuBar(BitreeMainWindow); 79 | menuBar->setObjectName(QStringLiteral("menuBar")); 80 | menuBar->setGeometry(QRect(0, 0, 352, 26)); 81 | BitreeMainWindow->setMenuBar(menuBar); 82 | mainToolBar = new QToolBar(BitreeMainWindow); 83 | mainToolBar->setObjectName(QStringLiteral("mainToolBar")); 84 | BitreeMainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar); 85 | statusBar = new QStatusBar(BitreeMainWindow); 86 | statusBar->setObjectName(QStringLiteral("statusBar")); 87 | BitreeMainWindow->setStatusBar(statusBar); 88 | 89 | retranslateUi(BitreeMainWindow); 90 | 91 | QMetaObject::connectSlotsByName(BitreeMainWindow); 92 | } // setupUi 93 | 94 | void retranslateUi(QMainWindow *BitreeMainWindow) 95 | { 96 | BitreeMainWindow->setWindowTitle(QApplication::translate("BitreeMainWindow", "BitreeMainWindow", Q_NULLPTR)); 97 | createBitreeButton->setText(QApplication::translate("BitreeMainWindow", "CreateBitree", Q_NULLPTR)); 98 | bitreeLeafCountButton->setText(QApplication::translate("BitreeMainWindow", "BitreeLeafCount", Q_NULLPTR)); 99 | bitreeThreadButton->setText(QApplication::translate("BitreeMainWindow", "BitreeThread", Q_NULLPTR)); 100 | bitreeTravelButton->setText(QApplication::translate("BitreeMainWindow", "BitreeTravel", Q_NULLPTR)); 101 | bitreeDisplayButton->setText(QApplication::translate("BitreeMainWindow", "BitreeDisplay", Q_NULLPTR)); 102 | } // retranslateUi 103 | 104 | }; 105 | 106 | namespace Ui { 107 | class BitreeMainWindow: public Ui_BitreeMainWindow {}; 108 | } // namespace Ui 109 | 110 | QT_END_NAMESPACE 111 | 112 | #endif // UI_BITREEMAINWINDOW_H 113 | -------------------------------------------------------------------------------- /ui_bitreepaint.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | ** Form generated from reading UI file 'bitreepaint.ui' 3 | ** 4 | ** Created by: Qt User Interface Compiler version 5.9.8 5 | ** 6 | ** WARNING! All changes made in this file will be lost when recompiling UI file! 7 | ********************************************************************************/ 8 | 9 | #ifndef UI_BITREEPAINT_H 10 | #define UI_BITREEPAINT_H 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | QT_BEGIN_NAMESPACE 20 | 21 | class Ui_BiTreePaint 22 | { 23 | public: 24 | 25 | void setupUi(QWidget *BiTreePaint) 26 | { 27 | if (BiTreePaint->objectName().isEmpty()) 28 | BiTreePaint->setObjectName(QStringLiteral("BiTreePaint")); 29 | BiTreePaint->resize(400, 300); 30 | 31 | retranslateUi(BiTreePaint); 32 | 33 | QMetaObject::connectSlotsByName(BiTreePaint); 34 | } // setupUi 35 | 36 | void retranslateUi(QWidget *BiTreePaint) 37 | { 38 | BiTreePaint->setWindowTitle(QApplication::translate("BiTreePaint", "Form", Q_NULLPTR)); 39 | } // retranslateUi 40 | 41 | }; 42 | 43 | namespace Ui { 44 | class BiTreePaint: public Ui_BiTreePaint {}; 45 | } // namespace Ui 46 | 47 | QT_END_NAMESPACE 48 | 49 | #endif // UI_BITREEPAINT_H 50 | -------------------------------------------------------------------------------- /ui_bitreethreadwindow.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | ** Form generated from reading UI file 'bitreethreadwindow.ui' 3 | ** 4 | ** Created by: Qt User Interface Compiler version 5.9.8 5 | ** 6 | ** WARNING! All changes made in this file will be lost when recompiling UI file! 7 | ********************************************************************************/ 8 | 9 | #ifndef UI_BITREETHREADWINDOW_H 10 | #define UI_BITREETHREADWINDOW_H 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | QT_BEGIN_NAMESPACE 25 | 26 | class Ui_BiTreeThreadWindow 27 | { 28 | public: 29 | QWidget *widget; 30 | QVBoxLayout *verticalLayout_3; 31 | QVBoxLayout *verticalLayout_2; 32 | QGroupBox *threadGroupBox; 33 | QVBoxLayout *verticalLayout; 34 | QRadioButton *preOrderThreadRadioButton; 35 | QRadioButton *inOrderThreadRadioButton; 36 | QRadioButton *postOrderThreadRadioButton; 37 | QPushButton *threadWindowButton_OK; 38 | 39 | void setupUi(QDialog *BiTreeThreadWindow) 40 | { 41 | if (BiTreeThreadWindow->objectName().isEmpty()) 42 | BiTreeThreadWindow->setObjectName(QStringLiteral("BiTreeThreadWindow")); 43 | BiTreeThreadWindow->resize(400, 300); 44 | widget = new QWidget(BiTreeThreadWindow); 45 | widget->setObjectName(QStringLiteral("widget")); 46 | widget->setGeometry(QRect(70, 50, 251, 211)); 47 | verticalLayout_3 = new QVBoxLayout(widget); 48 | verticalLayout_3->setObjectName(QStringLiteral("verticalLayout_3")); 49 | verticalLayout_3->setContentsMargins(0, 0, 0, 0); 50 | verticalLayout_2 = new QVBoxLayout(); 51 | verticalLayout_2->setObjectName(QStringLiteral("verticalLayout_2")); 52 | threadGroupBox = new QGroupBox(widget); 53 | threadGroupBox->setObjectName(QStringLiteral("threadGroupBox")); 54 | verticalLayout = new QVBoxLayout(threadGroupBox); 55 | verticalLayout->setObjectName(QStringLiteral("verticalLayout")); 56 | preOrderThreadRadioButton = new QRadioButton(threadGroupBox); 57 | preOrderThreadRadioButton->setObjectName(QStringLiteral("preOrderThreadRadioButton")); 58 | 59 | verticalLayout->addWidget(preOrderThreadRadioButton); 60 | 61 | inOrderThreadRadioButton = new QRadioButton(threadGroupBox); 62 | inOrderThreadRadioButton->setObjectName(QStringLiteral("inOrderThreadRadioButton")); 63 | 64 | verticalLayout->addWidget(inOrderThreadRadioButton); 65 | 66 | postOrderThreadRadioButton = new QRadioButton(threadGroupBox); 67 | postOrderThreadRadioButton->setObjectName(QStringLiteral("postOrderThreadRadioButton")); 68 | 69 | verticalLayout->addWidget(postOrderThreadRadioButton); 70 | 71 | 72 | verticalLayout_2->addWidget(threadGroupBox); 73 | 74 | 75 | verticalLayout_3->addLayout(verticalLayout_2); 76 | 77 | threadWindowButton_OK = new QPushButton(widget); 78 | threadWindowButton_OK->setObjectName(QStringLiteral("threadWindowButton_OK")); 79 | 80 | verticalLayout_3->addWidget(threadWindowButton_OK); 81 | 82 | 83 | retranslateUi(BiTreeThreadWindow); 84 | 85 | QMetaObject::connectSlotsByName(BiTreeThreadWindow); 86 | } // setupUi 87 | 88 | void retranslateUi(QDialog *BiTreeThreadWindow) 89 | { 90 | BiTreeThreadWindow->setWindowTitle(QApplication::translate("BiTreeThreadWindow", "BiTreeThread", Q_NULLPTR)); 91 | threadGroupBox->setTitle(QApplication::translate("BiTreeThreadWindow", "Thread", Q_NULLPTR)); 92 | preOrderThreadRadioButton->setText(QApplication::translate("BiTreeThreadWindow", "PreOrderThread", Q_NULLPTR)); 93 | inOrderThreadRadioButton->setText(QApplication::translate("BiTreeThreadWindow", "InOrderThread", Q_NULLPTR)); 94 | postOrderThreadRadioButton->setText(QApplication::translate("BiTreeThreadWindow", "PostOrderThread", Q_NULLPTR)); 95 | threadWindowButton_OK->setText(QApplication::translate("BiTreeThreadWindow", "OK", Q_NULLPTR)); 96 | } // retranslateUi 97 | 98 | }; 99 | 100 | namespace Ui { 101 | class BiTreeThreadWindow: public Ui_BiTreeThreadWindow {}; 102 | } // namespace Ui 103 | 104 | QT_END_NAMESPACE 105 | 106 | #endif // UI_BITREETHREADWINDOW_H 107 | -------------------------------------------------------------------------------- /ui_bitreetravelwindow.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | ** Form generated from reading UI file 'bitreetravelwindow.ui' 3 | ** 4 | ** Created by: Qt User Interface Compiler version 5.9.8 5 | ** 6 | ** WARNING! All changes made in this file will be lost when recompiling UI file! 7 | ********************************************************************************/ 8 | 9 | #ifndef UI_BITREETRAVELWINDOW_H 10 | #define UI_BITREETRAVELWINDOW_H 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | QT_BEGIN_NAMESPACE 23 | 24 | class Ui_biTreeTravelWindow 25 | { 26 | public: 27 | QWidget *widget; 28 | QVBoxLayout *verticalLayout; 29 | QPushButton *preTravelButton; 30 | QPushButton *inTravelButton; 31 | QPushButton *postTravelButton; 32 | 33 | void setupUi(QDialog *biTreeTravelWindow) 34 | { 35 | if (biTreeTravelWindow->objectName().isEmpty()) 36 | biTreeTravelWindow->setObjectName(QStringLiteral("biTreeTravelWindow")); 37 | biTreeTravelWindow->resize(400, 300); 38 | widget = new QWidget(biTreeTravelWindow); 39 | widget->setObjectName(QStringLiteral("widget")); 40 | widget->setGeometry(QRect(110, 40, 171, 171)); 41 | verticalLayout = new QVBoxLayout(widget); 42 | verticalLayout->setObjectName(QStringLiteral("verticalLayout")); 43 | verticalLayout->setContentsMargins(0, 0, 0, 0); 44 | preTravelButton = new QPushButton(widget); 45 | preTravelButton->setObjectName(QStringLiteral("preTravelButton")); 46 | 47 | verticalLayout->addWidget(preTravelButton); 48 | 49 | inTravelButton = new QPushButton(widget); 50 | inTravelButton->setObjectName(QStringLiteral("inTravelButton")); 51 | 52 | verticalLayout->addWidget(inTravelButton); 53 | 54 | postTravelButton = new QPushButton(widget); 55 | postTravelButton->setObjectName(QStringLiteral("postTravelButton")); 56 | 57 | verticalLayout->addWidget(postTravelButton); 58 | 59 | 60 | retranslateUi(biTreeTravelWindow); 61 | 62 | QMetaObject::connectSlotsByName(biTreeTravelWindow); 63 | } // setupUi 64 | 65 | void retranslateUi(QDialog *biTreeTravelWindow) 66 | { 67 | biTreeTravelWindow->setWindowTitle(QApplication::translate("biTreeTravelWindow", "BiTreeTravel", Q_NULLPTR)); 68 | preTravelButton->setText(QApplication::translate("biTreeTravelWindow", "PreTravel", Q_NULLPTR)); 69 | inTravelButton->setText(QApplication::translate("biTreeTravelWindow", "InTravel", Q_NULLPTR)); 70 | postTravelButton->setText(QApplication::translate("biTreeTravelWindow", "PostTravel", Q_NULLPTR)); 71 | } // retranslateUi 72 | 73 | }; 74 | 75 | namespace Ui { 76 | class biTreeTravelWindow: public Ui_biTreeTravelWindow {}; 77 | } // namespace Ui 78 | 79 | QT_END_NAMESPACE 80 | 81 | #endif // UI_BITREETRAVELWINDOW_H 82 | -------------------------------------------------------------------------------- /ui_createwindow.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | ** Form generated from reading UI file 'createwindow.ui' 3 | ** 4 | ** Created by: Qt User Interface Compiler version 5.9.8 5 | ** 6 | ** WARNING! All changes made in this file will be lost when recompiling UI file! 7 | ********************************************************************************/ 8 | 9 | #ifndef UI_CREATEWINDOW_H 10 | #define UI_CREATEWINDOW_H 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | QT_BEGIN_NAMESPACE 26 | 27 | class Ui_CreateWindow 28 | { 29 | public: 30 | QWidget *layoutWidget; 31 | QVBoxLayout *verticalLayout; 32 | QLabel *createWindowText; 33 | QTextEdit *createWindowTextEdit; 34 | QHBoxLayout *horizontalLayout; 35 | QPushButton *createBitreeWindowButton_OK; 36 | QPushButton *createBitreeWindowButton_Cancel; 37 | 38 | void setupUi(QDialog *CreateWindow) 39 | { 40 | if (CreateWindow->objectName().isEmpty()) 41 | CreateWindow->setObjectName(QStringLiteral("CreateWindow")); 42 | CreateWindow->resize(400, 300); 43 | layoutWidget = new QWidget(CreateWindow); 44 | layoutWidget->setObjectName(QStringLiteral("layoutWidget")); 45 | layoutWidget->setGeometry(QRect(40, 10, 310, 253)); 46 | verticalLayout = new QVBoxLayout(layoutWidget); 47 | verticalLayout->setObjectName(QStringLiteral("verticalLayout")); 48 | verticalLayout->setContentsMargins(0, 0, 0, 0); 49 | createWindowText = new QLabel(layoutWidget); 50 | createWindowText->setObjectName(QStringLiteral("createWindowText")); 51 | 52 | verticalLayout->addWidget(createWindowText); 53 | 54 | createWindowTextEdit = new QTextEdit(layoutWidget); 55 | createWindowTextEdit->setObjectName(QStringLiteral("createWindowTextEdit")); 56 | 57 | verticalLayout->addWidget(createWindowTextEdit); 58 | 59 | horizontalLayout = new QHBoxLayout(); 60 | horizontalLayout->setObjectName(QStringLiteral("horizontalLayout")); 61 | createBitreeWindowButton_OK = new QPushButton(layoutWidget); 62 | createBitreeWindowButton_OK->setObjectName(QStringLiteral("createBitreeWindowButton_OK")); 63 | 64 | horizontalLayout->addWidget(createBitreeWindowButton_OK); 65 | 66 | createBitreeWindowButton_Cancel = new QPushButton(layoutWidget); 67 | createBitreeWindowButton_Cancel->setObjectName(QStringLiteral("createBitreeWindowButton_Cancel")); 68 | 69 | horizontalLayout->addWidget(createBitreeWindowButton_Cancel); 70 | 71 | 72 | verticalLayout->addLayout(horizontalLayout); 73 | 74 | 75 | retranslateUi(CreateWindow); 76 | 77 | QMetaObject::connectSlotsByName(CreateWindow); 78 | } // setupUi 79 | 80 | void retranslateUi(QDialog *CreateWindow) 81 | { 82 | CreateWindow->setWindowTitle(QApplication::translate("CreateWindow", "CreateBiTree", Q_NULLPTR)); 83 | createWindowText->setText(QApplication::translate("CreateWindow", "\350\257\267\350\276\223\345\205\245\344\272\214\345\217\211\346\240\221\345\205\210\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\357\274\214\346\227\240\345\255\220\350\212\202\347\202\271\347\224\250#\350\241\250\347\244\272", Q_NULLPTR)); 84 | createBitreeWindowButton_OK->setText(QApplication::translate("CreateWindow", "OK", Q_NULLPTR)); 85 | createBitreeWindowButton_Cancel->setText(QApplication::translate("CreateWindow", "Cancel", Q_NULLPTR)); 86 | } // retranslateUi 87 | 88 | }; 89 | 90 | namespace Ui { 91 | class CreateWindow: public Ui_CreateWindow {}; 92 | } // namespace Ui 93 | 94 | QT_END_NAMESPACE 95 | 96 | #endif // UI_CREATEWINDOW_H 97 | --------------------------------------------------------------------------------