Posted By


marmomcil on 01/06/11

Tagged


Statistics


Viewed 419 times
Favorited by 0 user(s)

bstablo_polje.h - marmomcil


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

Header file bstablo_polje.h - odn. implementacija binarnog stabla pomoću polja


Copy this code and paste it in your HTML
  1. struct element{
  2. labeltype oznaka;
  3. int zapisan;
  4. };
  5.  
  6. struct tstablo{
  7. struct element polje[10000];
  8. };
  9.  
  10. typedef struct tstablo *tree;
  11. typedef int node;
  12.  
  13. node ParentB(node n,tree stablo){
  14. if(n==1) return -1;
  15. else return ((int)n/2);
  16. }
  17.  
  18. node LeftChildB(node n,tree stablo){
  19. if (stablo->polje[2*n].zapisan) return 2*n;
  20. else return -1;
  21. }
  22.  
  23. node RightChildB(node n,tree stablo){
  24. if (stablo->polje[2*n+1].zapisan) return(2*n+1);
  25. else return -1;
  26. }
  27.  
  28. labeltype LabelB(node n,tree stablo){
  29. return(stablo->polje[n].oznaka);
  30. }
  31.  
  32. void ChangeLabelB(labeltype x, node n,tree stablo){
  33. if (stablo->polje[n].zapisan) stablo->polje[n].oznaka = x;
  34. else return;
  35. }
  36. }
  37.  
  38. node RootB(tree stablo){
  39. return 1;
  40. }
  41.  
  42. void CreateLeftB(labeltype x,node n,tree stablo){
  43. if (stablo->polje[2*n].zapisan) return;
  44. else{
  45. stablo->polje[2*n].zapisan = -1;
  46. stablo->polje[2*n].oznaka = x;
  47. }
  48. }
  49.  
  50. void CreateRightB(labeltype x,node n,tree stablo){
  51. if (stablo->polje[2*n+1].zapisan) return;
  52. else{
  53. stablo->polje[2*n+1].zapisan = -1;
  54. stablo->polje[2*n+1].oznaka = x;
  55. }
  56. }
  57.  
  58. void DeleteB(node n,tree stablo){
  59. node i;
  60. stack S;
  61. if(!stablo->polje[n].zapisan) return;
  62. else{
  63. MakeNullS(&S);
  64. if (stablo->polje[2*n].zapisan) PushS(2*n,&S);
  65. if (stablo->polje[2*n+1].zapisan) PushS(2*n+1,&S);
  66. stablo->polje[n].zapisan=0;
  67. while(!IsEmptyS(&S)){
  68. i=TopS(&S);
  69. PopS(&S);
  70. if (stablo->polje[2*i].zapisan) PushS(2*i,&S);
  71. if (stablo->polje[2*i+1].zapisan) PushS(2*i+1,&S);
  72. stablo->polje[i].zapisan=0;
  73. }
  74. }
  75.  
  76. void InitB(tree stablo, node x){
  77. for (int i=0;i<=9999;i++) stablo->polje[i].zapisan=-1;
  78. stablo->polje[1].oznaka=x;
  79. stablo->polje[1].zapisan=-1;
  80. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.