Posted By


mateom11 on 01/19/15

Tagged


Statistics


Viewed 543 times
Favorited by 0 user(s)

Related snippets


prvo_dijete-sljedeci_brati.h


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

prvo_dijete-sljedeci_brat


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.