Friday, January 15, 2016

სორტირება ორობითი ხის გამოყენებით C

#include <stdio.h>
#include <stdlib.h>

typedef struct tree
{
int a; // monacemebi
struct tree *left; // marcxena Svili
struct tree *right; // marjvena Svili
} TREE;

TREE *add_to_tree(TREE *root, int new_value)
{
if (root==NULL) // tu ara hyavs shvilebi-vqmnit axal elements
{
root = (TREE*)malloc(sizeof(TREE));
root->a = new_value;
root->left = root->right = 0;
return root;
}
if (root->a < new_value) // vamatebt wibos
root->right = add_to_tree(root->right, new_value);
else
root->left = add_to_tree(root->left, new_value);
return root;
}

void tree_to_array(TREE *root, int a[]) // masivis shevsebis procedura
{
static int max2=0; // axali masivis elementebis mtvleli
if (root==NULL) return; // dasrulebis piroba-ar yavs shvilebi
tree_to_array(root->left, a); //marcxena qvexis shemovla
a[max2++] = root->a;
tree_to_array(root->right, a); //marjvena qvexis shemovla
free(root);
}

void sort_tree(int a[], int elem_total) //sakutriv sortireba
{
TREE *root;
int i;

root = NULL;
for (i=0; i<elem_total; i++) // masivis gavla da xis shevseba
root = add_to_tree(root, a[i]);
tree_to_array(root, a); //masivis shevseba
}
int main() {
int i;
//davalagot es mimdevroba
int a[14]={ 0,7,8,3,52,14,16,18,15,13,42,30,35,26 };

sort_tree(a, 14);
printf("sorted array:\n");
for (i=0;i<14;i++) printf("%d ",a[i]);
printf("\n\n");
system("pause");
return 0;
}

 პრეზენტაციის ლინკი:
https://www.dropbox.com/s/9z4zv0crwd35nx6/104541.pptx?dl=0

No comments:

Post a Comment