Advertisement
Guest User

Untitled

a guest
May 19th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 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. struct node* usun_liscie(struct node*root) {
  68. if (root != NULL)
  69. {
  70. if (root->L == NULL && root->P == NULL)
  71. {
  72. node * p = new node;
  73. p = root->P;
  74. root->P = NULL;
  75. delete p;
  76.  
  77. return NULL;
  78. }
  79. else
  80. {
  81. root->L = usun_liscie(root->L);
  82. root->P = usun_liscie(root->P);
  83. }
  84. }
  85.  
  86.  
  87. return root;
  88. }
  89.  
  90. int main()
  91. {
  92. node*root = NULL;
  93. insertBST(root, 15);
  94. insertBST(root, 6);
  95. insertBST(root, 18);
  96. insertBST(root, 17);
  97. insertBST(root, 20);
  98. insertBST(root, 3);
  99. insertBST(root, 7);
  100. insertBST(root, 2);
  101. insertBST(root, 4);
  102. insertBST(root, 13);
  103. insertBST(root, 9);
  104.  
  105. inorder(root);
  106. cout << endl;
  107. usun_liscie(root);
  108. inorder(root);
  109. cout << endl;
  110.  
  111. system("PAUSE");
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement