Posted By


igotepava on 01/18/15

Tagged


Statistics


Viewed 841 times
Favorited by 0 user(s)

opcenito_stablo.h


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

Implementacija općenitog stabla pomoću polja.


Copy this code and paste it in your HTML
  1. struct elem{
  2. char label;
  3. int firstC, nextS;
  4. };
  5.  
  6. struct tr{
  7. elem elements[10000];
  8. int first;
  9. };
  10.  
  11. tr *InitT(int x, tr *T){
  12. T = new tr;
  13. for(int i=0; i<10000; i++){
  14. T->elements[i].label = '0';
  15. T->elements[i].firstC = -1;
  16. T->elements[i].nextS = -1;
  17. }
  18. T->elements[x].label = 'A';
  19. T->first = x;
  20. return T;
  21. }
  22.  
  23. int RootT(tr *T){
  24. return T->first;
  25. }
  26.  
  27. int ParentT(int n, tr *T){
  28. if(RootT(T) == n) return -1;
  29. for(int i=0; i<10000; i++){
  30. if(T->elements[i].firstC == n) return i;
  31. }
  32. }
  33.  
  34. int FirstChildT(int n, tr *T){
  35. return T->elements[n].firstC;
  36. }
  37.  
  38. int NextSiblingT(int n, tr *T){
  39. return T->elements[n].nextS;
  40. }
  41.  
  42. char LabelT(int n, tr *T){
  43. return T->elements[n].label;
  44. }
  45.  
  46. void ChangeLabelT(char x, int n, tr *T){
  47. T->elements[n].label = x;
  48. }
  49.  
  50. void CreateT(int x, int n, tr *T){
  51. if(LabelT(x, T) != '0'){
  52. cout << "Vrijednost vec postoji!\n\n";
  53. return;
  54. }
  55. if(LabelT(n, T) == '0'){
  56. cout << "Cvor ne postoji!\n\n";
  57. return;
  58. }
  59. if(FirstChildT(n, T) == -1) T->elements[n].firstC = x;
  60. else if(NextSiblingT(FirstChildT(n, T), T) == -1) T->elements[FirstChildT(n, T)].nextS = x;
  61. else{
  62. n = FirstChildT(n, T);
  63. while(NextSiblingT(n, T) != -1) n = NextSiblingT(n, T);
  64. T->elements[n].nextS = x;
  65. }
  66. T->elements[x].firstC = -1;
  67. T->elements[x].nextS = -1;
  68. T->elements[x].label = T->elements[n].label+1;
  69. }
  70.  
  71. void DeleteT(int n, tr *T){
  72. if(FirstChildT(n, T) != -1) DeleteT(FirstChildT(n, T), T);
  73. if(NextSiblingT(n, T) != -1) DeleteT(NextSiblingT(n, T), T);
  74. T->elements[n].label = '0';
  75. T->elements[n].firstC = -1;
  76. T->elements[n].nextS = -1;
  77. if(NextSiblingT(ParentT(n, T), T) != -1) T->elements[ParentT(n, T)].firstC = NextSiblingT(ParentT(n, T), T);
  78. else T->elements[ParentT(n, T)].firstC = -1;
  79. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.