-
Notifications
You must be signed in to change notification settings - Fork 0
/
treesort.c
59 lines (54 loc) · 1.08 KB
/
treesort.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
#include <stdio.h>
#include <stdlib.h>
int i;
struct node {
int data;
struct node* l;
struct node* r;
};
void treeToArr(struct node* t, int A[])
{
if (t != NULL)
{
treeToArr(t->l, A);
A[i++] = t->data;
treeToArr(t->r, A);
}
}
void treesort(int A[], int sz)
{
struct node* root = NULL;;
struct node* t, *itr, *p;
for (int i = 0; i < sz; i++)
{
t = malloc(sizeof(struct node));
t->data = A[i];
t->l = t->r = NULL;
if (root == NULL)
{
root = t;
} else {
for (itr = root; itr != NULL; itr = (A[i] < itr->data) ? itr->l:itr->r)
p = itr;
if (A[i] < p->data) p->l = t;
else p->r = t;
}
}
i = 0;
treeToArr(root, A);
}
void printArr(int A[], int sz)
{
for (int i = 0; i < sz; i++)
printf("%d ", *(A+i));
printf("\n");
}
int main()
{
int A[] = { 13, 37, 42, 11, 7, 69, 5, 108, 127, 17};
int sz = sizeof(A)/sizeof(A[0]);
printArr(A, sz);
treesort(A, sz);
printArr(A, sz);
return 0;
}