用于存放学校的作业便于复习。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.0 KiB

  1. //
  2. // Created by 423A35C7 on 2023-12-14.
  3. //
  4. // 20:30
  5. // 22:20
  6. #include <algorithm>
  7. #include <fstream>
  8. #include <functional>
  9. #include <iostream>
  10. #include <memory>
  11. #include <vector>
  12. template<typename T>
  13. class BiSortNode {
  14. std::unique_ptr<BiSortNode> left_child;
  15. std::unique_ptr<BiSortNode> right_child;
  16. T data;
  17. std::function<bool(T&, T&)> compare_function_;
  18. public:
  19. // 好像这里没法使用initializer_list,initializer_list是对于多个相同值的初始化,而不是一个东西的初始化参数列表
  20. template<typename... Args>
  21. explicit BiSortNode(Args... args, std::function<bool(T&, T&)> _compare) : data(args...) {
  22. this->compare_function_(_compare);
  23. }
  24. template<typename... Args>
  25. explicit BiSortNode(Args... args) : data(args...) {
  26. this->compare_function_ = [](T&a, T&b) { return a < b; };
  27. }
  28. void insert(T new_data) {
  29. if (new_data < this->data) {
  30. if (this->left_child == nullptr) {
  31. this->left_child = std::make_unique<BiSortNode>(new_data);
  32. }
  33. else {
  34. this->left_child->insert(new_data);
  35. }
  36. }
  37. else {
  38. if (this->right_child == nullptr) {
  39. this->right_child = std::make_unique<BiSortNode>(new_data);
  40. }
  41. else {
  42. this->right_child->insert(new_data);
  43. }
  44. }
  45. }
  46. void preorder_traversal(std::vector<T>&output) {
  47. output.push_back(this->data);
  48. if (this->left_child != nullptr) this->left_child->preorder_traversal(output);
  49. if (this->right_child != nullptr) this->right_child->preorder_traversal(output);
  50. }
  51. void mirror_preorder_traversal(std::vector<T>&output) {
  52. output.push_back(this->data);
  53. if (this->right_child != nullptr) this->right_child->mirror_preorder_traversal(output);
  54. if (this->left_child != nullptr) this->left_child->mirror_preorder_traversal(output);
  55. }
  56. void postorder_traversal(std::vector<T>&output) {
  57. if (this->left_child != nullptr) this->left_child->postorder_traversal(output);
  58. if (this->right_child != nullptr) this->right_child->postorder_traversal(output);
  59. output.push_back(this->data);
  60. }
  61. void mirror_postorder_traversal(std::vector<T>&output) {
  62. if (this->right_child != nullptr) this->right_child->mirror_postorder_traversal(output);
  63. if (this->left_child != nullptr) this->left_child->mirror_postorder_traversal(output);
  64. output.push_back(this->data);
  65. }
  66. };
  67. int main() {
  68. std::vector<int> origin_vector;
  69. int temp;
  70. while (std::cin >> temp) {
  71. origin_vector.push_back(temp);
  72. }
  73. auto generator = origin_vector.cbegin();
  74. BiSortNode<int> bi_sort_node{*generator++};
  75. while (generator != origin_vector.cend()) {
  76. bi_sort_node.insert(*generator++);
  77. }
  78. std::vector<int> preorder_traversal_result;
  79. bi_sort_node.preorder_traversal(preorder_traversal_result);
  80. std::vector<int> mirror_result;
  81. bi_sort_node.mirror_preorder_traversal(mirror_result);
  82. std::ofstream outfile;
  83. outfile.open("temp_out.txt", std::ios::out);
  84. if (origin_vector == preorder_traversal_result || origin_vector == mirror_result) {
  85. std::cout << "Yes" << std::endl;
  86. std::vector<int> post_result;
  87. bi_sort_node.postorder_traversal(post_result);
  88. for (const auto&i: post_result) {
  89. std::cout << i << " ";
  90. }
  91. std::cout << std::endl;
  92. std::vector<int> mirror_post_result;
  93. bi_sort_node.mirror_postorder_traversal(mirror_post_result);
  94. for (const auto&i: mirror_post_result) {
  95. outfile << i << " ";
  96. }
  97. outfile << std::endl;
  98. }
  99. else {
  100. std::cout << "No" << std::endl;
  101. }
  102. outfile.close();
  103. return 0;
  104. }
  105. // 输入:
  106. // 41 40 19 8 5 5 17 32 39 32 98 95 68 56 60 59 67 94 77 98
  107. // 输出:
  108. // Yes
  109. // 5 5 17 8 32 39 32 19 40 59 67 60 56 77 94 68 95 98 98 41
  110. // temp_out.txt 文件输出:
  111. // 98 77 94 67 59 60 56 68 95 98 32 39 32 17 5 5 8 19 40 41