-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTreeSort.h
59 lines (54 loc) · 1.12 KB
/
BinarySearchTreeSort.h
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
TRI Binary_search_tree_sort_Create()
{
TRI This;
This.name = "Binary search tree";
This.sort = binary_search_tree_sort;
return This;
}
struct node
{
struct node *rightchild;
int value;
struct node *leftchild;
};
void insert(struct node **source, int value)
{
if(*source == NULL)
{
*source = malloc(sizeof(struct node));
(*source)->leftchild = NULL;
(*source)->value = value;
(*source)->rightchild = NULL;
return;
}
else
{
if(value < (*source)->value)
insert(&((*source)->leftchild), value);
else
insert(&((*source)->rightchild), value);
}
return;
}
int indexOrder = -1;
void inorder(struct node *source, int tab[])
{
if(source != NULL)
{
inorder(source->leftchild, tab);
tab[++indexOrder] = source->value;
inorder(source->rightchild, tab);
}
else
return;
}
void binary_search_tree_sort(int tab[], int size)
{
int i;
struct node *root;
root = NULL;
for (i = 0; i < size; i++) {
insert(&root, tab[i]);
}
inorder(root, tab);
}