Posted By

dare10 on 01/20/14


Tagged


Versions (?)

Binarno polje


 / Published in: C++
 

Header datoteka za implementaciju binarnog stabla pomocu polja

  1. #include<iostream>
  2. using namespace std;
  3. struct element{
  4. int label;
  5. bool used;
  6. };
  7. struct tree{
  8. element polje[1000];
  9. };
  10. tree *InitB(int x,tree *T){
  11. T = new tree;
  12. for(int i=0;i<1000;i++) T->polje[i].used = false;
  13. T->polje[1].label = x;
  14. T->polje[1].used = true;
  15. return T;
  16. }
  17. int LabelB(int n,tree *T){
  18. return T->polje[n].label;
  19. }
  20. void ChangeLabelB(int x,int n,tree *T){
  21. T->polje[n].label = x;
  22. }
  23. int RootB(tree *T){
  24. return T->polje[1].label;
  25. }
  26. int LeftChildB(int n,tree *T){
  27. if(!T->polje[2*n].used) return -1;
  28. return 2*n;
  29. }
  30. int RightChildB(int n,tree *T){
  31. if(!T->polje[2*n+1].used) return -1;
  32. return 2*n+1;
  33. }
  34. int ParentB(int n,tree *T){
  35. if(T->polje[1].label==n) return -1;
  36. if(n%2) return n/2+1;
  37. else return n/2;
  38. }
  39. void CreateLeftB(int x,int n,tree *T){
  40. if(T->polje[2*n].used) cout << "Zauzeta pozicija!" << endl;
  41. else{
  42. T->polje[2*n].label = x;
  43. T->polje[2*n].used = true;
  44. }
  45. }
  46. void CreateRightB(int x,int n,tree *T){
  47. if(T->polje[2*n+1].used) cout << "Zauzeta pozicija!" << endl;
  48. else{
  49. T->polje[2*n+1].label = x;
  50. T->polje[2*n+1].used = true;
  51. }
  52. }
  53. void DeleteB(int n,tree *T){
  54. if(LeftChildB(n,T)!=-1) DeleteB(LeftChildB(n,T),T);
  55. if(RightChildB(n,T)!=-1) DeleteB(RightChildB(n,T),T);
  56. T->polje[n].used = false;
  57. }

Report this snippet  

You need to login to post a comment.