mirror of
https://github.com/xusun0623/exam_code_for_408.git
synced 2026-06-17 15:56:37 +08:00
add 2017&2016
This commit is contained in:
39
code_tree_2017.cpp
Normal file
39
code_tree_2017.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include<stdio.h>
|
||||
#include <stdlib.h>
|
||||
typedef struct node {
|
||||
char data[10];
|
||||
struct node* left, * right;
|
||||
}BTree;
|
||||
|
||||
void midQuation(BTree* T) {
|
||||
if (T != NULL && T->data[0] != '@') {
|
||||
char t = T->data[0];
|
||||
bool flag = false;
|
||||
flag = (t == '+' || t == '-' || t == '*' || t == '/');
|
||||
if (flag)printf("(");
|
||||
if (T->left != NULL)midQuation(T->left);
|
||||
for (int i = 0;T->data[i] != '\0';i++) {
|
||||
printf("%c", T->data[i]);
|
||||
}
|
||||
if (T->right != NULL)midQuation(T->right);
|
||||
if (flag)printf(")");
|
||||
}
|
||||
}
|
||||
|
||||
void createTree(BTree* node, char c[], int pos, int length) {
|
||||
if (pos < length) {//填入数据
|
||||
node->data[0] = c[pos];
|
||||
node->data[1] = '\0';
|
||||
node->left = (BTree*)malloc(sizeof(BTree));
|
||||
node->right = (BTree*)malloc(sizeof(BTree));
|
||||
createTree(node->left, c, pos * 2 + 1, length);
|
||||
createTree(node->right, c, pos * 2 + 2, length);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
BTree* a = (BTree*)malloc(sizeof(BTree));
|
||||
createTree(a, "*+*abc-@@@@@@@d@@@@@@@@@@@@@@@@", 0, 31);
|
||||
midQuation(a);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user