Posted By

lumikolaj on 01/06/11


Tagged


Versions (?)

Z4 lumikolaj


 / Published in: C++
 

  1. // opcenito stablo
  2.  
  3. struct elem{
  4. int label;
  5. int firstchild,nextsibling;
  6. };
  7. struct tr{
  8. elem elements[10000];
  9. int first;
  10. };
  11. int ParentT(int n, tr *tree){
  12. if(n>0)return (n-1);
  13. else {
  14. if(n==0)return -1;
  15. else cout << "Cvor " << n << " ne postoji u stablu." << endl;
  16. }
  17. }
  18.  
  19. int FirstChildT(int n,tr *tree){
  20. if(n<0) cout << "Cvor " << n << " ne postoji u stablu." << endl;
  21. else{
  22. if(n>=0 && ((n+1)!=0))return tree->elements[n].firstchild;
  23. else return -1;
  24. }
  25. }
  26.  
  27. int NextSiblingT(int n,tr *tree){
  28. if(n<0)cout <<"Cvor " << n << " ne postoji u stablu." << endl;
  29. else{
  30. if(n>=0 && (tree->elements[n].nextsibling!=-1))return tree->elements[n].nextsibling;
  31. else cout << "Cvor " << n << " je najdesnije dijete." << endl;
  32. }
  33. }
  34. int LabelT(int n,tr *tree){
  35. if(n<0)cout << "Cvor " << n << " ne postoji." << endl;
  36. else{
  37. if(tree->elements[n].label==-1) cout << "Cvor ne postoji!" << endl;
  38. else return tree->elements[n].label;
  39. }
  40. }
  41. int RootT(tr *tree){
  42. return tree->elements[0].label;
  43. }
  44. int CreateT(int x,int n,tr *tree){
  45. tree->elements[tree->first].label=n;
  46. tree->elements[tree->first].firstchild=x;
  47. tree->first++;
  48. }
  49. int ChangeLabelT(int x,int n,tr *tree){
  50. if(n<0)cout << "Cvor ne postoji!" << endl;
  51. else tree->elements[n].label=x;
  52. }
  53. int DeleteT(int n,tr *tree){
  54. if(n<0) cout << "Cvor ne postoji u stablu." << endl;
  55. else{
  56. if(tree->elements[n].firstchild>0)DeleteT(tree->elements[n].firstchild,tree);
  57. else{
  58. tree->elements[n].label=-1;
  59. tree->first--;}
  60. }
  61. }
  62. int InitT(int x,tr *tree){
  63. delete [] tree->elements;
  64. tree->first=0;
  65. tree->elements[tree->first].label=x;
  66. tree->first++;
  67. }
  68.  
  69.  
  70. // binarno stablo pomocu polja
  71.  
  72. struct element{
  73. int label;
  74. int used;
  75. };
  76. struct tr{
  77. element elements[10000];
  78. };
  79.  
  80. int ParentT(int n,tr *tree){
  81. if(n==1) return -1;
  82. else{
  83. if(!(n%2))return (n/2);
  84. else return ((n-1)/2);
  85. }
  86. }
  87.  
  88. int LeftChildT(int n,tr *tree){
  89. if(tree->elements[2*n].used) return 2*n;
  90. else cout << "Cvor " << n << " nema lijevo dijete." << endl;
  91. }
  92.  
  93. int RightChildT(int n,tr *tree){
  94. if(tree->elements[(2*n)+1].used) return ((2*n)+1);
  95. else cout << "Cvor " << n << " nema desno dijete." << endl;
  96. }
  97. int LabelT(int n,tr *tree){
  98. if(tree->elements[n].used) return tree->elements[n].label;
  99. else cout << "Cvor ne postoji." << endl;
  100. }
  101.  
  102. int ChangeLabelT(int x, int n, tr *tree){
  103. if(tree->elements[n].used) tree->elements[n].label=x;
  104. else cout << "Cvor ne postoji u stablu." << endl;
  105. }
  106.  
  107. int RootT(tr *tree){
  108. if(tree->elements[0].used) return 1;
  109. else cout << "Stablo nema korijen, ne postoji!" << endl;
  110. }
  111.  
  112. int CreateLeftT(int x, int n, tr *tree){
  113. if(tree->elements[2*n].used==1) cout << "Cvor je vec popunjen" << endl;
  114. else {
  115. tree->elements[2*n].label=x;
  116. tree->elements[2*n].used=1;
  117. }
  118. }
  119.  
  120. int CreateRightT(int x, int n, tr *tree){
  121. if(tree->elements[(2*n)+1].used)cout << "Cvor vec ima desno dijete" << endl;
  122. else{
  123. tree->elements[(2*n)+1].label=x;
  124. tree->elements[(2*n)+1].used=1;
  125. }
  126. }
  127. int ExistsLeftChild(int n,tr *tree){
  128. if(tree->elements[n*2].used) return 1;
  129. else return -1;
  130. }
  131. int ExistsRightChild(int n, tr *tree){
  132. if(tree->elements[(2*n)+1].used) return 1;
  133. else return -1;
  134. }
  135.  
  136. int DeleteT(int n, tr *tree){
  137. int brl_cvora=1;
  138. int brd_cvora=1;
  139. if(tree->elements[n].used){
  140.  
  141. while(ExistsLeftChild(brl_cvora,tree)==1) brl_cvora=2*brl_cvora;
  142. for(brl_cvora;brl_cvora>n;brl_cvora/2)tree->elements[brl_cvora].used=0;
  143. while(ExistsRightChild(brd_cvora,tree)==1) brd_cvora=(brd_cvora*2)+1;
  144. for(brd_cvora;brd_cvora>n;(brd_cvora-1)/2) tree->elements[brd_cvora].used=0;
  145. }
  146. tree->elements[n].used=0;
  147. }
  148. int InitT(int x, tr *tree){
  149. if(tree->elements[0].used)
  150. for(int i=9999;i>=0;i--){
  151. tree->elements[i].used=0;
  152. }
  153. tree->elements[0].label=x;
  154. tree->elements[0].used=1;
  155. }
  156.  
  157. // binarno stablo pomocu pokazivaca
  158.  
  159. struct element{
  160. int label;
  161. element *left,*right;
  162. };
  163. typedef struct element *elem;
  164.  
  165. elem ParentT(int n, element *tree){
  166. element *tekucil=tree->left;
  167. element *tekucid=tree->right;
  168. bool nadjen =false;
  169. while(tekucil){
  170. if(tekucil->label==n) return tekucil;
  171. tekucil=tekucil->left;
  172. }
  173. if(!tekucil){
  174. while(tekucid){
  175. if(tekucid->label==n) return tekucid;
  176. tekucid=tekucid->right;
  177. }
  178. }
  179. if(!tekucid && !tekucil) cout << "Cvor ne postoji u stablu." << endl;
  180. }
  181.  
  182. elem LeftChildT(element *n, element *tree){
  183. return n->left;
  184. }
  185. elem RightChildT(element *n, element *tree){
  186. return n->right;
  187. }
  188. int LabelT(element *n, element *tree){
  189. return n->label;
  190. }
  191. int ChangeLabelT(int x, element *n, element *tree){
  192. n->label=x;
  193. }
  194. elem RootT(element *tree){
  195. return tree;
  196. }
  197. elem CreateLeftT(int x, element *n, element *tree){
  198. if(n->left==NULL){
  199. element *novi;
  200. novi=new element;
  201. n->left=novi;
  202. novi->left=NULL;
  203. novi->right=NULL;
  204. novi->label=x;
  205. }
  206. else cout << "Lijevi cvor je vec popunjen." << endl;
  207. }
  208. elem CreateRightT(int x,element *n, element *tree){
  209. if(n->right==NULL){
  210. element *novi;
  211. novi=new element;
  212. n->right=novi;
  213. novi->left=NULL;
  214. novi->right=NULL;
  215. novi->label=x;
  216. }
  217. else cout << "Desni cvor je vec popunjen." << endl;
  218. }
  219. elem DeleteT(element *n, element *tree){
  220. element *tekucil;
  221. tekucil=n->left;
  222. while(tekucil){
  223. while(tekucil->left!=NULL)tekucil=tekucil->left;
  224. delete tekucil;
  225. while(tekucil->right!=NULL)tekucil=tekucil->right;
  226. delete tekucil;
  227. tekucil=n->left;
  228. }
  229. element *tekucid;
  230. tekucid=n->right;
  231. while(tekucid){
  232. while(tekucid->left!=NULL)tekucid=tekucid->left;
  233. delete tekucid;
  234. while(tekucid->right!=NULL)tekucid=tekucid->right;
  235. delete tekucid;
  236. tekucid=n->right;
  237. }
  238. delete n;
  239. }
  240.  
  241. elem InitT(int x, element *tree){
  242. element *tekucil;
  243. tekucil=tree->left;
  244. while(tekucil){
  245. while(tekucil->left!=NULL)tekucil=tekucil->left;
  246. delete tekucil;
  247. while(tekucil->right!=NULL)tekucil=tekucil->right;
  248. delete tekucil;
  249. tekucil=tree->left;
  250. }
  251. element *tekucid;
  252. tekucid=tree->right;
  253. while(tekucid){
  254. while(tekucid->left!=NULL)tekucid=tekucid->left;
  255. delete tekucid;
  256. while(tekucid->right!=NULL)tekucid=tekucid->right;
  257. delete tekucid;
  258. tekucid=tree->right;
  259. }
  260. delete tree;
  261. element *novi=new element;
  262. novi->left=NULL;
  263. novi->right=NULL;
  264. novi->label=x;
  265. cout << "Inicijalizacija je izvrsena." << endl;
  266. }

Report this snippet  

You need to login to post a comment.