Posted By

kapocrnic on 01/06/11


Tagged

sp


Versions (?)

a zadatak


 / Published in: C++
 

  1. #include <string>
  2. #include <iostream>
  3.  
  4. struct element {
  5. std::string label;
  6. int fc,ns;
  7. bool used;
  8. element() {
  9. this->fc = this->ns = 0;
  10. this->used = false;
  11. }
  12. };
  13.  
  14. struct tree {
  15. element elements[10000];
  16. int first;
  17. tree() {
  18. this->first = 0;
  19. }
  20. };
  21.  
  22. int FirstChildT(int n, tree *T) {
  23. return T->elements[n].fc;
  24. }
  25.  
  26. int NextSiblingT(int n, tree *T) { return T->elements[n].ns;}
  27.  
  28. std::string LabelT(int n, tree *T) { return T->elements[n].label; }
  29.  
  30. int RootT(tree * T) { return 1;}
  31.  
  32. int CreateT(std::string x, int n, tree *T) {
  33.  
  34. int first_free = 1;
  35. while (T->elements[first_free].used) {
  36. first_free++;
  37. }
  38.  
  39. T->elements[first_free].label = x;
  40. T->elements[first_free].used = true;
  41.  
  42.  
  43. if (!T->elements[n].fc) {
  44. T->elements[n].fc = first_free;
  45. }
  46.  
  47. else {
  48. int bla = T->elements[n].fc;
  49. while (T->elements[bla].ns) {
  50. bla = T->elements[bla].ns;
  51. }
  52. T->elements[bla].ns = first_free;
  53. }
  54. return first_free;
  55. }
  56.  
  57. void ChangeLabelT(std::string x, int n, tree *T) { T->elements[n].label = x;}
  58.  
  59. int ParentT(int n, tree *T) {
  60. for (int i = 1; i < 10000; ++i) {
  61. if (T->elements[i].fc == n)
  62. return i;
  63. else if (T->elements[i].ns == n)
  64. return ParentT(i, T);
  65. }
  66. return 0;
  67. }
  68.  
  69.  
  70.  
  71. void DeleteT(int n, tree *T) {
  72. std::cout << T->elements[n].ns << std::endl;
  73.  
  74. if (FirstChildT(ParentT(n, T), T) == n) {
  75. T->elements[ParentT(n, T)].fc = T->elements[n].ns;
  76. }
  77.  
  78. if (T->elements[n].ns == 0) {
  79. for (int i = 1; i < 10000; ++i) {
  80. if (T->elements[i].ns == n) {
  81. T->elements[i].ns = 0;
  82. break;
  83. }
  84. }
  85. }
  86.  
  87. else {
  88. for (int i = 1; i < 10000; ++i) {
  89. if (T->elements[i].ns == n) {
  90. T->elements[i].ns = T->elements[n].ns;
  91. break;
  92. }
  93. }
  94.  
  95. }
  96.  
  97. T->elements[n].used = false;
  98. }
  99.  
  100. void InitT(std::string x, tree *T) {
  101. T->first = 1;
  102. T->elements[1].label = x;
  103. T->elements[1].used = true;
  104. }

Report this snippet  

You need to login to post a comment.