Posted By

sara on 01/05/11


Tagged

sp btree zadatak4


Versions (?)

bstablo_polje.h


 / Published in: C++
 

  1. #include <stdlib.h>
  2. #define ARRAYSIZE 1000000
  3. #define Null -1
  4. typedef int labeltype;
  5.  
  6. struct element {
  7. labeltype label;
  8. int used;
  9. };
  10.  
  11. typedef struct bt {
  12. struct element elements[ARRAYSIZE];
  13. }*btree;
  14.  
  15. typedef int node;
  16.  
  17. btree InitB (labeltype x){
  18. btree myBTree;
  19. int i;
  20.  
  21. myBTree=(btree)malloc(sizeof(struct bt));
  22. for(i=1; i<ARRAYSIZE; i++){
  23. if(i==1){
  24. myBTree->elements[i].label=x;
  25. myBTree->elements[i].used=1;
  26. }else{
  27. myBTree->elements[i].used=0;
  28. }
  29. }
  30.  
  31. return myBTree;
  32. }
  33.  
  34. node RootB(btree T){
  35. return 1;
  36. }
  37.  
  38. node ParrentB(node n, btree T){
  39. if(n==1){
  40. printf("Zatrazili ste roditelja korjenskog cvora\n");
  41. return Null;
  42. }
  43.  
  44. if(T->elements[n].used==0){
  45. printf("Ne postoji taj cvor u stablu\n");
  46. exit(1);
  47. }
  48.  
  49. return (n/2);
  50. }
  51.  
  52. node LeftChildB(node n, btree T){
  53. node child;
  54. child=n*2;
  55. if (child > ARRAYSIZE-1){
  56. printf("\nPolje nije toliko veliko\n");
  57. return Null;
  58. //exit(1);
  59. }
  60.  
  61. if(T->elements[child].used==0){
  62. return Null;
  63. }else{
  64. return child;
  65. }
  66. }
  67.  
  68. node RightChildB(node n, btree T){
  69. node child;
  70. child=(n*2)+1;
  71. if (child > ARRAYSIZE-1){
  72. printf("\nPolje nije toliko veliko\n");
  73. return Null;
  74. //exit(1);
  75. }
  76.  
  77. if(T->elements[child].used==0){
  78. return Null;
  79. }else{
  80. return child;
  81. }
  82. }
  83.  
  84. labeltype LabelB(node n, btree T){
  85. if(T->elements[n].used==0){
  86. printf("Nepostojeci cvor u stablu\n");
  87. exit(1);
  88. }else{
  89. return T->elements[n].label;
  90. }
  91. }
  92.  
  93. void ChangeLabelB(labeltype x, node n, btree T){
  94. if(T->elements[n].used==0){
  95. printf("Nepostojeci cvor u stablu\n");
  96. exit(1);
  97. }else{
  98. T->elements[n].label=x;
  99. }
  100. }
  101.  
  102. void CreateLeftB(labeltype x, node n, btree T){
  103. node child;
  104. child=n*2;
  105. if (child > ARRAYSIZE-1){
  106. printf("\nPolje nije toliko veliko\n");
  107. return;
  108. //exit(1);
  109. }
  110.  
  111. if(T->elements[child].used==1){
  112. printf("Lijevo dijete ovog cvora vec postoji\n");
  113. exit(1);
  114. }else{
  115. T->elements[child].used=1;
  116. T->elements[child].label=x;
  117. }
  118. }
  119.  
  120. void CreateRightB(labeltype x, node n, btree T){
  121. node child;
  122. child=(n*2)+1;
  123. if (child > ARRAYSIZE-1){
  124. printf("\nPolje nije toliko veliko\n");
  125. return;
  126. //exit(1);
  127. }
  128.  
  129. if(T->elements[child].used==1){
  130. printf("Desno dijete ovog cvora vec postoji\n");
  131. exit(1);
  132. }else{
  133. T->elements[child].used=1;
  134. T->elements[child].label=x;
  135. }
  136. }
  137.  
  138. void DeleteBRek(node n, btree T){
  139. if(n==Null)
  140. return;
  141.  
  142. DeleteBRek(LeftChildB(n, T), T);
  143.  
  144. T->elements[n].used=0;
  145.  
  146. DeleteBRek(RightChildB(n, T), T);
  147. }
  148.  
  149. void DeleteB(node n, btree T ){
  150. if(ParrentB(n, T)==Null){
  151. printf("Ne mogu obrisati korjen\n");
  152. exit(1);
  153. }
  154.  
  155. DeleteBRek(n,T);
  156. }

Report this snippet  

You need to login to post a comment.