-
Notifications
You must be signed in to change notification settings - Fork 3
/
2017_code_tree.cpp
42 lines (38 loc) · 1.21 KB
/
2017_code_tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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);//左子树递归
printf("%c", T->data[0]);//打印当前节点
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);//用@表示空节点
createTree(a, "+*-ab@-@@@@@@cd@@@@@@@@@@@@@@@@", 0, 31);//用@表示空节点
midQuation(a);
return 0;
}