Posted By


AOT_code on 01/12/14

Tagged


Statistics


Viewed 707 times
Favorited by 0 user(s)

binarno_stabo_pokazivac.h


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

Implementacija binarnog stabla pomoću funkcija koristeći pokazivače.


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.