Posted By

mjug on 01/20/15


Tagged

polje implementacija


Versions (?)

polje.h


 / Published in: C++
 

implementacija stabla preko polja

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. typedef int labeltype;
  5.  
  6. struct elem {
  7. labeltype label;
  8. int used; };
  9.  
  10. struct bt {
  11. elem elems[10000]; };
  12.  
  13. void InitB (int x, bt *bs) {
  14. if (bs) {
  15. delete bs; }
  16. for (int i = 0; i < 10000; i++) {
  17. bs->elems[i].used = -1; }
  18. bs->elems[1].label = x;
  19. bs->elems[1].used = 1; }
  20.  
  21. int RootB ( bt *bs) {
  22. return 1; }
  23. void CreateLeftB (int x, int oznaka, bt *bs) {
  24. if (bs->elems[oznaka * 2].used == 1) {
  25. cout << "Dijete vec postoji!" << endl; }
  26. else {
  27. bs->elems[oznaka * 2].label = x;
  28. bs->elems[oznaka * 2].used = 1; } }
  29.  
  30. void CreateRightB (int x, int oznaka, bt *bs) {
  31. if (bs->elems[oznaka * 2 + 1].used == 1) {
  32. cout << "Dijete vec postoji!" << endl; }
  33. else {
  34. bs->elems[oznaka * 2 + 1].label = x;
  35. bs->elems[oznaka * 2 + 1].used = 1; } }
  36.  
  37. int FindB (int x, bt *bs) {
  38. for (int i = 0; i < 10000; i++) {
  39. if (bs->elems[i].label == x) {
  40. return i; } }
  41.  
  42. cout << "Cvor ne postoji!" << endl;
  43. return 1; }
  44. int LeftChildB (int dijete, bt *bs) {
  45. return dijete * 2; }
  46.  
  47. int RightChildB (int dijete, bt *bs) {
  48. return dijete * 2 + 1; }
  49.  
  50. int LabelB (int oznaka, bt *bs) {
  51. return bs->elems[oznaka].label; }
  52. void DeleteB (int x, bt *bs) {
  53. if (bs->elems[x].used == 1) {
  54. if (bs->elems[x * 2].used == 1) {
  55. DeleteB (x * 2, bs); }
  56.  
  57. if (bs->elems[x * 2 + 1].used == 1) {
  58. DeleteB (x * 2 + 1, bs); }
  59.  
  60. bs->elems[x].used = 0;
  61. bs->elems[x].label = 0; }
  62. else {
  63. cout << "Cvor ne postoji." << endl; } }
  64. void ChangeLabelB (int x, int oznaka, bt *bs) {
  65. bs->elems[oznaka].label = x; }
  66.  
  67. int ParentB (int dijete, bt *bs) {
  68. if (dijete == 1) {
  69. return 0; }
  70. return dijete / 2; }

Report this snippet  

You need to login to post a comment.