Posted By


mflajsek on 01/13/14

Tagged


Statistics


Viewed 89 times
Favorited by 0 user(s)

prvo_dijete_sljedeci_brat_polje


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

Implementacija opcenitog stabla „prvo dijete - sljedeci brat“ pomocu polja


Copy this code and paste it in your HTML
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct element{
  6. int name;
  7. int firstchild, nextsibling;
  8. bool root;
  9. };
  10.  
  11. struct tree{
  12. element Element[10000];
  13. int first;
  14. };
  15.  
  16. int ParentT(int node, tree *Stablo){
  17. for(int i=0; i<10000; i++){
  18. if (Stablo->Element[i].firstchild==node)
  19. return i;
  20. else if (Stablo->Element[i].nextsibling==node)
  21. ParentT(i,Stablo);
  22. }
  23. return -1;
  24. }
  25.  
  26. int FirstChildT(int node, tree *Stablo){
  27. return Stablo->Element[node].firstchild;
  28. }
  29.  
  30. int NextSiblingT(int node, tree *Stablo){
  31. return Stablo->Element[node].nextsibling;
  32. }
  33.  
  34. int LabelT(int node, tree *Stablo){
  35. if (Stablo->Element[node].root==0)
  36. return -1;
  37. else return Stablo->Element[node].name;
  38. }
  39.  
  40. int RootT(tree *Stablo){
  41. return Stablo->first;
  42. }
  43.  
  44. void CreateT(int child, int node, tree *Stablo){
  45. if (Stablo->Element[node].root==0)
  46. cout << "Cvor ne postoji. " << endl;
  47.  
  48. else if (Stablo->Element[node].firstchild==-1){
  49. Stablo->Element[node].firstchild=child;
  50. Stablo->Element[child].root=1;
  51. cout << "Unesite oznaku: " ;
  52. cin >> Stablo->Element[child].name;
  53. }
  54.  
  55. else{
  56. int brother;
  57. bool brat=true;
  58. brother=Stablo->Element[node].firstchild;
  59.  
  60. do{
  61. if (Stablo->Element[brother].nextsibling==-1) brat=false;
  62. else brother=Stablo->Element[brother].nextsibling;
  63. }while(brat);
  64.  
  65. Stablo->Element[brother].nextsibling=child;
  66. Stablo->Element[child].root=1;
  67. cout << "Unesite oznaku: " ;
  68. cin >> Stablo->Element[child].name;
  69. }
  70. }
  71.  
  72. void ChangeLabelT(int novi, int node, tree *Stablo){
  73. if (Stablo->Element[node].root==1)
  74. Stablo->Element[node].name=novi;
  75.  
  76. else cout << "Cvor ne postoji. " << endl;
  77. }
  78.  
  79. void Delete(int node, tree *Stablo){
  80. if (Stablo->Element[node].nextsibling==-1 && Stablo->Element[ParentT(node,Stablo)].firstchild==node){
  81. Stablo->Element[ParentT(node,Stablo)].firstchild=-1;
  82. Stablo->Element[ParentT(node,Stablo)].root=0;
  83. Stablo->Element[node].name=-1;
  84. }
  85.  
  86. else if (Stablo->Element[node].nextsibling!=-1 && Stablo->Element[ParentT(node,Stablo)].firstchild==node)
  87. Stablo->Element[ParentT(node,Stablo)].firstchild = Stablo->Element[node].nextsibling;
  88.  
  89. else{
  90. int brother=Stablo->Element[ParentT(node,Stablo)].firstchild;
  91. while (Stablo->Element[brother].nextsibling!=node)
  92. brother=Stablo->Element[brother].nextsibling;
  93. Stablo->Element[brother].nextsibling=Stablo->Element[node].firstchild;
  94. }
  95. }
  96.  
  97. void DeleteT(int node, tree *Stablo){
  98. if (Stablo->Element[node].root==0)
  99. cout << "Cvor ne postoji." << endl;
  100.  
  101. else if (Stablo->Element[node].firstchild!=-1){
  102. while (Stablo->Element[node].firstchild!=-1)
  103. Delete(Stablo->Element[node].firstchild,Stablo);
  104. Delete(node,Stablo);
  105. }
  106.  
  107. else Delete(node,Stablo);
  108. }
  109.  
  110. void InitT(int x, tree *Stablo){
  111. for (int i=0; i<10000; i++){
  112. Stablo->Element[i].firstchild=-1;
  113. Stablo->Element[i].nextsibling=-1;
  114. Stablo->Element[i].root=0;
  115. }
  116.  
  117. Stablo->first=x;
  118. Stablo->Element[x].root=1;
  119. cout << "Unesite oznaku korijena: ";
  120. cin >> Stablo->Element[x].name;
  121. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.