|
1 |
| -#include <stdio.h> |
| 1 | +#include "utility.h" |
| 2 | + |
| 3 | +#include <stddef.h> |
2 | 4 | #include <stdlib.h>
|
3 |
| -#include <string.h> |
| 5 | +#include <stdio.h> |
4 | 6 |
|
5 |
| -typedef struct node { |
| 7 | +struct node { |
6 | 8 | struct node *children;
|
7 |
| - int children_num; |
8 |
| - int ID; |
9 |
| -} node; |
10 |
| - |
11 |
| -typedef struct node_list { |
12 |
| - node n; |
13 |
| - struct node_list *last_list, *next_list; |
14 |
| -} node_list; |
15 |
| - |
16 |
| -typedef struct node_points { |
17 |
| - node_list *start_point, *end_point; |
18 |
| -} node_points; |
19 |
| - |
20 |
| -void push(node_points *np, node n) { |
21 |
| - node_list *temp = (node_list*)malloc(sizeof(node_list)); |
22 |
| - temp->n = n; |
23 |
| - temp->last_list = temp->next_list = NULL; |
24 |
| - |
25 |
| - if (!np->end_point) { |
26 |
| - np->start_point = temp; |
27 |
| - } else { |
28 |
| - np->end_point->next_list = temp; |
29 |
| - temp->last_list = np->end_point; |
| 9 | + size_t children_size; |
| 10 | + int id; |
| 11 | +}; |
| 12 | + |
| 13 | +struct node create_tree(int rows, size_t num_children) { |
| 14 | + struct node n = {NULL, 0, rows}; |
| 15 | + |
| 16 | + if (rows > 0) { |
| 17 | + n.children = (struct node*)malloc(num_children * sizeof(struct node)); |
| 18 | + n.children_size = num_children; |
| 19 | + for (size_t i = 0; i < num_children; ++i) { |
| 20 | + n.children[i] = create_tree(rows - 1, num_children); |
| 21 | + } |
30 | 22 | }
|
31 | 23 |
|
32 |
| - np->end_point = temp; |
| 24 | + return n; |
33 | 25 | }
|
34 | 26 |
|
35 |
| -void stack_pop(node_points *np) { |
36 |
| - node_list *temp; |
37 |
| - temp = np->end_point; |
38 |
| - |
39 |
| - if (temp) { |
40 |
| - np->end_point = temp->last_list; |
41 |
| - if (!np->end_point) { |
42 |
| - np->start_point = NULL; |
| 27 | +void destroy_tree(struct node n) { |
| 28 | + if (n.id > 0) { |
| 29 | + for (size_t i = 0; i < n.children_size; ++i) { |
| 30 | + destroy_tree(n.children[i]); |
43 | 31 | }
|
44 | 32 |
|
45 |
| - free(temp); |
| 33 | + free(n.children); |
46 | 34 | }
|
47 | 35 | }
|
48 | 36 |
|
49 |
| -void queue_pop(node_points *np) { |
50 |
| - node_list *temp; |
51 |
| - temp = np->start_point; |
52 |
| - if (temp) { |
53 |
| - np->start_point = temp->next_list; |
54 |
| - if (!np->start_point) { |
55 |
| - np->end_point = NULL; |
56 |
| - } |
| 37 | +void dfs_recursive(struct node n) { |
| 38 | + printf("%d\n", n.id); |
57 | 39 |
|
58 |
| - free(temp); |
| 40 | + if (n.children) { |
| 41 | + for (size_t i = 0; i < n.children_size; ++i) { |
| 42 | + dfs_recursive(n.children[i]); |
| 43 | + } |
59 | 44 | }
|
60 | 45 | }
|
61 | 46 |
|
62 |
| -void create_tree(node *n, int num_row, int num_child) { |
63 |
| - n->ID = num_row; |
64 |
| - if (num_row == 0) { |
65 |
| - return; |
| 47 | +void dfs_recursive_postorder(struct node n) { |
| 48 | + for (size_t i = 0; i < n.children_size; ++i) { |
| 49 | + dfs_recursive_postorder(n.children[i]); |
66 | 50 | }
|
67 | 51 |
|
68 |
| - n->children = (node *)malloc(num_child * sizeof(*n->children)); |
69 |
| - n->children_num = num_child; |
70 |
| - for (int i = 0; i < num_child; ++i) { |
71 |
| - node child; |
72 |
| - create_tree(&child, num_row - 1, num_child); |
73 |
| - *(n->children + i) = child; |
74 |
| - } |
| 52 | + printf("%d\n", n.id); |
75 | 53 | }
|
76 | 54 |
|
77 |
| -void DFS_recursive(node n) { |
78 |
| - printf("%d\n", n.ID); |
79 |
| - if (!n.children) { |
80 |
| - return; |
81 |
| - } |
82 |
| - |
83 |
| - for (int i = 0; i < n.children_num; ++i) { |
84 |
| - DFS_recursive(n.children[i]); |
| 55 | +void dfs_recursive_inorder_btree(struct node n) { |
| 56 | + switch (n.children_size) { |
| 57 | + case 2: |
| 58 | + dfs_recursive_inorder_btree(n.children[0]); |
| 59 | + printf("%d\n", n.id); |
| 60 | + dfs_recursive_inorder_btree(n.children[1]); |
| 61 | + break; |
| 62 | + case 1: |
| 63 | + dfs_recursive_inorder_btree(n.children[0]); |
| 64 | + printf("%d\n", n.id); |
| 65 | + break; |
| 66 | + case 0: |
| 67 | + printf("%d\n", n.id); |
| 68 | + break; |
| 69 | + default: |
| 70 | + printf("This is not a binary tree.\n"); |
| 71 | + break; |
85 | 72 | }
|
86 | 73 | }
|
87 | 74 |
|
88 |
| -void DFS_stack(node n) { |
89 |
| - node_points stack; |
90 |
| - memset(&stack, 0, sizeof(node_points)); |
91 |
| - push(&stack, n); |
92 |
| - node temp; |
93 |
| - |
94 |
| - while (stack.start_point != NULL) { |
95 |
| - temp = stack.end_point->n; |
96 |
| - printf("%d\n", temp.ID); |
97 |
| - stack_pop(&stack); |
98 |
| - for (int i = 0; i < temp.children_num; ++i) { |
99 |
| - if (!temp.children) { |
100 |
| - break; |
101 |
| - } |
102 |
| - |
103 |
| - push(&stack, temp.children[i]); |
| 75 | +void dfs_stack(struct node n) { |
| 76 | + struct stack stk = get_stack(sizeof(struct node*)); |
| 77 | + stack_push(&stk, &n); |
| 78 | + struct node *tmp; |
| 79 | + |
| 80 | + while (!stack_empty(&stk)) { |
| 81 | + tmp = (struct node*)stack_pop(&stk); |
| 82 | + if (!tmp) { |
| 83 | + break; |
104 | 84 | }
|
105 |
| - } |
106 |
| -} |
107 | 85 |
|
108 |
| -void BFS_queue(node n) { |
109 |
| - node_points queue; |
110 |
| - memset(&queue, 0, sizeof(node_points)); |
111 |
| - push(&queue, n); |
112 |
| - node temp; |
113 |
| - |
114 |
| - while (queue.start_point != NULL) { |
115 |
| - temp = queue.start_point->n; |
116 |
| - printf("%d\n", temp.ID); |
117 |
| - queue_pop(&queue); |
118 |
| - for (int i = 0; i < temp.children_num; ++i) { |
119 |
| - if (!temp.children) { |
120 |
| - break; |
121 |
| - } |
122 |
| - |
123 |
| - push(&queue, temp.children[i]); |
| 86 | + printf("%d\n", tmp->id); |
| 87 | + for (size_t i = 0; i < tmp->children_size; ++i) { |
| 88 | + stack_push(&stk, &tmp->children[i]); |
124 | 89 | }
|
125 | 90 | }
|
| 91 | + |
| 92 | + free_stack(stk); |
126 | 93 | }
|
127 | 94 |
|
128 |
| -void destroy_tree(node *n) { |
129 |
| - if (n->ID == 0) { |
130 |
| - return; |
131 |
| - } |
| 95 | +void bfs_queue(struct node n) { |
| 96 | + struct queue q = get_queue(sizeof(struct node*)); |
| 97 | + enqueue(&q, &n); |
| 98 | + struct node *tmp; |
132 | 99 |
|
133 |
| - for (int i = 0; i < n->children_num; ++i) { |
134 |
| - destroy_tree(n->children + i); |
| 100 | + while (!queue_empty(&q)) { |
| 101 | + tmp = (struct node*)dequeue(&q); |
| 102 | + if (!tmp) { |
| 103 | + break; |
| 104 | + } |
| 105 | + |
| 106 | + printf("%d\n", tmp->id); |
| 107 | + for (size_t i = 0; i < tmp->children_size; ++i) { |
| 108 | + enqueue(&q, &tmp->children[i]); |
| 109 | + } |
135 | 110 | }
|
136 | 111 |
|
137 |
| - free(n->children); |
| 112 | + free_queue(q); |
138 | 113 | }
|
139 | 114 |
|
140 | 115 | int main() {
|
141 |
| - node root; |
142 |
| - create_tree(&root, 3, 3); |
143 |
| - DFS_recursive(root); |
144 |
| - //DFS_stack(root); |
145 |
| - //BFS_queue(root); |
146 |
| - destroy_tree(&root); |
| 116 | + struct node root = create_tree(3, 3); |
| 117 | + bfs_queue(root); |
| 118 | + destroy_tree(root); |
147 | 119 |
|
148 | 120 | return 0;
|
149 | 121 | }
|
150 |
| - |
0 commit comments