nucLeaRsc2

Untitled

Jan 9th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. // DIN AFISARILE din SRD si SDR (inOrder si postOrder).
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. using namespace std;
  7.  
  8. struct Node{
  9. Node* left;
  10. Node* right;
  11. int value;
  12. };
  13.  
  14. void readData(char* fileName, int *n, int** inValues, int** postValues){
  15. FILE *in = fopen(fileName, "rt");
  16. int i, *localInValues, *localPostValues;
  17. fscanf(in, "%i", n);
  18.  
  19. localInValues = (int*)malloc(*n * sizeof(int));
  20. localPostValues = (int*)malloc(*n * sizeof(int));
  21.  
  22. for(i=0; i<*n; i++)
  23. fscanf(in, "%i", &localInValues[i]);
  24.  
  25. for(i=0; i<*n; i++)
  26. fscanf(in, "%i", &localPostValues[i]);
  27.  
  28. *inValues = localInValues;
  29. *postValues = localPostValues;
  30. }
  31.  
  32. void printArray(int* v, int n){
  33. int i;
  34. for(i=0; i<n; i++)
  35. printf("%i ", v[i]);
  36.  
  37. printf("\n");
  38. }
  39.  
  40. int findPosition(int value, int left, int right, int* inValues){
  41. int i;
  42. for(i=left; i<right; i++){
  43. if(inValues[i] == value)
  44. return i;
  45. }
  46.  
  47. return -1;
  48. }
  49.  
  50. Node* solveTask(int leftIn, int rightIn, int leftPost, int rightPost, int* inValues, int* postValues){
  51. if(rightIn - leftIn < 1)
  52. return NULL;
  53.  
  54. Node* localNode = (Node*)malloc(sizeof(Node));
  55. int currentValue = postValues[rightPost-1];
  56. int positionOnInValues = findPosition(currentValue, leftIn, rightIn, inValues);
  57. if(positionOnInValues == -1){
  58. printf("S-a intamplat ceva naspa\n");
  59. exit;
  60. }
  61.  
  62. localNode->value = currentValue;
  63. localNode->left = (Node*)malloc(sizeof(Node));
  64. localNode->right = (Node*)malloc(sizeof(Node));
  65.  
  66. int rightValues = rightIn - positionOnInValues;
  67. int leftValues = positionOnInValues - leftIn;
  68.  
  69. int rightSonRightPost = rightPost - 1; // aici e -1 ca trecem de radacina curenta (100 in primul exemplu)
  70. int rightSonLeftPost = rightPost - rightValues;
  71. int leftSonRightPost = rightSonLeftPost;
  72. int leftSonLeftPost = rightSonLeftPost - leftValues;
  73.  
  74. localNode->left = solveTask(leftIn, positionOnInValues, leftSonLeftPost, leftSonRightPost, inValues, postValues);
  75. localNode->right = solveTask(positionOnInValues+1, rightIn, rightSonLeftPost, rightSonRightPost, inValues, postValues);
  76.  
  77. return localNode;
  78.  
  79. }
  80.  
  81. void RSD(Node* root){
  82. if(!root)
  83. return;
  84.  
  85. printf("%i ", root->value);
  86.  
  87. if(root->left)
  88. RSD(root->left);
  89. if(root->right)
  90. RSD(root->right);
  91.  
  92. }
  93.  
  94. void SRD(Node* root){
  95. if(!root)
  96. return;
  97.  
  98. if(root->left)
  99. SRD(root->left);
  100.  
  101. printf("%i ", root->value);
  102.  
  103. if(root->right)
  104. SRD(root->right);
  105.  
  106. }
  107.  
  108. void SDR(Node* root){
  109. if(!root)
  110. return;
  111.  
  112. if(root->left)
  113. SDR(root->left);
  114.  
  115. if(root->right)
  116. SDR(root->right);
  117.  
  118. printf("%i ", root->value);
  119.  
  120.  
  121. }
  122.  
  123.  
  124. int main()
  125. {
  126. Node* root = (Node*)malloc(sizeof(Node));
  127.  
  128. int *inValues, *postValues, size;
  129. readData("in.in", &size, &inValues, &postValues);
  130.  
  131. //printArray(postValues, size);
  132. root = solveTask(0, size, 0, size, inValues, postValues);
  133.  
  134. RSD(root);
  135. printf("\n");
  136. SRD(root);
  137. printf("\n");
  138. SDR(root);
  139.  
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment