-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
147 lines (119 loc) · 3.02 KB
/
main.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
144
145
146
147
// The base C code is genereated using ChatGPT!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
// random number max value
#define MAX_RAND 100
// array, tree node num
#define COUNT 10
// Define the node structure
struct Node {
int data;
// left, right child pointer
struct Node* left;
struct Node* right;
};
int sort_tree(struct Node* root, int i, int sorted_nums[]) {
if (root == NULL) {
return i; // 현재 인덱스를 반환
}
// 왼쪽 서브트리 순회
i = sort_tree(root->left, i, sorted_nums);
// 현재 노드의 데이터 저장
sorted_nums[i++] = root->data;
// 오른쪽 서브트리 순회
i = sort_tree(root->right, i, sorted_nums);
return i;
}
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation error\n");
exit(1);
}
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Function to insert a new node in the binary tree
struct Node* insert(struct Node* node, int data) {
if (node == NULL) {
return createNode(data);
}
if (data < node->data) {
node->left = insert(node->left, data);
} else if (data > node->data) {
node->right = insert(node->right, data);
}
return node;
}
bool check_if_sorted(int nums[], int sorted_nums[]){
bool is_found;
#ifdef DEBUG
printf("nums: ");
for (int i=0; i<COUNT; i++) {
printf("%d ", nums[i]);
}
printf("\n");
printf("sorted_nums: ");
for (int i=0; i<COUNT; i++) {
printf("%d ", sorted_nums[i]);
}
printf("\n");
#endif
for (int i=0; i<COUNT-1; i++) {
is_found = false;
for (int j=0; j<COUNT; j++)
if (sorted_nums[i] == nums[j])
is_found = true;
if (!is_found) {
printf("[-] FAIL\n");
return false;
}
if (sorted_nums[i] > sorted_nums[i+1]) {
printf("[-] FAIL\n");
return false;
}
}
printf("[+] PASS\n");
return true;
}
// Generate unique, random numbers
bool gen_rand_nums(int nums[]) {
for (int i=0; i<COUNT; i++) {
nums[i] = rand() % MAX_RAND;
for (int j=0; j<i; j++) {
if (nums[j] == nums[i])
return false;
}
}
return true;
}
struct Node *make_tree(int nums[]) {
struct Node* root = NULL;
for (int i=0; i<COUNT; i++) {
if (i == 0)
root = insert(root, nums[i]);
else
insert(root, nums[i]);
}
return root;
}
// Main function
int main() {
int nums[COUNT];
int sorted_nums[COUNT];
srand(time(NULL));
while (1) {
if (gen_rand_nums(nums))
break;
}
struct Node* root = NULL;
root = make_tree(nums);
sort_tree(root, 0, sorted_nums);
check_if_sorted(nums, sorted_nums);
return 0;
}