Posted By


kgrlic on 01/19/15

Tagged


Statistics


Viewed 119 times
Favorited by 0 user(s)

binarno_polje.cpp


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

test


Copy this code and paste it in your HTML
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. const int ARRAY_SIZE = 100;
  5.  
  6. struct st_ELEM{
  7. int label;
  8. bool used;
  9. };
  10.  
  11. struct st_BTREE{
  12. st_ELEM node[ARRAY_SIZE];
  13. };
  14.  
  15. st_BTREE *INIT_B(int x, st_BTREE *T){
  16. T = new st_BTREE;
  17. for(int i=0;i<ARRAY_SIZE;i++) T->node[i].used = false;
  18. T->node[1].label = x;
  19. T->node[1].used = true;
  20. return T;
  21. }
  22.  
  23. int LABEL_B(int n, st_BTREE *T){
  24. return T->node[n].label;
  25. }
  26.  
  27. void CHANGE_LABEL_B(int x,int n,st_BTREE *T){
  28. T->node[n].label = x;
  29. }
  30.  
  31. int ROOT_B(st_BTREE *T){
  32. return T->node[1].label;
  33. }
  34.  
  35. int LEFT_CHILD_B(int n, st_BTREE *T){
  36. if(!T->node[2*n].used) return -1;
  37. return 2*n;
  38. }
  39.  
  40. int RIGHT_CHILD_B(int n,st_BTREE *T){
  41. if(!T->node[2*n+1].used) return -1;
  42. return 2*n+1;
  43. }
  44.  
  45. int PARENT_B(int n,st_BTREE *T){
  46. if(T->node[1].label==n) return -1;
  47. if(n%2) return n/2+1;
  48. else return n/2;
  49. }
  50.  
  51. void CREATE_LEFT_B(int x,int n,st_BTREE *T){
  52. if(T->node[2*n].used) cout << "Polje puno!" << endl;
  53. else{
  54. T->node[2*n].label = x;
  55. T->node[2*n].used = true;
  56. }
  57. }
  58.  
  59. void CREATE_RIGHT_B(int x,int n,st_BTREE *T){
  60. if(T->node[2*n+1].used) cout << "Polje puno!" << endl;
  61. else{
  62. T->node[2*n+1].label = x;
  63. T->node[2*n+1].used = true;
  64. }
  65. }
  66.  
  67. void DELETE_B(int n,st_BTREE *T){
  68. if(LEFT_CHILD_B(n,T)!=-1) DELETE_B(LEFT_CHILD_B(n,T),T);
  69. if(RIGHT_CHILD_B(n,T)!=-1) DELETE_B(RIGHT_CHILD_B(n,T),T);
  70. T->node[n].used = false;
  71. }

URL: test

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.