Я создаю программу на C++ для преобразования входного выражения в постфиксное выражение, а затем визуализирую его в виде двоичного дерева (вы можете использовать клавишу со стрелкой для перемещения графика и нажать ПРОБЕЛ, чтобы вернуться в окно меню). Я также создаю простое меню, позволяющее пользователю выбирать, использовать ли доступный файл выражения или ввести новое выражение самостоятельно.
Проблема в том, что после того, как я выбираю 1 вариант, программа выполняет и визуализирует выражение нормально. Но если после этого я вернусь в меню и выберу визуализацию другого выражения, формат двоичного дерева будет не таким, как я ожидал (мелкий шрифт, отсутствует линия между узлами и отсутствует граница круга каждого узла)
Я создаю программу на C++ для преобразования входного выражения в постфиксное выражение, а затем визуализирую его в виде двоичного дерева (вы можете использовать клавишу со стрелкой для перемещения графика и нажать ПРОБЕЛ, чтобы вернуться в окно меню). Я также создаю простое меню, позволяющее пользователю выбирать, использовать ли доступный файл выражения или ввести новое выражение самостоятельно. Проблема в том, что после того, как я выбираю 1 вариант, программа выполняет и визуализирует выражение нормально. Но если после этого я вернусь в меню и выберу визуализацию другого выражения, формат двоичного дерева будет не таким, как я ожидал (мелкий шрифт, отсутствует линия между узлами и отсутствует граница круга каждого узла)[code]#include #include #include #include #include #include #include #include #include
#include
#define E exp(1) #define Pi M_PI using namespace std;
int heightOfTree(tNode *root){ if(root==NULL) return 0; int height = 1; if(root->pLeft==NULL and root->pRight==NULL) return 1; return 1 + max(heightOfTree(root->pLeft), heightOfTree(root->pRight)); }
//Read tree follow the order Left node - Middle Node - Right Node void LNR(tNode *root){ if(root!=NULL){ if(root->pLeft != NULL) LNR(root->pLeft); if(root!=NULL){ cout data pRight != NULL) LNR(root->pRight); } }
int childCount(struct tNode* root){ int x=0,y=0; if(root->pLeft == NULL && root->pRight == NULL) { return 1; }else {
if(root->pRight !=NULL && root->pLeft==NULL){ x = 1 + childCount(root->pRight); }
if(root->pLeft != NULL){ x = 1+ childCount(root->pLeft); } if(root->pRight != NULL){ y = 1+ childCount(root->pRight); }
if(x >= y){ return x; } else{ return y; } } }
/*Code fixed*/ //check neu la ve node sau thang xuong thi check = 0, con binh thuong check = 1 //check if following node is drawn straight down, check = 0, else check = 1
void drawTree(struct tNode* root, int x, int y, int xOffset, int level, int check) {
if (root != NULL) { // Draw left subtree int xOffset_1 = xOffset*check; if(root -> pRight != NULL && root -> pLeft == NULL){ // Draw when node has only 1 child node // delay(1000); line(x,y,x,y+80);
drawTree(root->pRight, x, y+80, xOffset, level+1, 0); // if xOffset = 0 the following childNode will be place above each other
}else{ if (root->pLeft != NULL) { int cntLeft = childCount(root->pLeft);
if (level == 0 ){ // delay(1000); line(x, y, x - xOffset*cntLeft, y + 80); drawTree(root->pLeft, x - xOffset*cntLeft, y + 80, xOffset, level + 1, 1 ); } else { // delay(1000); line(x, y, x - xOffset*cntLeft, y + 80); drawTree(root->pLeft, x - xOffset*cntLeft, y + 80, xOffset, level + 1, 1);
} } // Draw right subtree if (root->pRight != NULL) { int cntRight = childCount(root->pRight);
if (level == 0 ){ // delay(1000); line(x, y, x + xOffset*cntRight, y + 80); drawTree(root->pRight, x + xOffset*cntRight, y + 80, xOffset , level + 1, 1);
} else{ // delay(1000); line(x, y, x + xOffset*(cntRight), y + 80);
drawTree(root->pRight, x + xOffset*(cntRight), y + 80 , xOffset , level + 1 ,1);
}
}
}
// Draw current node setfillstyle(SOLID_FILL, BLUE); fillellipse(x, y, 33, 33); moveto(x - 10 - 6*(root->data.size() - 1), y - 10); char buffer[2]; buffer[1] = '\0'; for(int i = 0; i < root->data.size(); i++) { buffer[0] = root->data[i]; outtext(buffer); } } }
void drawFunction(){ // Open graphic mode
int gd = DETECT, gm; initwindow(1280,1600);
int startX = 500, startY=50; int page =0;
setlinestyle(CENTER_LINE, 0,3); // change line thickness settextstyle(4, HORIZ_DIR, 1); //change character size and font setbkcolor(BLUE);
/*Use arrow key to move the graph, Up_key to see upper part Down_key to see lower part Right_key to see right_part Left_key to see left_part SPACE to quit the graphic window and go back to the menu window*/
if(GetAsyncKeyState(VK_RIGHT)){ startX -= 20; //Move the graph to the right }else if(GetAsyncKeyState(VK_LEFT)){ startX += 20; //Move the graph to the left }else if( GetAsyncKeyState(VK_UP) ){
startY +=20; //Move the graph up }else if( GetAsyncKeyState(VK_DOWN)){ startY -= 20; //Move the graph down }else if(GetAsyncKeyState(VK_SPACE)){ break; } delay(10); page = 1 - page;