-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtreeSort.c
58 lines (51 loc) · 1.65 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
#include "insertion.h"
// Worst case performance О(n**2) comparisons
// Best case performance O(n log n) comparisons
// Average performance О(n log n) comparisons
// Function that releases the tree
Node* release(Node *T){
if(T != NULL) {
if(T->right != NULL) // Check if there's a node on right
release(T->right);
if(T->left != NULL) // Check if there's a node on left
release(T->left);
free(T); // Reaches a leaf and releases the node
}
return NULL;
}
// Function that stores the elements of the sorted tree in the array
void storeValueBinaryTree(Node *root, long int *array, int **i){
if(root != NULL){
if(root->left != NULL)
storeValueBinaryTree(root->left, array, i);
*(array + **i) = root->value;
(**i)++;
if(root->right != NULL)
storeValueBinaryTree(root->right, array, i);
}
}
// Function that creates new nodes for the tree
void insertNodeBinaryTree(Node **node, long int value){
if(!(*node)){
(*node) = malloc(sizeof(Node));
if(!(*node)) return;
(*node)->value = value;
(*node)->left = (*node)->right = NULL;
}
else{
if(value < (*node)->value)
insertNodeBinaryTree(&(*node)->left,value);
else// if(n > (*node)->value)
insertNodeBinaryTree(&(*node)->right,value);
}
}
void treeSort(long int *array, int length){
Node *root = NULL;
int i,*p;
for(i ^= i; i < length; i++)
insertNodeBinaryTree(&root, *(array + i));
i ^= i;
p = &i;
storeValueBinaryTree(root, array, &p); // Store the values in the array
root = release(root);
}