forked from AllAlgorithms/c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_tree_sort.c
64 lines (52 loc) · 1.18 KB
/
binary_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
#include <stdio.h>
#include <stdlib.h>
struct btreenode
{
struct btreenode *leftchild;
int data ;
struct btreenode *rightchild;
};
void insert ( struct btreenode **, int );
void inorder ( struct btreenode * );
int main( )
{
struct btreenode *bt;
int i, j;
bt = NULL ;
int arr[] = {12, 11, 13, 5, 6, 7};
int num = sizeof(arr)/sizeof(arr[0]);
printf("Given array is \n");
for (i = 0 ; i < num ; i++){
printf ( "%d\t", arr[i]) ;
}
for (j = 0 ; j < num ; j++){
insert ( &bt, arr[j] ) ;
}
printf("\nSorted array is \n");
inorder (bt) ;
return 0;
}
void insert (struct btreenode **sr, int num)
{
if ( *sr == NULL ){
*sr = malloc (sizeof(struct btreenode));
(*sr)->leftchild = NULL;
(*sr)->data = num;
(*sr)->rightchild = NULL;
}else{
if (num < (*sr)->data){
insert (&( (*sr)->leftchild), num);
}
else{
insert (&( (*sr)->rightchild), num);
}
}
}
void inorder (struct btreenode *sr)
{
if ( sr != NULL ){
inorder (sr->leftchild);
printf ("%d\t", sr->data);
inorder (sr->rightchild);
}
}