数据结构——第七章作业
This commit is contained in:
128
作业/数据结构-金健/C++/第七章作业/第七章作业1.cpp
Normal file
128
作业/数据结构-金健/C++/第七章作业/第七章作业1.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
//
|
||||
// Created by 423A35C7 on 2023-12-14.
|
||||
//
|
||||
// 20:30
|
||||
// 22:20
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
template<typename T>
|
||||
class BiSortNode {
|
||||
std::unique_ptr<BiSortNode> left_child;
|
||||
std::unique_ptr<BiSortNode> right_child;
|
||||
T data;
|
||||
std::function<bool(T&, T&)> compare_function_;
|
||||
|
||||
public:
|
||||
// 好像这里没法使用initializer_list,initializer_list是对于多个相同值的初始化,而不是一个东西的初始化参数列表
|
||||
template<typename... Args>
|
||||
explicit BiSortNode(Args... args, std::function<bool(T&, T&)> _compare) : data(args...) {
|
||||
this->compare_function_(_compare);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
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<BiSortNode>(new_data);
|
||||
}
|
||||
else {
|
||||
this->left_child->insert(new_data);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (this->right_child == nullptr) {
|
||||
this->right_child = std::make_unique<BiSortNode>(new_data);
|
||||
}
|
||||
else {
|
||||
this->right_child->insert(new_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void preorder_traversal(std::vector<T>&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<T>&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<T>&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<T>&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<int> origin_vector;
|
||||
int temp;
|
||||
while (std::cin >> temp) {
|
||||
origin_vector.push_back(temp);
|
||||
}
|
||||
auto generator = origin_vector.cbegin();
|
||||
BiSortNode<int> bi_sort_node{*generator++};
|
||||
while (generator != origin_vector.cend()) {
|
||||
bi_sort_node.insert(*generator++);
|
||||
}
|
||||
|
||||
std::vector<int> preorder_traversal_result;
|
||||
bi_sort_node.preorder_traversal(preorder_traversal_result);
|
||||
std::vector<int> 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<int> post_result;
|
||||
bi_sort_node.postorder_traversal(post_result);
|
||||
for (const auto&i: post_result) {
|
||||
std::cout << i << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
std::vector<int> 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
|
||||
Reference in New Issue
Block a user