Posted By

mateja2802 on 01/05/11


Tagged


Versions (?)

Implementacija binarnog stabla pomoću polja


 / Published in: C++
 

  1. #define N 10000
  2. struct element {
  3. char labela[20];
  4. int used;
  5. };
  6.  
  7. struct bt {
  8. element polje[N];
  9. };
  10.  
  11. int ParentB (int p, bt* stablo) {
  12. if (stablo->polje[p].used == 0) return -1;
  13. if (p & 1) return (p - 1) / 2;
  14. else return p / 2;}
  15.  
  16. int RootB (bt* stablo) {
  17. if (stablo->polje[1].used) return 1;
  18. else return 0;}
  19.  
  20. int LeftChildB (int p, bt* stablo) {
  21. if ((stablo->polje[p * 2]).used) return p * 2;
  22. return -1;}
  23.  
  24. int RightChildB (int p, bt* stablo) {
  25. if ((stablo->polje[p * 2 + 1]).used) return p * 2 + 1;
  26. return -1;}
  27.  
  28. void DeleteB (int p, bt* stablo) {
  29. if (stablo->polje[p*2].used)
  30. DeleteB(p*2, stablo);
  31. stablo->polje[p].used ^= stablo->polje[p].used;
  32. if (stablo->polje[p*2+1].used)
  33. DeleteB(p*2+1, stablo);}
  34.  
  35. void InitB (char* label, bt* stablo) {
  36. if ((stablo->polje[1]).used) {
  37. DeleteB(1, stablo);
  38. (stablo->polje[1]).used = 1;
  39. strcpy((stablo->polje[1].labela), label);
  40. }
  41. else {
  42. (stablo->polje[1]).used = 1;
  43. strcpy((stablo->polje[1].labela), label);}}
  44.  
  45. char* LabelB(int p, bt* stablo) {
  46. if (stablo->polje[p].used) return stablo->polje[p].labela;
  47. return 0;}
  48.  
  49. void ChangeLabelB (char* label, int p, bt* stablo) {
  50. if (stablo->polje[p].used) strcpy(stablo->polje[p].labela, label);}
  51.  
  52. void CreateLeftB (char* label, int p, bt* stablo) {
  53. if (! stablo->polje[p*2].used && stablo->polje[p].used) {
  54. stablo->polje[p*2].used = 1;
  55. strcpy(stablo->polje[p*2].labela, label);}}
  56.  
  57. void CreateRightB (char* label, int p, bt* stablo) {
  58. if (! stablo->polje[p*2+1].used && stablo->polje[p].used) {
  59. stablo->polje[p*2+1].used = 1;
  60. strcpy(stablo->polje[p*2+1].labela, label);}}1

Report this snippet  

You need to login to post a comment.