Posted By


cvitka on 01/17/15

Tagged


Statistics


Viewed 497 times
Favorited by 0 user(s)

Related snippets


bstablo_pokazivaci.h


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

Implementacija binarnog stabla pomoću pokazivača u jeziku C++


Copy this code and paste it in your HTML
  1. struct binarno_stablo
  2. {
  3. int label;
  4. int left,right;
  5. };
  6. binarno_stablo *tree = new binarno_stablo[1000];
  7. int ParentB(int n, binarno_stablo *B)
  8. {
  9. if (n<=1)
  10. return -1;
  11. if (tree[n].label>0)
  12. return (n/2);
  13. else
  14. return -1;
  15. };
  16.  
  17. int LeftChildB(int n, binarno_stablo *B)
  18. {
  19. if (tree[n].label>=0)
  20. {
  21. if (tree[n].left>0)
  22. return (n*2);
  23. else
  24. return -1;
  25. }
  26. else
  27. return -1;
  28. };
  29.  
  30. int RightChildB(int n, binarno_stablo *B)
  31. {
  32. if (tree[n].label>=0)
  33. {
  34. if (tree[n].right>0)
  35. return (n*2+1);
  36. else
  37. return -1;
  38. }
  39. else
  40. return -1;
  41.  
  42. };
  43.  
  44. int LabelB(int n, binarno_stablo *B)
  45. {
  46. if (tree[n].label>0)
  47. return (tree[n].label);
  48. else
  49. return -1;
  50. };
  51.  
  52. int ChangeLabelB(int x, int n, binarno_stablo *B)
  53. {
  54. if (tree[n].label>0)
  55. tree[n].label=x;
  56. else
  57. return -1;
  58. };
  59.  
  60. int RootB(binarno_stablo *B)
  61. {
  62. if (tree[1].label>0)
  63. return 1;
  64. else
  65. return -1;
  66. };
  67.  
  68. int CreateLeftB(int x, int n, binarno_stablo *B)
  69. {
  70. if (tree[n].label>0)
  71. {
  72. if (tree[n].left<=0)
  73. {
  74. binarno_stablo *left1=new binarno_stablo;
  75. tree[n].left=n*2;
  76. left1->label=x;
  77. tree[n*2]=*left1;
  78. }
  79. else
  80. return -1;
  81. }
  82. else
  83. return -1;
  84. };
  85.  
  86. int CreateRightB(int x, int n, binarno_stablo *B)
  87. {
  88. if (tree[n].label>0)
  89. {
  90. if (tree[n].right<=0)
  91. {
  92. binarno_stablo *right1=new binarno_stablo;
  93. tree[n].right=(n*2)+1;
  94. right1->label=x;
  95. tree[(n*2)+1]=*right1;
  96. }
  97. else
  98. return -1;
  99. }
  100. else
  101. return -1;
  102. };
  103.  
  104. int DeleteB(int n, binarno_stablo *B)
  105. {
  106. if (tree[n].label>0)
  107. {
  108. if (tree[n*2].label>0)
  109. {
  110. DeleteB(n*2, B);
  111. tree[n*2].label=-1;
  112. tree[n*2].left=-1;
  113. tree[n*2].right=1;
  114. }
  115. if (tree[(n*2)+1].label>0)
  116. {
  117. DeleteB(n*2, B);
  118. tree[(n*2)+1].label=-1;
  119. tree[(n*2)+1].left=-1;
  120. tree[(n*2)+1].right=1;
  121. }
  122. tree[n].label=-1;
  123. tree[n].right=-1;
  124. tree[n].left=-1;
  125. }
  126. };
  127.  
  128. int InitB(int x, binarno_stablo *B)
  129. {
  130. B->label=x;
  131. B->right=0;
  132. B->left=0;
  133. tree[1]=*B;
  134. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.