Posted By


fstrunja on 01/12/14

Tagged


Statistics


Viewed 59 times
Favorited by 0 user(s)

opcenito_stablo_prvi_brat_sljedeci_brat.h


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

Implementacija općenitog stabla "prvi brat - sljedeći brat" pomoću polja


Copy this code and paste it in your HTML
  1. struct elem {
  2. int label, firstchild, nextsibling;
  3. };
  4.  
  5. struct t {
  6. elem elementi[10000];
  7. int first;
  8. };
  9.  
  10. t stablo;
  11.  
  12. int FirstChildT (int n, t stablo) {
  13. return stablo.elementi[n].firstchild;
  14. }
  15.  
  16. int NextSiblingT (int n, t stablo) {
  17. return stablo.elementi[n].nextsibling;
  18. }
  19.  
  20. int LabelT (int n, t stablo) {
  21. return stablo.elementi[n].label;
  22. }
  23.  
  24. int ParentT (int n, t stablo) {
  25. for (int i=1; i<10000; i++) {
  26. if (FirstChildT(i, stablo)==n)
  27. return i;
  28. if (NextSiblingT(i, stablo)==n)
  29. return ParentT(i, stablo);
  30. }
  31. return -1;
  32. }
  33.  
  34. int RootT (t stablo) {
  35. return stablo.first;
  36. }
  37.  
  38. int CreateT (int x, int n, t &stablo) {
  39. int pom = 1;
  40. while (LabelT(pom, stablo)!=-1)
  41. pom++;
  42. stablo.elementi[pom].label = x;
  43. stablo.elementi[pom].firstchild = -1;
  44. stablo.elementi[pom].nextsibling = -1;
  45. if (stablo.elementi[n].firstchild != -1) {
  46. n = FirstChildT(n, stablo);
  47. while (NextSiblingT(n, stablo)!=-1)
  48. n = NextSiblingT(n, stablo);
  49. stablo.elementi[n].nextsibling = pom;
  50. }
  51. else stablo.elementi[n].firstchild = pom;
  52. return pom;
  53. }
  54.  
  55. void ChangeLabelT (int x, int n, t &stablo) {
  56. stablo.elementi[n].label = x;
  57. }
  58.  
  59. void DeleteT (int n, t &stablo) {
  60. int pom;
  61. if (FirstChildT(n, stablo)!=-1) {
  62. pom = FirstChildT(n, stablo);
  63. DeleteT(pom, stablo);
  64. while (NextSiblingT(pom, stablo)!=-1) {
  65. pom = NextSiblingT(pom, stablo);
  66. DeleteT(pom, stablo);
  67. }
  68. }
  69. stablo.elementi[n].label = -1;
  70. if (n != RootT(stablo) && FirstChildT(ParentT(n,stablo), stablo)==n)
  71. stablo.elementi[ParentT(n, stablo)].firstchild = NextSiblingT(n, stablo);
  72. else if (n != RootT(stablo)) {
  73. pom = FirstChildT(ParentT(n,stablo), stablo);
  74. while (NextSiblingT(pom, stablo) != n)
  75. pom = NextSiblingT(pom, stablo);
  76. stablo.elementi[pom].nextsibling = NextSiblingT(n, stablo);
  77. }
  78. }
  79.  
  80. void InitT (int x, t &stablo) {
  81. stablo.first = 1;
  82. stablo.elementi[1].label = x;
  83. stablo.elementi[1].firstchild = -1;
  84. stablo.elementi[1].nextsibling = -1;
  85. for (int i=2; i<10000; i++) {
  86. stablo.elementi[i].label = -1;
  87. stablo.elementi[i].firstchild = -1;
  88. stablo.elementi[i].nextsibling = -1;
  89. }
  90. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.