Guest User

Untitled

a guest
Jul 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. typedef char TIP;
  2. struct elem
  3. {
  4. TIP vrij;
  5. int c, b;
  6. }; //struct elem
  7.  
  8. class tr
  9. {
  10. public:
  11. elem el[1000];
  12. int R;
  13. bool o;
  14.  
  15. tr()
  16. {
  17. //potrebno za InitT(x, T)
  18. o = false;
  19. }
  20. }; //class tr
  21.  
  22.  
  23. int FirstChildT(int n, tr T)
  24. {
  25. return T.el[n].c;
  26. } //int FirstChildT(int n, tr T)
  27.  
  28.  
  29. int NextSiblingT(int n, tr T)
  30. {
  31. return T.el[n].b;
  32. } //int NextSiblingT(int n, tr T)
  33.  
  34.  
  35. void parentPomT(int &rod, int n, int r, int d, tr T)
  36. {
  37. if(rod != -1) return; //ako je rod drugaciji, znaci da je rod pronadjen pa prekini rekurzije
  38. if(d == n)
  39. {
  40. rod = r;
  41. return;
  42. } //if(d == n)
  43.  
  44. if(FirstChildT(d, T) != -1) parentPomT(rod, n, d, FirstChildT(d, T), T);
  45. if(rod != -1) return; //ako je rod drugaciji, znaci da je rod pronadjen pa prekini rekurzije
  46. if(NextSiblingT(d, T) != -1) parentPomT(rod, n, r, NextSiblingT(d, T), T);
  47. } //void parentPomT(int &rod, int n, int r, int d, tr T)
  48.  
  49.  
  50. int ParentT(int n, tr T)
  51. {
  52. if(T.R == n || FirstChildT(T.R, T) == -1) return -1; //ako je n cvor ili ako stablo ima samo cvor, javi gresku
  53. int rod = -1;
  54. parentPomT(rod, n, T.R, FirstChildT(T.R, T), T);
  55.  
  56. return rod;
  57. } //int ParentT(int n, tr T)
  58.  
  59.  
  60. TIP LabelT(int n, tr T)
  61. {
  62. return T.el[n].vrij;
  63. } //TIP LabelT(int n, tr T)
  64.  
  65.  
  66. int RootT(tr T)
  67. {
  68. return T.R;
  69. } //int RootT(tr T)
  70.  
  71.  
  72. void CreateT(TIP x, int n, tr &T)
  73. {
  74. int pm = -1;
  75. for(int i = 0; pm == -1; i++) //treba naci prazno mjesto u polju
  76. if(T.el[i].vrij == '\0') pm = i;
  77.  
  78. T.el[pm].vrij = x;
  79. T.el[pm].c = -1;
  80. T.el[pm].b = -1;
  81.  
  82. if(FirstChildT(n, T) == -1) T.el[n].c = pm; //ako n nema djece, posao je kraci
  83. else //ako ima... treba naci trenutno najmladje djete i reci mu da ima bracu
  84. {
  85. int t = FirstChildT(n, T);
  86. while(NextSiblingT(t, T) != -1) t = NextSiblingT(t, T);
  87. T.el[t].b = pm;
  88. } //if(FirstChildT(n, T) == -1) : else
  89. } //void CreateT(elem x, int n, tr &T)
  90.  
  91.  
  92. void ChangeLabelT(TIP x, int n, tr &T)
  93. {
  94. T.el[n].vrij = x;
  95. } //void ChangeLabelT(TIP x, int n, tr &T)
  96.  
  97.  
  98. void DeletePomT(int n, tr &T)
  99. {
  100. if(FirstChildT(n, T) != -1) DeletePomT(FirstChildT(n, T), T);
  101. if(NextSiblingT(n, T) != -1) DeletePomT(NextSiblingT(n, T), T);
  102. T.el[n].vrij = '\0';
  103. T.el[n].c = -1;
  104. T.el[n].b = -1;
  105. } //void DeletePomT(int n, tr &T)
  106.  
  107.  
  108. void DeleteT(int n, tr &T)
  109. {
  110. //treba znati roditelja kako bi se braca mogla urediti
  111. int rod = -1;
  112. rod = ParentT(n, T);
  113.  
  114. if(rod != -1)
  115. {
  116. //ako je zrtva prvo djete, onda samo postavimo adresu brata na kursor c
  117. //inace treba naci starijeg brata i dati mu adresu brata brisanog elementa
  118. if(FirstChildT(rod, T) == n) T.el[rod].c = NextSiblingT(n, T);
  119. else
  120. {
  121. int t = FirstChildT(rod, T);
  122. while(NextSiblingT(t, T) != n) t = NextSiblingT(t, T);
  123. T.el[t].b = NextSiblingT(n, T);
  124. } //if(FirstChildT(rod, T) == n) : else
  125. } //if(rod != -1)
  126.  
  127. if(FirstChildT(n, T) != -1) DeletePomT(FirstChildT(n, T), T);
  128. T.el[n].vrij = '\0';
  129. T.el[n].c = -1;
  130. T.el[n].b = -1;
  131. } //void DeleteT(int n, tr &T)
  132.  
  133. void InitT(int x, tr &T)
  134. {
  135. if(T.o) DeleteT(T.R, T); //brisati stablo samo ako stablo nije prije deklarirano
  136. T.R = x;
  137. T.o = true;
  138. } //void InitT(int x, tr &T)
Add Comment
Please, Sign In to add comment