Posted By


mateom11 on 01/19/15

Tagged


Statistics


Viewed 539 times
Favorited by 0 user(s)

Related snippets


Binarno_stablo_polje.h


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

Binarno_stablo_polje


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.