Posted By

javert on 01/19/15


Tagged

sp


Versions (?)

tree_array.h


 / Published in: C++
 

Implementacija opcenitog stabla, polje

  1. struct oelement {
  2. int value;
  3. int fc, ns;
  4. };
  5. struct otree {
  6. oelement el[10000];
  7. int korijen;
  8. };
  9. typedef otree* oTree;
  10. typedef int onode;
  11.  
  12.  
  13. onode FirstChildT(onode n, oTree T) {
  14. return T->el[n].fc;
  15. }
  16.  
  17. onode NextSiblingT(onode n, oTree T) {
  18. return T->el[n].ns;
  19. }
  20.  
  21. int LabelT(onode n, oTree T) {
  22. return T->el[n].value;
  23. }
  24.  
  25. void ChangeLabelT(int x, onode n, oTree T) {
  26. T->el[n].value = x;
  27. }
  28.  
  29. onode RootT(oTree T) {
  30. return T->korijen;
  31. }
  32.  
  33.  
  34. onode ParentT(onode n, oTree T) {
  35. for(int m=0; m<10000; m++) {
  36.  
  37. if(n == T->el[m].fc) return m;
  38.  
  39. if(n == T->el[m].ns) return ParentT(m, T);
  40. }
  41.  
  42. return -1;
  43. }
  44.  
  45.  
  46. void CreateT(onode x, onode n, oTree T) {
  47. if(T->el[x].value!=0) return;
  48. T->el[x].fc = -1;
  49. T->el[x].ns = -1;
  50. T->el[x].value = rand() % 99+1;
  51.  
  52. if(T->el[n].fc==-1) {
  53. T->el[n].fc = x;
  54. return;
  55. }
  56. n = FirstChildT(n, T);
  57. while(NextSiblingT(n, T)!=-1) n=NextSiblingT(n, T);
  58. T->el[n].ns = x;
  59. }
  60.  
  61.  
  62.  
  63.  
  64. oTree InitT(onode x, oTree T) {
  65. T = new otree;
  66. for(int i=0; i<10000; i++) {
  67. T->el[i].fc = -1;
  68. T->el[i].ns = -1;
  69. T->el[i].value = 0;
  70. }
  71. T->korijen = x;
  72. return T;
  73. }
  74.  
  75.  
  76.  
  77. void DeleteT(onode n, oTree T, bool m=1) {
  78. if(m) {
  79. for(int i=0; i<10000; i++) {
  80. if(T->el[i].fc==n) {
  81. T->el[i].fc = NextSiblingT(n, T);
  82. T->el[n].ns = -1;
  83. break;
  84. }
  85. else if(T->el[i].ns==n) {
  86. T->el[i].ns = NextSiblingT(n, T);
  87. T->el[n].ns = -1;
  88. break;
  89. }
  90. }
  91. DeleteT(n, T, 0);
  92. return;
  93. }
  94. if(FirstChildT(n, T)!=-1) DeleteT(FirstChildT(n, T), T, 0);
  95. for(onode n2=NextSiblingT(n, T); n2!=-1; n2=NextSiblingT(n2, T) )
  96. DeleteT(n2, T, 0);
  97. T->el[n].value = 0;
  98. T->el[n].ns = -1;
  99. T->el[n].fc = -1;
  100. }

Report this snippet  

You need to login to post a comment.