binarno stablo_polja


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

binarno stablo_polja


Copy this code and paste it in your HTML
  1. struct belement {
  2. int v;
  3. bool u;
  4. };
  5.  
  6. struct bstablo {
  7. belement elems[10000];
  8. };
  9.  
  10. typedef bstablo* binStablo;
  11. typedef int binNode;
  12.  
  13. binStablo InitB(int x, binStablo t) {
  14. t = new bstablo;
  15.  
  16. t->elems[0].u=0;
  17. for(int i=2; i<10000; i++)
  18. t->elems[i].u=0;
  19.  
  20. t->elems[1].v = x;
  21. t->elems[1].u = 1;
  22. return t;
  23. }
  24.  
  25. binNode RootB(binStablo t) {
  26. return 1;
  27. }
  28.  
  29. binNode ParentB(binNode n, binStablo t) {
  30. if(n==RootB(t))
  31. return 0;
  32. else return n/2;
  33. }
  34.  
  35. binNode LeftChildB(binNode n, binStablo t) {
  36. if(t->elems[n*2].u==0)
  37. return 0;
  38. else return n*2;
  39. }
  40.  
  41. binNode RightChildB(binNode n, binStablo t) {
  42. if(t->elems[n*2+1].u==0)
  43. return 0;
  44. else return n*2+1;
  45. }
  46.  
  47. int LabelB(binNode n, binStablo t) {
  48. int label=t->elems[n].v;
  49. return label;
  50. }
  51.  
  52. void ChangeLabelB(int x, binNode n, binStablo t) {
  53. if(n>=0 && n<10000)
  54. t->elems[n].v = x;
  55. }
  56.  
  57. void CreateLeftB(int x, binNode n, binStablo t) {
  58. if(n<0 || n>10000) return;
  59.  
  60. if(t->elems[n*2].u)
  61. return;
  62.  
  63. t->elems[n*2].v = x;
  64. t->elems[n*2].u = 1;
  65. }
  66.  
  67. void CreateRightB(int x, binNode n, binStablo t) {
  68. if(n<0 || n>10000) return;
  69.  
  70. if(t->elems[n*2+1].u)
  71. return;
  72.  
  73. t->elems[n*2+1].v = x;
  74. t->elems[n*2+1].u = 1;
  75. }
  76.  
  77. void DeleteB(binNode n, binStablo t) {
  78. if(n==1)return;
  79. if(n>=10000)return;
  80.  
  81. if(t->elems[n*2].u)
  82. DeleteB(n*2, t);
  83. if(t->elems[n*2+1].u)
  84. DeleteB(n*2+1, t);
  85.  
  86. t->elems[n].u = 0;
  87. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.