hoome

Untitled

Nov 18th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define true 1
  6. #define false 0
  7.  
  8. // Struct of a tree
  9. typedef struct Leaf {
  10.     struct Leaf *left;
  11.     struct Leaf *right;
  12.     int value;
  13.     int pos;
  14. } Leaf;
  15.  
  16. // Find a value in a vector
  17. int find(int *vector, int size, int match) {
  18.     int counter = 0;
  19.     while (counter < size) {
  20.         if (vector[counter] == match) {
  21.             return(counter);
  22.         }
  23.         counter++;
  24.     }
  25.     return(-1);
  26. }
  27.  
  28. // Variables to create a tree/vector
  29. int maxrand = 10;
  30. int counter = 0;
  31.  
  32. // Create a tree/vector
  33. Leaf *makeTree(int *vector,int size) {
  34.     if (size != 0) {
  35.         Leaf *aux = (Leaf *)malloc(sizeof(Leaf));
  36.         int rs = -1;
  37.         int value = 0;
  38.         do {
  39.             value = rand()%(maxrand+1);
  40.             rs = find(vector,counter,value);
  41.         } while (rs != -1);
  42.         vector[counter] = value;
  43.         aux->value = value;
  44.         aux->pos = counter;
  45.         counter++;
  46.         aux->left = makeTree(vector,size/2);
  47.         aux->right = makeTree(vector,size - (size/2) - 1);
  48.         return(aux);
  49.     } else {
  50.         return(NULL);
  51.     }
  52. }
  53.  
  54. // Print a vector
  55. void print(int *vector,int size) {
  56.     int counter = 0;
  57.     while (counter < size) {
  58.         printf("[%d] => %d\n", counter,vector[counter]);
  59.         counter++;
  60.     }
  61. }
  62.  
  63. // Print a tree
  64. void printt(Leaf *tree) {
  65.     if (tree != NULL) {
  66.         printf("[%d] => %d\n",tree->pos,tree->value);
  67.         printt(tree->left);
  68.         printt(tree->right);
  69.     }
  70. }
  71.  
  72. int iterator = 0;
  73. // Find in a tree
  74. int findt(Leaf *tree,int value) {
  75.     iterator++;
  76.     if (tree == NULL) {
  77.         return(-1);
  78.     } else {
  79.         if (tree->value == value) {
  80.             return(tree->pos);
  81.         } else {
  82.             int rs = findt(tree->left,value);
  83.             if (rs == -1) {
  84.                 return(findt(tree->right,value));
  85.             } else {
  86.                 return(rs);
  87.             }
  88.         }
  89.     }
  90.     return(-1);
  91. }
  92.  
  93. // Main Loop
  94. int main(int argc, char *argv[]) {
  95.     if (argc != 4) {
  96.         printf("You need to use %s [size] [random max number] [search].\nEx: %s 30 100 8.\n",argv[0],argv[0]);
  97.     } else {
  98.         srand(time(NULL));
  99.         int size = atoi(argv[1]);
  100.         maxrand = atoi(argv[2]);
  101.         int search = atoi(argv[3]);
  102.  
  103.         int vector[size];
  104.         Leaf *tree = makeTree(vector,size);
  105.         //printf("Original Vector:\n");
  106.         //print(vector,size);
  107.         printf("\nTree:\n");
  108.         printt(tree);
  109.         int res = find(vector,size,search);
  110.         if (res == -1) {
  111.             printf("\nNot found using While,%d iteractions.\n", size);
  112.         } else {
  113.             printf("\nFound using While (pos = %d),%d iteractions.\n",res,res+1);
  114.         }
  115.         res = findt(tree,search);
  116.         if (res == -1) {
  117.             printf("\nNot found using Tree, %d iteraction.\n", iterator);
  118.         } else {
  119.             printf("\nFound using Tree (pos = %d), %d iteractions.\n",res,iterator);
  120.         }
  121.     }
  122.     return(false);
  123. }
Advertisement
Add Comment
Please, Sign In to add comment