Posted By

mvracan on 01/12/14


Tagged

Strukture


Versions (?)

opcenito stablo.h


 / Published in: C++
 

zadatak4

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct node{
  5. char label;
  6. int child,brother;
  7. };
  8.  
  9. struct tree{
  10. node polje[10000];
  11. int root;
  12. };
  13.  
  14. tree *InitT(int k,tree *T){
  15. T = new tree;
  16. for(int i=0;i<10000;i++){
  17. T->polje[i].label = ' ';
  18. T->polje[i].child = -1;
  19. T->polje[i].brother = -1;
  20. }
  21. T->polje[k].label = 'A';
  22. T->root = k;
  23. return T;
  24. }
  25.  
  26. void ChangeLabelT(char x,int n,tree *T){
  27. T->polje[n].label = x;
  28. }
  29.  
  30. int RootT(tree *T){
  31. return T->root;
  32. }
  33.  
  34. char LabelT(int n,tree *T){
  35. if(T->polje[n].label==' ') cout << "Cvor " << n << " ne postoji u stablu!" << endl;
  36. return T->polje[n].label;
  37. }
  38.  
  39. void CreateT(int x,int n,tree *T){
  40. if(T->polje[n].label==' ') cout << "Cvor " << n << " ne postoji u stablu!" << endl;
  41. else{
  42. if(T->polje[n].child==-1) T->polje[n].child = x;
  43. else if(T->polje[T->polje[n].child].brother==-1) T->polje[T->polje[n].child].brother = x;
  44. else{
  45. n = T->polje[n].child;
  46. while(T->polje[n].brother!=-1) n = T->polje[n].brother;
  47. T->polje[n].brother = x;
  48. }
  49. T->polje[x].label = T->polje[n].label+1;
  50. T->polje[x].child = -1;
  51. T->polje[x].brother = -1;
  52. }
  53. }
  54.  
  55. int FirstChildT(int n,tree *T){
  56. return T->polje[n].child;
  57. }
  58.  
  59. int NextSiblingT(int n,tree *T){
  60. return T->polje[n].brother;
  61. }
  62.  
  63. int ParentT(int n,tree *T){
  64. for(int i=0;i<10000;i++){
  65. if(T->polje[i].child==n) return i;
  66. if(T->polje[i].brother==n) return ParentT(i,T);
  67. }
  68. }
  69.  
  70. tree *DeleteT(int n,tree *T){
  71. if(n==RootT(T)){
  72. T = InitT(-1,T);
  73. return T;
  74. }
  75. else{
  76. if(T->polje[n].child!=-1) DeleteT(T->polje[n].child,T);
  77. if(T->polje[n].brother!=-1) DeleteT(T->polje[n].brother,T);
  78. T->polje[n].child = -1;
  79. T->polje[n].brother = -1;
  80. T->polje[n].label = ' ';
  81. if(T->polje[ParentT(n,T)].brother!=-1) T->polje[ParentT(n,T)].child = T->polje[n].brother;
  82. else T->polje[ParentT(n,T)].child = -1;
  83. }
  84. }

Report this snippet  

You need to login to post a comment.