Advertisement
Guest User

Untitled

a guest
May 19th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node {
  6. int val;
  7. node *P;
  8. node *L;
  9. node *O;
  10. };
  11.  
  12. void insertBST(node *& root, int val) {
  13. if (root == NULL) {
  14. root = new node;
  15. root->val = val;
  16. root->L = root->P = NULL;
  17. }
  18. else {
  19. if (val < root->val) {
  20. insertBST(root->L, val);
  21. root->L->O = root;
  22. }
  23. else {
  24. insertBST(root->P, val);
  25. root->P->O = root;
  26. }
  27. }
  28. }
  29.  
  30. void inorder(node *root) {
  31. if (root != NULL) {
  32. inorder(root->L);
  33. cout << root->val << " ";
  34. inorder(root->P);
  35. }
  36. }
  37.  
  38. void find_sek(node*root, int val) {
  39. while (root) {
  40. if (root->val == val) {
  41. cout << "element istnieje!" << endl;
  42. break;
  43. }
  44. else if (root->val > val) {
  45. root = root->L;
  46. }
  47. else {
  48. root = root->P;
  49. }
  50. }
  51. }
  52.  
  53. void find_rek(node*root, int val) {
  54. if (root != NULL) {
  55. if (root->val == val) {
  56. cout << "element istnieje" << endl;
  57. }
  58. else if (root->val > val) {
  59. find_rek(root->L, val);
  60. }
  61. else {
  62. find_rek(root->P, val);
  63. }
  64. }
  65. }
  66.  
  67. node* usun_liscie(node*root) {
  68. if (root != NULL)
  69. {
  70. if (root->L == NULL && root->P == NULL)
  71. {
  72. delete root;
  73. return NULL;
  74. }
  75. else
  76. {
  77. root->L = usun_liscie(root->L);
  78. root->P = usun_liscie(root->P);
  79. }
  80. }
  81. return root;
  82. }
  83.  
  84. node* max(node*root) {
  85. while (root->P) root = root->P;
  86. return root;
  87. }
  88.  
  89. void poprzednik(node*root, node*&pop, int x) {
  90. if (root == NULL) {
  91. pop = NULL;
  92. return;
  93. }
  94. if (root->val == x) {
  95. if (root->L) {
  96. pop = max(root->L);
  97. }
  98. }
  99. else if (x < root->val) {
  100. poprzednik(root->L, pop, x);
  101. }
  102. else {
  103. pop = root;
  104. poprzednik(root->P, pop, x);
  105. }
  106.  
  107. }
  108.  
  109. int main()
  110. {
  111. node*root = NULL;
  112. insertBST(root, 15);
  113. insertBST(root, 6);
  114. insertBST(root, 18);
  115. insertBST(root, 17);
  116. insertBST(root, 20);
  117. insertBST(root, 3);
  118. insertBST(root, 7);
  119. insertBST(root, 2);
  120. insertBST(root, 4);
  121. insertBST(root, 13);
  122. insertBST(root, 9);
  123.  
  124. inorder(root);
  125. cout << endl;
  126. //usun_liscie(root);
  127. inorder(root);
  128. cout << endl;
  129. node*pop = NULL;
  130. poprzednik(root, pop, 17);
  131. cout << "Poprzednik dla wartosci wprowadzonej w funkcji wyzej (17) to: " <<pop->val << endl;
  132.  
  133. system("PAUSE");
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement