Posted By


tomislav_matic on 01/06/11

Tagged


Statistics


Viewed 435 times
Favorited by 0 user(s)

opcenito stablo sa bibliotekom_a


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



Copy this code and paste it in your HTML
  1. #include "a_zadatak.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main(void) {
  7. tree T;
  8. cout << "Test 1 (init funkcija)" << endl;
  9. InitT(1, &T);
  10. cout << "Test 1 zavrsen" << endl;
  11.  
  12.  
  13. cout << "Test 2 (label, init, root funkcije): ispisuje 1 ako je tocno." << endl;
  14. cout << LabelT(RootT(&T), &T) << endl;;
  15. cout << "Test 2 zavrsen" << endl;
  16.  
  17. cout << "Test 3 (create ): bez ispisa" << endl;
  18. CreateT(2, RootT(&T), &T);
  19. CreateT(3, RootT(&T), &T);
  20. cout << "Test 3 zavrsen" << endl;
  21.  
  22. cout << "Test 4 (create, first child, next sibling): ispisuje 2,3" << endl;
  23. cout << LabelT(FirstChildT(RootT(&T), &T), &T) << endl;
  24. cout << LabelT(NextSiblingT(FirstChildT(RootT(&T), &T), &T), &T) << endl;
  25. cout << "Test 4 zavrsen" << endl;
  26.  
  27.  
  28. cout << "Test 5 (create): bez ispisa" << endl;
  29. CreateT(4, FirstChildT(RootT(&T), &T), &T);
  30. CreateT(5, FirstChildT(RootT(&T), &T), &T);
  31. cout << "Test 5 zavrsen" << endl;
  32.  
  33. cout << "Test 6 (change label): ispisuje \"42\"" << endl;
  34. ChangeLabelT(42, FirstChildT(RootT(&T), &T), &T);
  35. cout << LabelT(FirstChildT(RootT(&T), &T), &T) << endl;
  36. cout << "Test 6 zavrsen" << endl;
  37.  
  38. cout << "Test 7 (delete): bez ispisa" << endl;
  39. DeleteT(FirstChildT(RootT(&T), &T), &T);
  40. cout << "Test zavrsen" << endl;
  41.  
  42.  
  43. cout << "Test 8 (delete): ispisuje 3" << endl;
  44. cout << LabelT(FirstChildT(RootT(&T), &T) , &T);
  45. cout << endl;
  46.  
  47.  
  48. return 0;
  49.  
  50. }
  51.  
  52. BIBLIOTEKA A
  53.  
  54. struct elem {
  55. int label;
  56. int child,sibling;
  57. bool empty;
  58. };
  59.  
  60. struct tree {
  61. elem elements[10000];
  62. int first;
  63. };
  64.  
  65. typedef int node;
  66.  
  67. node ParentT(node n, tree* T) {
  68. for (int i = 1; i < 10000; i++) {
  69. if (T->elements[i].child == n)
  70. return i;
  71. else {
  72. if (T->elements[i].sibling == n)
  73. return ParentT(i, T);
  74. }
  75. }
  76. return 0;
  77. }
  78.  
  79. node FirstChildT(node n, tree*T) {
  80. return (T->elements[n].child);
  81. }
  82.  
  83. node NextSiblingT(node n, tree*T) {
  84. return (T->elements[n].sibling);
  85. }
  86.  
  87. int LabelT(node n, tree*T) {
  88. return (T->elements[n].label);
  89. }
  90.  
  91. node RootT(tree*T) {
  92. return (T->first);
  93. }
  94.  
  95. void CreateT(int x, node n, tree*T) {
  96.  
  97. int i;
  98. for (i = 1; i < 10000; i++) {
  99.  
  100. if (T->elements[i].empty) {
  101. T->elements[i].empty = false;
  102. T->elements[i].label = x;
  103. break;
  104. }
  105. }
  106.  
  107. node Parent = n;
  108. node sib;
  109.  
  110. if (FirstChildT(Parent, T) == 0) {
  111. T->elements[Parent].child = i;
  112. }
  113. else {
  114. sib = FirstChildT(Parent, T);
  115. while(2 < 5) {
  116. if (NextSiblingT(sib, T) != 0) {
  117. sib = NextSiblingT(sib, T);
  118. }
  119. else {
  120. break;
  121. }
  122. }
  123. T->elements[sib].sibling = i;
  124. }
  125. }
  126.  
  127. void ChangeLabelT(int x, node n, tree *T) {
  128. T->elements[n].label = x;
  129. }
  130.  
  131.  
  132. void rekurzivno_brisanje (node n, tree *T) {
  133. if (FirstChildT(n, T))
  134. rekurzivno_brisanje(FirstChildT(n, T), T);
  135. if (NextSiblingT(n, T))
  136. rekurzivno_brisanje(NextSiblingT(n, T), T);
  137. T->elements[n].empty = true;
  138. }
  139.  
  140. void DeleteT(node n, tree *T) {
  141.  
  142. rekurzivno_brisanje(FirstChildT(n, T), T);
  143.  
  144. node Parent = ParentT(n, T);
  145. node buraz;
  146.  
  147. if (FirstChildT(Parent, T) == n) {
  148. T->elements[Parent].child = NextSiblingT(n, T);
  149. }
  150.  
  151. else if (NextSiblingT(n, T)) {
  152. buraz = FirstChildT(Parent, T);
  153. while (2 > -2) {
  154. if (NextSiblingT(buraz, T)) {
  155. buraz = NextSiblingT(buraz, T);
  156. }
  157. else {
  158. break;
  159. }
  160. }
  161. T->elements[buraz].sibling = T->elements[n].sibling;
  162. }
  163.  
  164. else {
  165.  
  166. buraz = FirstChildT(Parent, T);
  167. while (2 > -2) {
  168. if (NextSiblingT(buraz, T)) {
  169. buraz = NextSiblingT(buraz, T);
  170. }
  171. else {
  172. break;
  173. }
  174. }
  175.  
  176. T->elements[buraz].sibling = 0;
  177. }
  178. }
  179.  
  180. void InitT(int x, tree *T) {
  181. for (int i = 2; i < 10000; i++) {
  182. T->elements[i].empty = true;
  183. T->elements[i].child = 0;
  184. T->elements[i].sibling = 0;
  185. }
  186. T->first = 1;
  187. T->elements[1].empty = false;
  188. T->elements[1].label = x;
  189. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.