Binarno stablo pokazivac


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

pokazivac


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.