Posted By


mateocindric on 01/19/15

Tagged


Statistics


Viewed 584 times
Favorited by 0 user(s)

Related snippets


prvo_dijete-sljedeci_brat.h


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

Implementacija općenitog stabla prvo dijete - sljedeći brat pomoću polja


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.