fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct node
  5. {
  6. node *left;
  7. node *right;
  8. int data;
  9. };
  10. node *root = new node();
  11.  
  12. node* find_just_small(node* p)
  13. {
  14.  
  15. }
  16.  
  17. void delete_node(int d)
  18. {
  19. node* p = root;
  20. node* q;
  21. if (p == NULL)
  22. {
  23. cout << "root is null" << endl;
  24. return;
  25. }
  26.  
  27. if (p->data == d)
  28. {
  29. if (p->left == NULL && p->right == NULL)
  30. {
  31. p = NULL;
  32. }
  33. else if (p->left = NULL)
  34. root = p->right;
  35. else
  36. root = p->left;
  37. }
  38.  
  39. else if (d > p->data)
  40. q = p->right;
  41.  
  42. else
  43. q = p->left;
  44.  
  45. while (q)
  46. {
  47.  
  48. if (d > q->data)
  49. {
  50. p = q;
  51. q = q->right;
  52. }
  53.  
  54. else if (d < q->data)
  55. {
  56. p = q;
  57. q = q->left;
  58. }
  59. else
  60. break;
  61. }
  62. if (q == NULL)
  63. cout << "Node with key " << d << " does not exist" << endl;
  64. else
  65. {
  66. if (q->left == NULL)
  67. {
  68.  
  69.  
  70. }
  71. q = find_just_small(p);
  72. }
  73. }
  74.  
  75. void insert(int d)
  76. {
  77. node *p = root;
  78. while (p)
  79. {
  80. if (p->data > d)
  81. p = p->left;
  82. else
  83. p = p->right;
  84. }
  85.  
  86. p = new node();
  87. p->left = NULL;
  88. p->right = NULL;
  89. p->data = d;
  90. }
  91.  
  92. int main()
  93. {
  94. freopen("input.txt", "r", stdin);
  95. freopen("output.txt", "w", stdout);
  96.  
  97. root->left = NULL;
  98. root->right = NULL;
  99.  
  100. int n, temp;
  101. cin >> n;
  102.  
  103. for (int i = 0; i < n; i++)
  104. {
  105. cin >> temp;
  106. insert(temp);
  107. }
  108.  
  109. cin >> n;
  110. for (int i = 0; i < n; i++)
  111. {
  112. cin >> temp;
  113. delete_node(temp);
  114. }
  115.  
  116. return 0;
  117. }
Success #stdin #stdout 0.21s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty