Posted By

tturek on 01/03/14


Tagged

Strukture


Versions (?)

prvo dijete - sljedeci brat


 / Published in: C++
 

etuu

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct element{
  5. char label;
  6. int dijete,brat;
  7. };
  8.  
  9. struct tree{
  10. element polje[1000];
  11. int korijen;
  12. };
  13.  
  14. tree *InitT(int k,tree *T){
  15. T = new tree;
  16. for(int i=0;i<1000;i++){
  17. T->polje[i].label = '0';
  18. T->polje[i].dijete = T->polje[i].brat = -1;
  19. }
  20. T->polje[k].label = 'A';
  21. T->korijen = k;
  22. return T;
  23. }
  24.  
  25. void ChangeLabelT(char x,int n,tree *T){
  26. T->polje[n].label = x;
  27. }
  28.  
  29. int RootT(tree *T){
  30. return T->korijen;
  31. }
  32.  
  33. char LabelT(int n,tree *T){
  34. return T->polje[n].label;
  35. }
  36.  
  37. void CreateT(int x,int n,tree *T){
  38. if(T->polje[n].label=='0') cout << "Ne postoji cvor " << n << " !" << endl;
  39. else{
  40. if(T->polje[n].dijete==-1) T->polje[n].dijete = x;
  41. else if(T->polje[T->polje[n].dijete].brat==-1) T->polje[T->polje[n].dijete].brat = x;
  42. else{
  43. n = T->polje[n].dijete;
  44. while(T->polje[n].brat!=-1) n = T->polje[n].brat;
  45. T->polje[n].brat = x;
  46. }
  47. T->polje[x].label = T->polje[n].label+1;
  48. T->polje[x].dijete = T->polje[x].brat = -1;
  49. }
  50. }
  51.  
  52. int FirstChildT(int n,tree *T){
  53. return T->polje[n].dijete;
  54. }
  55.  
  56. int NextSiblingT(int n,tree *T){
  57. return T->polje[n].brat;
  58. }
  59.  
  60. int ParentT(int n,tree *T){
  61. for(int i=0;i<1000;i++){
  62. if(T->polje[i].dijete==n) return i;
  63. if(T->polje[i].brat==n) return ParentT(i,T);
  64. }
  65. }
  66.  
  67. void DeleteT(int n,tree *T){
  68. if(T->polje[n].dijete!=-1) DeleteT(T->polje[n].dijete,T);
  69. if(T->polje[n].brat!=-1) DeleteT(T->polje[n].brat,T);
  70. T->polje[n].dijete = T->polje[n].brat = -1;
  71. T->polje[n].label = '0';
  72. if(T->polje[ParentT(n,T)].brat!=-1) T->polje[ParentT(n,T)].dijete = T->polje[ParentT(n,T)].brat;
  73. else T->polje[ParentT(n,T)].dijete = -1;
  74. }

Report this snippet  

You need to login to post a comment.