5
5
typedef struct node {
6
6
struct node * children ;
7
7
int children_num ;
8
- int ID ;
8
+ int id ;
9
9
} node ;
10
10
11
11
typedef struct node_list {
@@ -60,8 +60,9 @@ void queue_pop(node_points *np) {
60
60
}
61
61
62
62
void create_tree (node * n , int num_row , int num_child ) {
63
- n -> ID = num_row ;
63
+ n -> id = num_row ;
64
64
if (num_row == 0 ) {
65
+ n -> children_num = 0 ;
65
66
return ;
66
67
}
67
68
@@ -74,26 +75,49 @@ void create_tree(node *n, int num_row, int num_child) {
74
75
}
75
76
}
76
77
77
- void DFS_recursive (node n ) {
78
- printf ("%d\n" , n .ID );
78
+ void dfs_recursive (node n ) {
79
+ printf ("%d\n" , n .id );
79
80
if (!n .children ) {
80
81
return ;
81
82
}
82
83
83
84
for (int i = 0 ; i < n .children_num ; ++ i ) {
84
- DFS_recursive (n .children [i ]);
85
+ dfs_recursive (n .children [i ]);
85
86
}
86
87
}
87
88
88
- void DFS_stack (node n ) {
89
+ void dfs_recursive_postorder (node n ) {
90
+ for (int i = 0 ; i < n .children_num ; ++ i ) {
91
+ dfs_recursive_postorder (n .children [i ]);
92
+ }
93
+
94
+ printf ("%d\n" , n .id );
95
+ }
96
+
97
+ void dfs_recursive_inorder_btree (node n ) {
98
+ if (n .children_num > 2 ) {
99
+ printf ("This is not a binary tree.\n" );
100
+ return ;
101
+ }
102
+
103
+ if (n .children_num > 0 ) {
104
+ dfs_recursive_inorder_btree (n .children [0 ]);
105
+ printf ("%d\n" , n .id );
106
+ dfs_recursive_inorder_btree (n .children [1 ]);
107
+ } else {
108
+ printf ("%d\n" , n .id );
109
+ }
110
+ }
111
+
112
+ void dfs_stack (node n ) {
89
113
node_points stack ;
90
114
memset (& stack , 0 , sizeof (node_points ));
91
115
push (& stack , n );
92
116
node temp ;
93
117
94
118
while (stack .start_point != NULL ) {
95
119
temp = stack .end_point -> n ;
96
- printf ("%d\n" , temp .ID );
120
+ printf ("%d\n" , temp .id );
97
121
stack_pop (& stack );
98
122
for (int i = 0 ; i < temp .children_num ; ++ i ) {
99
123
if (!temp .children ) {
@@ -105,15 +129,15 @@ void DFS_stack(node n) {
105
129
}
106
130
}
107
131
108
- void BFS_queue (node n ) {
132
+ void bfs_queue (node n ) {
109
133
node_points queue ;
110
134
memset (& queue , 0 , sizeof (node_points ));
111
135
push (& queue , n );
112
136
node temp ;
113
137
114
138
while (queue .start_point != NULL ) {
115
139
temp = queue .start_point -> n ;
116
- printf ("%d\n" , temp .ID );
140
+ printf ("%d\n" , temp .id );
117
141
queue_pop (& queue );
118
142
for (int i = 0 ; i < temp .children_num ; ++ i ) {
119
143
if (!temp .children ) {
@@ -126,7 +150,7 @@ void BFS_queue(node n) {
126
150
}
127
151
128
152
void destroy_tree (node * n ) {
129
- if (n -> ID == 0 ) {
153
+ if (n -> id == 0 ) {
130
154
return ;
131
155
}
132
156
@@ -140,11 +164,10 @@ void destroy_tree(node *n) {
140
164
int main () {
141
165
node root ;
142
166
create_tree (& root , 3 , 3 );
143
- DFS_recursive (root );
144
- //DFS_stack (root);
145
- //BFS_queue (root);
167
+ //dfs_recursive (root);
168
+ //dfs_stack (root);
169
+ //bfs_queue (root);
146
170
destroy_tree (& root );
147
171
148
172
return 0 ;
149
173
}
150
-
0 commit comments