-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintTreeByChildren.c
More file actions
113 lines (104 loc) · 2.68 KB
/
PrintTreeByChildren.c
File metadata and controls
113 lines (104 loc) · 2.68 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include<windows.h>
#endif
typedef struct Tree Tree;
typedef struct List List;
typedef struct List{
Tree* value;
struct List* next;
}List;
typedef struct Tree{
int value;
List* children;
}Tree;
Tree* createNode(int value){
Tree* now = (Tree*)malloc(sizeof(Tree));
now->value = value;
now->children = NULL;
return now;
}
void printPreorder(Tree* root){
if(root == NULL) return;
printf("%c ", root->value);
List* cur = root->children;
while(cur != NULL){
printPreorder(cur->value);
cur = cur->next;
}
}
void printByChildren(Tree* root){
if(root == NULL) return;
List* cur = root->children;
printf("%c -> ", root->value);
while(cur != NULL){
printf("%c ", cur->value->value);
printf("-> ");
cur = cur->next;
}
printf("^\n");
cur = root->children;
while(cur != NULL){
printByChildren(cur->value);
cur = cur->next;
}
}
Tree* input(){
Tree* root = NULL;
Tree* arr[100];
arr[0] = root;
int depth = 0;
printf("举例: A(B(D,E),C(F,G))\n");
printf("请输入二叉树的字符序列:");
char c = getchar();
while(c != '\n'){
if(c == '(')
depth++;
if(c == ')')
depth--;
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')){
if(root == NULL){
root = createNode(c);
arr[depth] = root;
} else{
Tree* now = createNode(c);
Tree* parent = arr[depth - 1];
List* cur = parent->children;
if(cur == NULL){
parent->children = (List*)malloc(sizeof(List));
parent->children->value = now;
parent->children->next = NULL;
} else{
while(cur->next != NULL){
cur = cur->next;
}
cur->next = (List*)malloc(sizeof(List));
cur->next->value = now;
cur->next->next = NULL;
}
arr[depth] = now;
}
}
c = getchar();
}
return root;
}
int main(){
// Windows下设置UTF-8编码
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
DWORD mode;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleMode(hConsole, &mode);
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hConsole, mode);
#endif
Tree* root = input();
printf("先序遍历: \n");
printPreorder(root);
printf("\n");
printf("孩子表示法: \n");
printByChildren(root);
return 0;
}