općenito stablo „prvo dijete - sljedeći brat“ pomoću polja


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

Implementacija općenitog stabla „prvo dijete - sljedeći brat“ pomoću polja za kolegij Strukture podataka.


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.