Posted By

mjug on 01/20/15


Tagged

implementacija stablo opcenito


Versions (?)

opcenito stablo.h


 / Published in: C++
 

opcenito stablo

  1. #include <iostream>
  2. using namespace std;
  3. #define MAX_elem 100000
  4.  
  5. typedef int labeltype;
  6.  
  7. struct elem {
  8. labeltype label;
  9. int firstchild, nextsibling; };
  10.  
  11. struct tr {
  12. elem eleme[MAX_elem];
  13. int first; };
  14.  
  15. void IniT (int x, tr *s) {
  16. for (int i = 0; i < MAX_elem; i++) {
  17. s->eleme[i].firstchild = -1;
  18. s->eleme[i].nextsibling = -1;
  19. s->eleme[i].label = -1; }
  20. s->first = x;
  21. cout << "Upisite oznaku korijena: " << endl;
  22. cin >> s->eleme[x].label;
  23.  
  24. }
  25.  
  26. int RootT (tr *s) {
  27. return s->first; }
  28.  
  29. void CreateT (int x, int n, tr *s) { // x dijete cvora n
  30. //Provjerava da li cvor uopce i postoji
  31. if (s->eleme[n].label == -1) {
  32. cout << "Cvor ne postoji!" << endl; }
  33.  
  34. // Ako evor nema dijece dodaj mu djete
  35. else if (s->eleme[n].firstchild == -1) {
  36. s->eleme[n].firstchild = x;
  37. cout << "Oznaka: ";
  38. cin >> s->eleme[x].label; }
  39.  
  40. //ako evor ima dijete
  41. else {
  42. int brat;
  43. bool provjera = true;
  44.  
  45. brat = s->eleme[n].firstchild;
  46. do {
  47. if (s->eleme[brat].nextsibling == -1) {
  48. provjera = false; }
  49.  
  50. else {
  51. brat = s->eleme[brat].nextsibling; } }
  52. while (provjera);
  53.  
  54. s->eleme[brat].nextsibling = x;
  55. cout << "Oznaka: ";
  56. cin >> s->eleme[x].label; } }
  57.  
  58. int FirstChildT (int x, tr *s) {
  59. return s->eleme[x].firstchild; }
  60.  
  61. int NextSiblingT (int n, tr *s) {
  62. return s->eleme[n].nextsibling; }
  63.  
  64. int ParentT (int n, tr *s) {
  65. for (int i = 0; i < MAX_elem; i++) {
  66. if (s->eleme[i].firstchild == n) {
  67. return i; }
  68. else if (s->eleme[i].nextsibling == n) {
  69. return ParentT (i, s); } }
  70. return -1; }
  71.  
  72. int LabelT (int n, tr *s) {
  73. if (s->eleme[n].label == -1) {
  74. return -1; }
  75. else {
  76. return s->eleme[n].label; } }
  77. void ChangeLabelT (int x, int n, tr *s) {
  78. if (s->eleme[n].label == -1) {
  79. cout << "Cvor ne postoji!" << endl;
  80. return; }
  81.  
  82. s->eleme[n].label = x; }
  83.  
  84. void Delete (int n, tr *s) {
  85. if (s->eleme[n].nextsibling == -1 && s->eleme[ParentT (n, s)].firstchild == n) {
  86. s->eleme[ParentT (n, s)].firstchild = -1;
  87. s->eleme[ParentT (n, s)].label = -1;
  88. s->eleme[n].label = -1; }
  89. else if (s->eleme[n].nextsibling != -1 && s->eleme[ParentT (n, s)].firstchild == n) {
  90. s->eleme[ParentT (n, s)].firstchild = s->eleme[n].nextsibling; }
  91. else {
  92. int brat = s->eleme[ParentT (n, s)].firstchild;
  93. while (s->eleme[brat].nextsibling != n) {
  94. brat = s->eleme[brat].nextsibling; }
  95. s->eleme[brat].nextsibling = s->eleme[n].firstchild; } }
  96.  
  97. void DeleteT (int n, tr *s) {
  98. if (s->eleme[n].label == -1) {
  99. cout << "Cvor ne postoji!" << endl; }
  100.  
  101. else if (s->eleme[n].firstchild != -1) {
  102. while (s->eleme[n].firstchild != -1) {
  103. Delete (s->eleme[n].firstchild, s); }
  104. Delete (n, s); }
  105. else {
  106. Delete (n, s); } }

Report this snippet  

You need to login to post a comment.