binarno stablo - polje


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #include <stdlib.h>
  2. #define ARRAYSIZE 1000000
  3.  
  4. struct element
  5. {
  6. int label;
  7. int used;
  8. };
  9.  
  10. typedef struct bt
  11. {
  12. struct element elements[ARRAYSIZE];
  13. } *btree;
  14.  
  15. typedef int node;
  16.  
  17. btree init(int x)
  18. {
  19. btree myBTree;
  20. int i;
  21.  
  22. myBTree = (btree)malloc(sizeof(struct bt));
  23. for(i = 1; i < ARRAYSIZE; i++){
  24. if(i == 1)
  25. {
  26. myBTree->elements[i].label = x;
  27. myBTree->elements[i].used = 1;
  28. }
  29. else
  30. {
  31. myBTree->elements[i].used = 0;
  32. }
  33. }
  34.  
  35. return myBTree;
  36. }
  37.  
  38. node root(btree T)
  39. {
  40. return 1;
  41. }
  42.  
  43. node parrent(node n, btree T)
  44. {
  45. if(n == 1)
  46. {
  47. printf("Roditelj korjenskog cvora ne postoji\n");
  48. return -1;
  49. }
  50.  
  51. if(T->elements[n].used == 0)
  52. {
  53. printf("Ne postoji\n");
  54. exit(1);
  55. }
  56.  
  57. return (n/2);
  58. }
  59.  
  60. node leftChild(node n, btree T)
  61. {
  62. node child;
  63. child = n * 2;
  64. if (child > ARRAYSIZE-1)
  65. {
  66. printf("\noverflow\n");
  67. return -1;
  68. //exit(1);
  69. }
  70.  
  71. if(T->elements[child].used == 0)
  72. {
  73. return -1;
  74. }
  75. else
  76. {
  77. return child;
  78. }
  79. }
  80.  
  81. node rightChild(node n, btree T)
  82. {
  83. node child;
  84. child = (n * 2) + 1;
  85. if (child > ARRAYSIZE - 1)
  86. {
  87. printf("\noverflow\n");
  88. return -1;
  89. //exit(1);
  90. }
  91.  
  92. if(T->elements[child].used == 0)
  93. {
  94. return -1;
  95. }
  96. else
  97. {
  98. return child;
  99. }
  100. }
  101.  
  102. int label(node n, btree T)
  103. {
  104. if(T->elements[n].used == 0)
  105. {
  106. printf("ne postoji\n");
  107. return -1;
  108. }
  109. else
  110. {
  111. return T->elements[n].label;
  112. }
  113. }
  114.  
  115. void changeLabel(int x, node n, btree T)
  116. {
  117. if(T->elements[n].used == 0)
  118. {
  119. printf("ne postoji\n");
  120. return;
  121. }
  122. else
  123. {
  124. T->elements[n].label = x;
  125. }
  126. }
  127.  
  128. void createLeft(int x, node n, btree T)
  129. {
  130. node child;
  131. child = n * 2;
  132. if (child > ARRAYSIZE - 1)
  133. {
  134. printf("\noverflow\n");
  135. return;
  136.  
  137. }
  138.  
  139. if(T->elements[child].used == 1){
  140. printf("lijevi vec postoji\n");
  141. return;
  142. }
  143. else
  144. {
  145. T->elements[child].used = 1;
  146. T->elements[child].label = x;
  147. }
  148. }
  149.  
  150. void createRight(int x, node n, btree T)
  151. {
  152. node child;
  153. child = (n * 2) + 1;
  154. if (child > ARRAYSIZE - 1)
  155. {
  156. printf("\noverflow\n");
  157. return;
  158. }
  159. if(T->elements[child].used == 1)
  160. {
  161. printf("desno vec postoji\n");
  162. return;
  163. }
  164. else
  165. {
  166. T->elements[child].used = 1;
  167. T->elements[child].label = x;
  168. }
  169. }
  170.  
  171. void deleteN(node n, btree T){
  172. if(n == -1)
  173. return;
  174.  
  175. deleteN(leftChild(n, T), T);
  176.  
  177. T->elements[n].used=0;
  178.  
  179. deleteN(rightChild(n, T), T);
  180. }
  181.  
  182. void deleteNode(node n, btree T ){
  183. if(parrent(n, T) == -1){
  184. printf("Korjen se nemoze obrisat\n");
  185. return;
  186. }
  187.  
  188. deleteN(n,T);
  189. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.