Posted By

igotepava on 01/18/15


Tagged

data tree structures binary pointer


Versions (?)

binarno_stablo_pokazivac.h


 / Published in: C++
 

Implementacija binarnog stabla pomoću pokazivača.

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

Report this snippet  

You need to login to post a comment.