Posted By


drmarinko on 01/06/11

Tagged


Statistics


Viewed 531 times
Favorited by 0 user(s)

bstablo_polje.h


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  1. struct element
  2. {
  3. labeltype label;
  4. int used;
  5. };
  6.  
  7. typedef struct bt
  8. {
  9. element elements[10000];
  10. } *btree;
  11.  
  12. typedef int node;
  13.  
  14. node RootB(btree b_stablo)
  15. {
  16. if (b_stablo->elements[1].used == 1)
  17. return 1;
  18. else
  19. return 0;
  20. }
  21.  
  22. void InitB(labeltype v, btree b_stablo)
  23. {
  24. for (int i = 0; i<10000; i++)
  25. b_stablo->elements[i].used = 0;
  26. b_stablo->elements[1].label = v;
  27. b_stablo->elements[1].used = 1;
  28. }
  29.  
  30. labeltype LabelB(node n, btree b_stablo)
  31. {
  32. return b_stablo->elements[n].label;
  33. }
  34.  
  35. void ChangeLabelB(labeltype v, node n, btree b_stablo)
  36. {
  37. b_stablo->elements[n].label = v;
  38. }
  39.  
  40. int ExistsLeftChild(node n, btree b_stablo)
  41. {
  42. if (b_stablo->elements[2*n].used == 1)
  43. return 1;
  44. else
  45. return 0;
  46. }
  47.  
  48. int ExistsRightChild(node n, btree b_stablo)
  49. {
  50. if (b_stablo->elements[(2*n)+1].used == 1)
  51. return 1;
  52. else
  53. return 0;
  54. }
  55.  
  56. void CreateLeftB(labeltype v, node n, btree b_stablo)
  57. {
  58. if (ExistsLeftChild(n, b_stablo))
  59. cout << "Lijevo dijete vec postoji\n";
  60. else
  61. {
  62. b_stablo->elements[2*n].used = 1;
  63. b_stablo->elements[2*n].label = v;
  64. }
  65. }
  66.  
  67. void CreateRightB(labeltype v, node n, btree b_stablo)
  68. {
  69. if (ExistsRightChild(n, b_stablo))
  70. cout << "Desno dijete vec postoji\n";
  71. else
  72. {
  73. b_stablo->elements[(2*n)+1].used = 1;
  74. b_stablo->elements[(2*n)+1].label = v;
  75. }
  76. }
  77.  
  78. node LeftChildB(node n, btree b_stablo)
  79. {
  80. if (ExistsLeftChild(n, b_stablo))
  81. return 2*n;
  82. else
  83. return 0;
  84. }
  85.  
  86. node RightChildB(node n, btree b_stablo)
  87. {
  88. if (ExistsRightChild(n, b_stablo))
  89. return (2*n)+1;
  90. else
  91. return 0;
  92. }
  93.  
  94. node ParentB(node n, btree b_stablo)
  95. {
  96. if(n == 1)
  97. return 0;
  98. if(n%2)
  99. n--;
  100. return n/2;
  101. }
  102.  
  103.  
  104. void DeleteB(node n, btree b_stablo)
  105. {
  106. if (ExistsLeftChild(n, b_stablo))
  107. DeleteB(LeftChildB(n, b_stablo), b_stablo);
  108. if (ExistsRightChild(n, b_stablo))
  109. DeleteB(RightChildB(n, b_stablo), b_stablo);
  110. b_stablo->elements[n].used = 0;
  111. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.