Posted By

dare10 on 01/20/14


Tagged


Versions (?)

Prvo dijete sljedeci brat


 / Published in: C++
 

Header datoteka za implementaciju stabla prvo dijete sljedeci brat

  1. #include <iostream>
  2. using namespace std;
  3. struct elem{
  4. char labela;
  5. int dijete,brat;
  6. };
  7. struct tree{
  8. elem P[1000];
  9. int root;
  10. };
  11. tree *InitT(int k,tree *T){
  12. T=new tree;
  13. for(int i=0;i<1000;i++){
  14. T->P[i].labela='0';
  15. T->P[i].dijete=T->P[i].brat=-1;
  16. }
  17. T->P[k].labela='A';
  18. T->root=k;
  19. return T;
  20. }
  21. void ChangeLabelT(char lab,int n,tree *T ){
  22. T->P[n].labela=lab;
  23. }
  24. int RootT(tree *T){
  25. return T->root;
  26. }
  27. char LabelT(int n,tree *T){
  28. return T->P[n].labela;
  29. }
  30. void CreateT(int x,int n,tree *T){
  31. if(T->P[n].labela=='0'){
  32. cout << "Ne postoji cvor: " << n << endl;
  33. return;
  34. }
  35. if(T->P[n].dijete==-1)T->P[n].dijete=x;
  36. else if(T->P[T->P[n].dijete].brat==-1)T->P[T->P[n].dijete].brat=x;
  37. else{
  38. n=T->P[n].dijete;
  39. while(T->P[n].brat!=-1) n = T->P[n].brat;
  40. T->P[n].brat = x;
  41. }
  42. T->P[x].labela = T->P[n].labela+1;
  43. T->P[x].dijete = T->P[x].brat = -1;
  44. }
  45. int FirstChildT(int n,tree *T){
  46. return T->P[n].dijete;
  47. }
  48. int NextSiblingT(int n,tree *T){
  49. return T->P[n].brat;
  50. }
  51. int ParentT(int n,tree *T){
  52. for(int i=0;i<1000;i++){
  53. if(T->P[i].dijete==n)return i;
  54. if(T->P[i].brat==n)return ParentT(i,T);
  55. }
  56. }
  57. void DeleteT(int n,tree *T){
  58. if(T->P[n].dijete!=-1) DeleteT(T->P[n].dijete,T);
  59. if(T->P[n].brat!=-1) DeleteT(T->P[n].brat,T);
  60. T->P[n].dijete=T->P[n].brat=-1;
  61. T->P[n].labela='0';
  62. if(T->P[ParentT(n,T)].brat!=-1) T->P[ParentT(n,T)].dijete = T->P[ParentT(n,T)].brat;
  63. else T->P[ParentT(n,T)].dijete = -1;
  64. }

Report this snippet  

You need to login to post a comment.