Posted By

mogrguric on 01/05/11


Tagged

sp4


Versions (?)

obicno stablo


 / Published in: C++
 

  1. using namespace std;
  2. struct el{
  3. char label;
  4. int firstchild,nextsibling;
  5. };
  6. struct otr{
  7. el elements[1000];
  8. int first;
  9. };
  10. int ParentT(int n, otr *tree){
  11. if(tree->first==n){
  12. return -1;
  13. cout<<"Cvor je korijen"<<endl;
  14. }
  15. for (int i=0;i<1000;i++){
  16. if(tree->elements[i].firstchild==n) return i;
  17. else if(tree->elements[i].nextsibling==n)ParentT(i,tree);
  18. }
  19. return -1;
  20. cout<<"Cvor ne postoji u stablu"<<endl;
  21. };
  22.  
  23. int FirstChildT(int n, otr *tree){
  24. if(tree->elements[n].firstchild==-1) {
  25. cout<<"Zadani cvor nema dijete"<<endl;
  26. return -1;
  27. }
  28. else
  29. return tree->elements[n].firstchild;
  30. };
  31.  
  32. int NextSiblingT(int n, otr *tree){
  33. if(tree->elements[n].nextsibling==-1){
  34. cout<<"Cvor je najdesnije dijete"<<endl;
  35. return -1;
  36. }
  37. else return tree->elements[n].nextsibling;
  38. };
  39.  
  40. char LabelT(int n, otr *tree){
  41. return tree->elements[n].label;
  42. };
  43.  
  44. int RootT(otr *tree){
  45. return tree->first;
  46. };
  47.  
  48. void CreateT(int dijete, int n, otr *tree){
  49. char labela;
  50. if(tree->elements[n].firstchild==-1){
  51. tree->elements[n].firstchild=dijete;
  52. tree->elements[dijete].firstchild=-1;
  53. tree->elements[dijete].nextsibling=-1;
  54. cout<<"Oznaka: ";
  55. cin>>labela;
  56. tree->elements[dijete].label=labela;
  57. }
  58. else{
  59. int brat;
  60. bool ima=true;
  61. brat=tree->elements[n].firstchild;
  62. do{
  63. if(tree->elements[brat].nextsibling==-1) ima=false;
  64. else brat=tree->elements[brat].nextsibling;
  65. }while(ima);
  66.  
  67. tree->elements[brat].nextsibling=dijete;
  68. tree->elements[dijete].firstchild=-1;
  69. tree->elements[dijete].nextsibling=-1;
  70. cout<<"Oznaka: ";
  71. cin>>labela;
  72. tree->elements[dijete].label=labela;
  73. }
  74. };
  75.  
  76. void ChangeLabelT(char x, int n, otr *tree){
  77. tree->elements[n].label=x;
  78. };
  79.  
  80. void Del(int n, otr *tree){
  81. if(tree->elements[n].nextsibling==-1&&tree->elements[ParentT(n,tree)].firstchild==n)
  82. tree->elements[ParentT(n,tree)].firstchild=-1;
  83.  
  84. else if(tree->elements[n].nextsibling!=-1&&tree->elements[ParentT(n,tree)].firstchild==n)
  85. tree->elements[ParentT(n,tree)].firstchild=tree->elements[n].nextsibling;
  86.  
  87. else{
  88. int brat=tree->elements[ParentT(n,tree)].firstchild;
  89. while(tree->elements[brat].nextsibling!=n)
  90. brat=tree->elements[brat].nextsibling;
  91.  
  92. tree->elements[brat].nextsibling=tree->elements[n].firstchild;
  93. }
  94. };
  95.  
  96. void DeleteT(int n,otr *tree){
  97. if(tree->elements[n].firstchild!=-1){
  98. while(tree->elements[n].firstchild!=-1)
  99. Del(tree->elements[n].firstchild,tree);
  100. Del(n,tree);
  101. }
  102. else Del(n,tree);
  103. };
  104.  
  105. void InitT(int korijen, otr *tree){
  106. char labela;
  107. tree->first=korijen;
  108. tree->elements[korijen].firstchild=-1;
  109. tree->elements[korijen].nextsibling=-1;
  110. cout<<"Oznaka: ";
  111. cin>>labela;
  112. tree->elements[korijen].label=labela;
  113. };

Report this snippet  

You need to login to post a comment.