Posted By


Dragana_Mlikota on 01/12/14

Tagged


Statistics


Viewed 699 times
Favorited by 0 user(s)

BinTree_array.h


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

binarno stablo header polje


Copy this code and paste it in your HTML
  1. #define ARRAY_SIZE 10000
  2.  
  3. typedef int labeltype;
  4.  
  5. struct element{
  6. labeltype label;
  7. int used;
  8. };
  9.  
  10. struct bt{
  11. struct element elements[ARRAY_SIZE];
  12. };
  13.  
  14.  
  15. typedef struct bt * btree;
  16. typedef int bnode;
  17.  
  18. bool testExistB(bnode n, btree T){
  19. if (n==-1) return false;
  20. if (T->elements[n].used) return true;
  21. return false;
  22. }
  23.  
  24. bnode ParentB(bnode n, btree T){
  25. if (n%2==0) return n/2;
  26. return (n-1)/2;
  27. }
  28.  
  29. bnode LeftChildB(bnode n, btree T){
  30. if (T->elements[n*2].used) return n*2;
  31. return T->elements[0].label;
  32. }
  33.  
  34. bnode RightChildB(bnode n, btree T){
  35. if (T->elements[n*2+1].used) return n*2+1;
  36. return T->elements[0].label;
  37. }
  38.  
  39. labeltype LabelB(bnode n, btree T){
  40. return T->elements[n].label;
  41. }
  42.  
  43. void ChangeLabelB(labeltype x, bnode n, btree T){
  44. T->elements[n].label = x;
  45. }
  46.  
  47. bnode RootB(btree T){
  48. if (T->elements[1].used) return 1;
  49. return T->elements[0].label;
  50. }
  51.  
  52. void CreateLeftB(labeltype x, bnode n, btree T){
  53. if (!T->elements[n*2].used) {
  54. T->elements[n*2].used = 1;
  55. T->elements[n*2].label = x;
  56. }
  57. else cout << "Nije moguce stvoriti cvor, cvor je vec zauzet\n";
  58. }
  59.  
  60. void CreateRightB(labeltype x, bnode n, btree T){
  61. if (!T->elements[n*2+1].used) {
  62. T->elements[n*2+1].used = 1;
  63. T->elements[n*2+1].label = x;
  64. }
  65. else cout << "Nije moguce stvoriti cvor, cvor je vec zauzet\n";
  66. }
  67.  
  68. void DeleteB(bnode n, btree T){
  69. if (T->elements[LeftChildB(n, T)].used) DeleteB(LeftChildB(n, T), T);
  70. if (T->elements[RightChildB(n, T)].used) DeleteB(RightChildB(n, T), T);
  71. T->elements[n].used = 0;
  72.  
  73. }
  74.  
  75. btree InitB(labeltype x, btree T){
  76. T = new bt;
  77. T->elements[1].label = x;
  78. T->elements[1].used = 1;
  79. T->elements[0].label = -1;
  80. T->elements[0].used = 1;
  81. for (int i=2; i<ARRAY_SIZE; i++) T->elements[i].used = 0;
  82. return T;
  83. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.