Guest User

Untitled

a guest
Mar 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct tree{
  4. int oznaka;
  5. tree *left, *right;
  6. };
  7. tree *root;
  8. void Init(int broj){
  9. root=new tree;
  10. root->oznaka=broj;
  11. root->left=0;
  12. root->right=0;
  13. }
  14. tree* LeftChild(tree* cvor){
  15. if(cvor) return cvor->left;
  16. return 0;
  17. }
  18. tree* RightChild(tree* cvor){
  19. if(cvor) return cvor->right;
  20. return 0;
  21. }
  22. int Label(tree *cvor){
  23. return cvor->oznaka;
  24. }
  25. void ChangeLabel(int a, tree *cvor){
  26. cvor->oznaka=a;
  27. }
  28. tree* Root(){
  29. return root;
  30. }
  31. tree* Parent(tree* dijete, tree* cvor=root){
  32. if(!cvor) return 0;
  33. if(cvor->left==dijete||cvor->right==dijete) return cvor;
  34. Parent(dijete, cvor->left);
  35. Parent(dijete, cvor->right);
  36. }
  37. void Delete(tree *cvor){
  38. tree *roditelj=Parent(cvor);
  39. if(roditelj->left==cvor) roditelj->left=0;
  40. if(roditelj->right==cvor) roditelj->right=0;
  41. if(cvor->left) Delete(cvor->left);
  42. if(cvor->right) Delete(cvor->right);
  43. delete cvor;
  44. }
  45. void CreateLeft(int broj, tree *cvor){
  46. if(cvor->left) cout<<"Error!"<<endl;
  47. tree *novi=new tree;
  48. cvor->left=novi;
  49. novi->left=0;
  50. novi->right=0;
  51. novi->oznaka=broj;
  52. }
  53. void CreateRight(int broj, tree *cvor){
  54. if(cvor->right) cout<<"Error!"<<endl;
  55. tree *novi=new tree;
  56. cvor->right=novi;
  57. novi->left=0;
  58. novi->right=0;
  59. novi->oznaka=broj;
  60. }
Add Comment
Please, Sign In to add comment