Posted By


akljaic on 01/19/15

Tagged


Statistics


Viewed 519 times
Favorited by 0 user(s)

opcenito_stablo.h


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

asdf


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

URL: asdf

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.