Posted By

hcosic2 on 01/18/15


Tagged

tree binary implementation stablo


Versions (?)

binstablo_polje.h


 / Published in: C++
 

Implementacija binarnog stabla pomoću polja

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

Report this snippet  

You need to login to post a comment.