Posted By


hcosic2 on 01/18/15

Tagged


Statistics


Viewed 639 times
Favorited by 0 user(s)

Related snippets


Opcenito_stablo


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

Implementacija općenitog stabla


Copy this code and paste it in your HTML
  1. #include <iostream>
  2. #define max 1000
  3. using namespace std;
  4.  
  5. struct elem
  6. {
  7. int label, First_Child, Next_Sibling;
  8. };
  9. struct stablo
  10. {
  11. elem polje[max];
  12. int korijen;
  13. };
  14.  
  15. stablo T;
  16.  
  17. int FirstChildT (int n, stablo T)
  18. {
  19. return T.polje[n].First_Child;
  20. };
  21.  
  22. int NextSiblingT (int n, stablo T)
  23. {
  24. return T.polje[n].Next_Sibling;
  25. };
  26.  
  27. int ParentT (int n, stablo T)
  28. {
  29. for (int I = 0; I < max; I++)
  30. if (FirstChildT (I, T) == n)
  31. return I;
  32. else if (NextSiblingT (I, T) == n)
  33. return ParentT (I, T);
  34. return -1;
  35. };
  36.  
  37. int LabelT (int n, stablo T)
  38. {
  39. return T.polje[n].label;
  40. };
  41.  
  42. int RootT (stablo T)
  43. {
  44. return T.korijen;
  45. };
  46.  
  47. int CreateT (int x, int n, stablo &T)
  48. {
  49. int temp = 1;
  50. while (LabelT (temp, T) != -1)
  51. temp++;
  52. T.polje[temp].label = x;
  53. T.polje[temp].First_Child = -1;
  54. T.polje[temp].Next_Sibling = -1;
  55. if (T.polje[n].First_Child != -1)
  56. {
  57. n = FirstChildT (n, T);
  58. while (NextSiblingT (n, T) != -1)
  59. n = NextSiblingT (n, T);
  60. T.polje[n].Next_Sibling = temp;
  61. }
  62. else T.polje[n].First_Child = temp;
  63. return temp;
  64. };
  65.  
  66. void ChangeLabelT (int x, int n, stablo &T)
  67. {
  68. T.polje[n].label = x;
  69. };
  70.  
  71. void DeleteT (int n, stablo &T)
  72. {
  73. int temp;
  74. if (FirstChildT (n, T) != -1)
  75. {
  76. temp = FirstChildT (n, T);
  77. DeleteT (temp, T);
  78. while (NextSiblingT (temp, T) != -1)
  79. {
  80. temp = NextSiblingT (temp, T);
  81. DeleteT (temp, T);
  82. }
  83. }
  84. T.polje[n].label = -1;
  85. if (n != RootT (T) && FirstChildT (ParentT (n, T), T) == n)
  86. T.polje[ParentT (n, T)].First_Child = NextSiblingT (n, T);
  87. else if (n != RootT (T))
  88. {
  89. temp = FirstChildT (ParentT (n, T), T);
  90. while (NextSiblingT (temp, T) != n)
  91. temp = NextSiblingT (temp, T);
  92. T.polje[temp].Next_Sibling = NextSiblingT (n, T);
  93. }
  94. };
  95.  
  96. void InitT (int x, stablo &T)
  97. {
  98. T.korijen = 1;
  99. T.polje[1].label = x;
  100. T.polje[1].First_Child = -1;
  101. T.polje[1].Next_Sibling = -1;
  102. T.polje[0].label = -1;
  103. T.polje[0].First_Child = -1;
  104. T.polje[0].Next_Sibling = -1;
  105. for (int I = 2; I < max; I++)
  106. {
  107. T.polje[I].label = -1;
  108. T.polje[I].First_Child = -1;
  109. T.polje[I].Next_Sibling = -1;
  110. }
  111. }
  112.  
  113. void PreOrderT (int temp)
  114. {
  115. cout << LabelT (temp, T) << ", ";
  116. if (FirstChildT (temp, T) != -1)
  117. PreOrderT (FirstChildT (temp, T));
  118. if (NextSiblingT (temp, T) != -1)
  119. PreOrderT (NextSiblingT (temp, T));
  120. }
  121.  
  122. void InOrderT (int temp)
  123. {
  124. if (FirstChildT (temp, T) != -1)
  125. InOrderT (FirstChildT (temp, T));
  126. cout << LabelT (temp, T) << ", ";
  127. if (FirstChildT (temp, T) != -1)
  128. {
  129. temp = FirstChildT (temp, T);
  130. while (NextSiblingT (temp, T) != -1)
  131. temp = NextSiblingT (temp, T), InOrderT (temp);
  132. }
  133. }
  134.  
  135. void PostOrderT (int temp)
  136. {
  137. if (FirstChildT (temp, T) != -1)
  138. PostOrderT (FirstChildT (temp, T));
  139. int pom = temp;
  140. if (FirstChildT (pom, T) != -1)
  141. {
  142. pom = FirstChildT (pom, T);
  143. while (NextSiblingT (pom, T) != -1)
  144. pom = NextSiblingT (pom, T), PostOrderT (pom);
  145. }
  146. cout << LabelT (temp, T) << ", ";
  147. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.