forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_traversal.c
143 lines (115 loc) · 3.08 KB
/
tree_traversal.c
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "utility.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
struct node {
struct node *children;
size_t children_size;
int id;
};
struct node create_tree(int rows, size_t num_children) {
struct node n = {NULL, 0, rows};
if (rows > 0) {
n.children = (struct node*)malloc(num_children * sizeof(struct node));
n.children_size = num_children;
for (size_t i = 0; i < num_children; ++i) {
n.children[i] = create_tree(rows - 1, num_children);
}
}
return n;
}
void destroy_tree(struct node n) {
if (n.id > 0) {
for (size_t i = 0; i < n.children_size; ++i) {
destroy_tree(n.children[i]);
}
free(n.children);
}
}
void dfs_recursive(struct node n) {
printf("%d ", n.id);
if (n.children) {
for (size_t i = 0; i < n.children_size; ++i) {
dfs_recursive(n.children[i]);
}
}
}
void dfs_recursive_postorder(struct node n) {
for (size_t i = 0; i < n.children_size; ++i) {
dfs_recursive_postorder(n.children[i]);
}
printf("%d ", n.id);
}
void dfs_recursive_inorder_btree(struct node n) {
switch (n.children_size) {
case 2:
dfs_recursive_inorder_btree(n.children[0]);
printf("%d ", n.id);
dfs_recursive_inorder_btree(n.children[1]);
break;
case 1:
dfs_recursive_inorder_btree(n.children[0]);
printf("%d ", n.id);
break;
case 0:
printf("%d ", n.id);
break;
default:
printf("This is not a binary tree.\n");
break;
}
}
void dfs_stack(struct node n) {
struct stack stk = get_stack(sizeof(struct node*));
stack_push(&stk, &n);
struct node *tmp;
while (!stack_empty(&stk)) {
tmp = (struct node*)stack_pop(&stk);
if (!tmp) {
break;
}
printf("%d ", tmp->id);
for (size_t i = 0; i < tmp->children_size; ++i) {
stack_push(&stk, &tmp->children[i]);
}
}
free_stack(stk);
}
void bfs_queue(struct node n) {
struct queue q = get_queue(sizeof(struct node*));
enqueue(&q, &n);
struct node *tmp;
while (!queue_empty(&q)) {
tmp = (struct node*)dequeue(&q);
if (!tmp) {
break;
}
printf("%d ", tmp->id);
for (size_t i = 0; i < tmp->children_size; ++i) {
enqueue(&q, &tmp->children[i]);
}
}
free_queue(q);
}
int main() {
struct node root = create_tree(2, 3);
printf("[#]\nRecursive DFS:\n");
dfs_recursive(root);
printf("\n");
printf("[#]\nRecursive Postorder DFS:\n");
dfs_recursive_postorder(root);
printf("\n");
printf("[#]\nStack-based DFS:\n");
dfs_stack(root);
printf("\n");
printf("[#]\nQueue-based BFS:\n");
bfs_queue(root);
printf("\n");
destroy_tree(root);
struct node root_binary = create_tree(3, 2);
printf("[#]\nRecursive Inorder DFS for Binary Tree:\n");
dfs_recursive_inorder_btree(root_binary);
printf("\n");
destroy_tree(root_binary);
return 0;
}