// // Created by 423A35C7 on 2023-12-14. // // 20:30 // 22:20 #include #include #include #include #include #include template class BiSortNode { std::unique_ptr left_child; std::unique_ptr right_child; T data; std::function compare_function_; public: // 好像这里没法使用initializer_list,initializer_list是对于多个相同值的初始化,而不是一个东西的初始化参数列表 template explicit BiSortNode(Args... args, std::function _compare) : data(args...) { this->compare_function_(_compare); } template explicit BiSortNode(Args... args) : data(args...) { this->compare_function_ = [](T&a, T&b) { return a < b; }; } void insert(T new_data) { if (new_data < this->data) { if (this->left_child == nullptr) { this->left_child = std::make_unique(new_data); } else { this->left_child->insert(new_data); } } else { if (this->right_child == nullptr) { this->right_child = std::make_unique(new_data); } else { this->right_child->insert(new_data); } } } void preorder_traversal(std::vector&output) { output.push_back(this->data); if (this->left_child != nullptr) this->left_child->preorder_traversal(output); if (this->right_child != nullptr) this->right_child->preorder_traversal(output); } void mirror_preorder_traversal(std::vector&output) { output.push_back(this->data); if (this->right_child != nullptr) this->right_child->mirror_preorder_traversal(output); if (this->left_child != nullptr) this->left_child->mirror_preorder_traversal(output); } void postorder_traversal(std::vector&output) { if (this->left_child != nullptr) this->left_child->postorder_traversal(output); if (this->right_child != nullptr) this->right_child->postorder_traversal(output); output.push_back(this->data); } void mirror_postorder_traversal(std::vector&output) { if (this->right_child != nullptr) this->right_child->mirror_postorder_traversal(output); if (this->left_child != nullptr) this->left_child->mirror_postorder_traversal(output); output.push_back(this->data); } }; int main() { std::vector origin_vector; int temp; while (std::cin >> temp) { origin_vector.push_back(temp); } auto generator = origin_vector.cbegin(); BiSortNode bi_sort_node{*generator++}; while (generator != origin_vector.cend()) { bi_sort_node.insert(*generator++); } std::vector preorder_traversal_result; bi_sort_node.preorder_traversal(preorder_traversal_result); std::vector mirror_result; bi_sort_node.mirror_preorder_traversal(mirror_result); std::ofstream outfile; outfile.open("temp_out.txt", std::ios::out); if (origin_vector == preorder_traversal_result || origin_vector == mirror_result) { std::cout << "Yes" << std::endl; std::vector post_result; bi_sort_node.postorder_traversal(post_result); for (const auto&i: post_result) { std::cout << i << " "; } std::cout << std::endl; std::vector mirror_post_result; bi_sort_node.mirror_postorder_traversal(mirror_post_result); for (const auto&i: mirror_post_result) { outfile << i << " "; } outfile << std::endl; } else { std::cout << "No" << std::endl; } outfile.close(); return 0; } // 输入: // 41 40 19 8 5 5 17 32 39 32 98 95 68 56 60 59 67 94 77 98 // 输出: // Yes // 5 5 17 8 32 39 32 19 40 59 67 60 56 77 94 68 95 98 98 41 // temp_out.txt 文件输出: // 98 77 94 67 59 60 56 68 95 98 32 39 32 17 5 5 8 19 40 41