Skip to content

Commit e8bac04

Browse files
committed
Adding missing examples to tree_traversal.c
1 parent 6a82e2b commit e8bac04

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

chapters/tree_traversal/code/c/tree_traversal.c

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
typedef struct node {
66
struct node *children;
77
int children_num;
8-
int ID;
8+
int id;
99
} node;
1010

1111
typedef struct node_list {
@@ -60,8 +60,9 @@ void queue_pop(node_points *np) {
6060
}
6161

6262
void create_tree(node *n, int num_row, int num_child) {
63-
n->ID = num_row;
63+
n->id = num_row;
6464
if (num_row == 0) {
65+
n->children_num = 0;
6566
return;
6667
}
6768

@@ -74,26 +75,49 @@ void create_tree(node *n, int num_row, int num_child) {
7475
}
7576
}
7677

77-
void DFS_recursive(node n) {
78-
printf("%d\n", n.ID);
78+
void dfs_recursive(node n) {
79+
printf("%d\n", n.id);
7980
if (!n.children) {
8081
return;
8182
}
8283

8384
for (int i = 0; i < n.children_num; ++i) {
84-
DFS_recursive(n.children[i]);
85+
dfs_recursive(n.children[i]);
8586
}
8687
}
8788

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) {
89113
node_points stack;
90114
memset(&stack, 0, sizeof(node_points));
91115
push(&stack, n);
92116
node temp;
93117

94118
while (stack.start_point != NULL) {
95119
temp = stack.end_point->n;
96-
printf("%d\n", temp.ID);
120+
printf("%d\n", temp.id);
97121
stack_pop(&stack);
98122
for (int i = 0; i < temp.children_num; ++i) {
99123
if (!temp.children) {
@@ -105,15 +129,15 @@ void DFS_stack(node n) {
105129
}
106130
}
107131

108-
void BFS_queue(node n) {
132+
void bfs_queue(node n) {
109133
node_points queue;
110134
memset(&queue, 0, sizeof(node_points));
111135
push(&queue, n);
112136
node temp;
113137

114138
while (queue.start_point != NULL) {
115139
temp = queue.start_point->n;
116-
printf("%d\n", temp.ID);
140+
printf("%d\n", temp.id);
117141
queue_pop(&queue);
118142
for (int i = 0; i < temp.children_num; ++i) {
119143
if (!temp.children) {
@@ -126,7 +150,7 @@ void BFS_queue(node n) {
126150
}
127151

128152
void destroy_tree(node *n) {
129-
if (n->ID == 0) {
153+
if (n->id == 0) {
130154
return;
131155
}
132156

@@ -140,11 +164,10 @@ void destroy_tree(node *n) {
140164
int main() {
141165
node root;
142166
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);
146170
destroy_tree(&root);
147171

148172
return 0;
149173
}
150-

0 commit comments

Comments
 (0)