-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_sort.c
116 lines (62 loc) · 1.75 KB
/
tree_sort.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
#include <stdio.h>
#include <stdlib.h>
struct bin_tree {
int value;
struct bin_tree* left;
struct bin_tree* right;
};
struct bin_tree* create_tree(int value){
struct bin_tree* tree = malloc(sizeof(struct bin_tree));
tree -> value = value;
tree -> left = NULL;
tree -> right = NULL;
return tree;
}
void drop_value(struct bin_tree** tree, int value){
if (*tree == NULL){
*tree = create_tree(value);
return;
}
struct bin_tree** child;
if (value < (*tree) -> value){
child = &((*tree) -> left);
}else{
child = &((*tree) -> right);
}
drop_value(child, value);
}
struct bin_tree* create_sort_tree(int* values, int n_elements){
printf("%d\n",n_elements);
struct bin_tree* top = create_tree(values[0]);
for (int i=1;i<n_elements;i++){
drop_value(&top, values[i]);
}
return top;
}
void collapse_sort_tree(struct bin_tree* tree, int* sorted, int* count){
if (tree == NULL){
return;
}
collapse_sort_tree(tree -> left, sorted, count);
sorted[*count] = tree -> value;
*count = (*count) + 1;
collapse_sort_tree(tree -> right, sorted, count);
}
int* sort(int* values, int size){
int n_elements = size/sizeof(values[0]);
printf("%d\n",n_elements);
struct bin_tree* sort_tree = create_sort_tree(values,n_elements);
int* sorted = calloc(n_elements, sizeof(int));
int count = 0;
collapse_sort_tree(sort_tree, sorted, &count);
return sorted;
}
int main(){
int values[] = {20,3,11,9,1,4,99,-10,1};
int* sorted = sort(values,sizeof(values));
int n_elements = sizeof(values)/4;
for (int i=0;i<n_elements;i++){
printf("%d,",sorted[i]);
}
return 0;
}