Posted By

akljaic on 01/19/15


Tagged

tree binary


Versions (?)

binarno_polje.h


 / Published in: C++
 

URL: asdf

asdf

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct bt{
  5. char label;
  6. bool used;
  7. };
  8.  
  9. struct btree{
  10. bt element[1000];
  11. };
  12. typedef struct btree element;
  13.  
  14. bool ExistLeftChild(int n, btree *T){
  15. if(T->element[2*n].used) return true;
  16. return false;
  17. }
  18.  
  19. bool ExistRightChild(int n, btree *T){
  20. if(T->element[2*n+1].used) return true;
  21. return false;
  22. }
  23.  
  24. int ParentB(int n, btree *T){
  25. if(T->element[1].label == n) return -1;
  26. if(n%2) return n/2+1;
  27. else return n/2;
  28. }
  29.  
  30. int LeftChildB(int n, btree *T){
  31. if(T->element[n*2].used) return n*2;
  32. else return -1;
  33. }
  34.  
  35. int RightChildB(int n, btree *T){
  36. if(T->element[n*2+1].used) return n*2+1;
  37. else return -1;
  38. }
  39.  
  40. int LabelB(int n, btree *T){
  41. return T->element[n].label;
  42. }
  43.  
  44. void ChangeLabelB(int x, int n, btree *T){
  45. T->element[n].label = x;
  46. }
  47.  
  48. int RootB(btree *T){
  49. return T->element[1].label;
  50. }
  51.  
  52. void DeleteB(int n, btree *T){
  53. if(LeftChildB(n,T) != -1) DeleteB(LeftChildB(n,T),T);
  54. if(RightChildB(n,T) != -1) DeleteB(RightChildB(n,T),T);
  55. T->element[n].used = false;
  56. }
  57.  
  58. btree *InitB(int x, btree *T){
  59. T = new btree;
  60. for (int i=2; i<1000; i++)
  61. T->element[i].used = false;
  62. T->element[1].label = x;
  63. T->element[1].used = true;
  64. return T;
  65. }
  66.  
  67. void CreateLeftB(int x, int n, btree *T){
  68. if (T->element[n*2].used) cout << "Ima vec lijevo dijete!" << endl;
  69. else{
  70. T->element[n*2].label = x;
  71. T->element[n*2].used = true;
  72. }
  73. }
  74. void CreateRightB(int x, int n, btree *T){
  75. if (T->element[n*2+1].used) cout << "Ima vec desno dijete!" << endl;
  76. else{
  77. T->element[n*2+1].label = x;
  78. T->element[n*2+1].used = true;
  79. }
  80. }

Report this snippet  

You need to login to post a comment.