Posted By


igradeca on 01/19/15

Tagged


Statistics


Viewed 615 times
Favorited by 0 user(s)

operacije_stablo.h


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

Datoteka zaglavlja za "main_drvo.cpp" iz kolegija Strukture podataka, zadaća 4.
Operacije nad općenitim stablom


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.