biblioteka-opcenito stablo


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

zadatak 4, opcenito stablo


Copy this code and paste it in your HTML
  1. struct elemOS {
  2. char v[15];
  3. int d, b;
  4. };
  5. struct OS {
  6. elemOS el[10000];
  7. int korijen;
  8. };
  9. typedef OS* stablo;
  10. typedef int onode;
  11.  
  12.  
  13. onode FirstChildT(onode n, stablo t) {
  14. return t->el[n].d;
  15. }
  16.  
  17. onode NextSiblingT(onode n, stablo t) {
  18. return t->el[n].b;
  19. }
  20.  
  21. char* LabelT(onode n, stablo t) {
  22. return t->el[n].v;
  23. }
  24.  
  25. void ChangeLabelT(char* x, onode n, stablo t) {
  26. strcpy(t->el[n].v, x);
  27. }
  28.  
  29. onode RootT(stablo t) {
  30. return t->korijen;
  31. }
  32.  
  33.  
  34. onode ParentT(onode n, stablo t) {
  35. for(int m=0; m<10000; m++) {
  36.  
  37. if(n == t->el[m].d) return m;
  38.  
  39. if(n == t->el[m].b) return ParentT(m, t);
  40. }
  41.  
  42. return -1;
  43. }
  44.  
  45.  
  46. void CreateT(onode x, onode n, stablo t) {
  47. if(strcmp(t->el[x].v, "")!=0) return;
  48. t->el[x].d = -1;
  49. t->el[x].b = -1;
  50. strcpy(t->el[x].v, "");
  51.  
  52. if(t->el[n].d==-1) {
  53. t->el[n].d = x;
  54. return;
  55. }
  56. n = FirstChildT(n, t);
  57. while(NextSiblingT(n, t)!=-1) n=NextSiblingT(n, t);
  58. t->el[n].b = x;
  59. }
  60.  
  61.  
  62.  
  63.  
  64. stablo InitT(onode x, stablo t) {
  65. t = new OS;
  66. for(int i=0; i<10000; i++) {
  67. t->el[i].d = -1;
  68. t->el[i].b = -1;
  69. strcpy(t->el[i].v, "");
  70. }
  71. t->korijen = x;
  72. return t;
  73. }
  74.  
  75.  
  76.  
  77. void DeleteT(onode n, stablo t, bool m=1) {
  78. if(m) {
  79. for(int i=0; i<10000; i++) {
  80. if(t->el[i].d==n) {
  81. t->el[i].d = NextSiblingT(n, t);
  82. t->el[n].b = -1;
  83. break;
  84. }
  85. else if(t->el[i].b==n) {
  86. t->el[i].b = NextSiblingT(n, t);
  87. t->el[n].b = -1;
  88. break;
  89. }
  90. }
  91. DeleteT(n, t, 0);
  92. return;
  93. }
  94. if(FirstChildT(n, t)!=-1) DeleteT(FirstChildT(n, t), t, 0);
  95. for(onode n2=NextSiblingT(n, t); n2!=-1; n2=NextSiblingT(n2, t) )
  96. DeleteT(n2, t, 0);
  97. strcpy(t->el[n].v, "");
  98. t->el[n].b = -1;
  99. t->el[n].d = -1;
  100. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.