bstablo_pokazivac za zadatak 4


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

Datoteka zaglavlja za zadatak 4


Copy this code and paste it in your HTML
  1. struct telement{
  2. int label, root;
  3. telement *left, *right;
  4. };
  5.  
  6.  
  7. typedef struct telement *tnode;
  8. typedef struct telement *ttree;
  9.  
  10.  
  11. tnode ParentB (tnode node, ttree tree){
  12. tnode par;
  13. if (LeftChildB(node, tree)){
  14. if (tree->left == node) return tree->left;
  15. par = ParentB (node, tree->left);
  16. }
  17. if (RightChildB(node, tree)){
  18. if (tree->right == node) return tree->right;
  19. par = ParentB(node, tree->right);
  20. }
  21. return par;
  22. };
  23.  
  24.  
  25. tnode LeftChildB (tnode node, ttree tree){
  26. if (node->left != NULL)
  27. return node->left;
  28. if (node->left == NULL)
  29. cout<<"Ne postoji lijevo dijete!"<<endl;
  30. };
  31.  
  32.  
  33. tnode RightChildB (tnode node, ttree tree){
  34. if (node->right != NULL)
  35. return node->right;
  36. if (node->right == NULL)
  37. cout<<"Ne postoji desno dijete!"<<endl;
  38. };
  39.  
  40.  
  41. int LabelB (tnode node, ttree tree){
  42. if(RootB (tree)){
  43. return node->label;
  44. }
  45. else
  46. cout<<"ERROR"<<endl;
  47. };
  48.  
  49.  
  50. void ChangeLabelB (int label, tnode node, ttree tree){
  51. if (!RootB (tree))
  52. cout << "Ne postoji cvor!" << endl;
  53. else
  54. tree->label = label;
  55. };
  56.  
  57.  
  58. tnode RootB (ttree tree){
  59. if (tree) return tree;
  60. else
  61. cout<<"ERROR"<<endl;
  62. };
  63.  
  64.  
  65. void CreateLeftB (int label, tnode node, ttree tree){
  66. tnode novi = new telement;
  67. if (LeftChildB(node, tree)){
  68. cout<<"Vec postoji lijevo dijete!"<<endl;
  69. return;
  70. }
  71. else{
  72. novi->label = label;
  73. novi->left = NULL;
  74. novi->right = NULL;
  75. node->left = novi;
  76. }
  77. };
  78.  
  79.  
  80. void CreateRightB (int label, tnode node, ttree tree){
  81. tnode novi = new telement;
  82. if (RightChildB(node, tree)){
  83. cout<<"Vec postoji desno dijete!"<<endl;
  84. return;
  85. }
  86. else{
  87. novi->label = label;
  88. novi->left = NULL;
  89. novi->right = NULL;
  90. node->right = novi;
  91. }
  92. };
  93.  
  94.  
  95. void DeleteB (tnode node, ttree tree){
  96. if (LeftChildB (node, tree)) DeleteB (node->left, tree);
  97. if (RightChildB (node, tree) ) DeleteB (node->right, tree);
  98. delete node;
  99. };
  100.  
  101.  
  102. void InitB (int root, ttree tree){
  103. tnode novi = new telement;
  104. novi->root = root;
  105. novi->left = NULL;
  106. novi->right = NULL;
  107. tree = novi;
  108. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.