Posted By


igradeca on 01/19/15

Tagged


Statistics


Viewed 608 times
Favorited by 0 user(s)

bin_pokazivac.h


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

Datoteka zaglavlja za "main_drvo.cpp" iz kolegija Strukture podataka, zadaća 4. Funkcije za rad nad binarnim stablom pomoću pokazivača.


Copy this code and paste it in your HTML
  1. struct pelement{
  2. int label;
  3. pelement *left, *right;
  4. };
  5.  
  6. pelement *InitB(int x, pelement *T){
  7. T= new pelement;
  8. T-> left= 0;
  9. T-> right= 0;
  10. T-> label= x;
  11. return T;
  12. }
  13.  
  14. pelement *RootB(pelement *T){
  15. return T;
  16. }
  17.  
  18. pelement *ParentB(int n, pelement *T){
  19.  
  20. if(T->label== n) return 0;
  21.  
  22. if(T-> left){
  23. if(T-> left-> label== n) return T;
  24. ParentB(n, T-> left);
  25. }
  26. if(T-> right){
  27. if(T-> right-> label== n) return T;
  28. ParentB(n, T-> right);
  29. }
  30. }
  31.  
  32. pelement *LeftChildB(pelement *T){
  33. return T-> left;
  34. }
  35.  
  36. pelement *RightChildB(pelement *T){
  37. return T-> right;
  38. }
  39.  
  40. int LabelB(pelement *T){
  41. return T-> label;
  42. }
  43.  
  44. void ChangeLabelB(int x, pelement *T){
  45. T->label = x;
  46. }
  47.  
  48. void CreateLeftB(int x, pelement *T){
  49. if(T-> left) return;
  50.  
  51. pelement *novi= new pelement;
  52. novi-> label= x;
  53. novi-> left= 0;
  54. novi-> right= 0;
  55. T-> left= novi;
  56. }
  57.  
  58. void CreateRightB(int x, pelement *T){
  59. if(T-> right) return;
  60.  
  61. pelement *novi= new pelement;
  62. novi-> label= x;
  63. novi-> left= 0;
  64. novi-> right= 0;
  65. T-> right= novi;
  66. }
  67.  
  68. void DeleteB(pelement *pom, pelement *T){
  69. pelement *erase= pom;
  70.  
  71. if(pom!= T){
  72. pelement *roditelj= ParentB(pom-> label, T);
  73. if(roditelj-> left== pom) roditelj-> left = NULL;
  74. else roditelj-> right= NULL;
  75. }
  76.  
  77. if(pom-> left) DeleteB(pom-> left, T);
  78. if(pom-> right) DeleteB(pom-> right, T);
  79. delete erase;
  80. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.