Files
Data-Structure/Exercise/Homework4/Q3.cpp
2025-11-08 13:46:52 +08:00

182 lines
4.5 KiB
C++
Raw Blame History

#include <iostream>
#include <vector>
#include<string>
using namespace std;
template <class T>
struct Node {
T element;
Node<T>* left;
Node<T>* right;
Node<T>* parent;
Node(const T& e, Node<T>* p) : element(e), parent(p), left(nullptr), right(nullptr) {}
};
template <class T>
class BinTree {
public:
Node<T>* root;
int height;
vector<T> arr;
BinTree() : root(nullptr), height(0) {}
BinTree(const vector<T>& arrs) : arr(arrs), height(0) {}
void test() {
for (auto x : arr) {
cout << x << " ";
}
}
Node<T>* returnroot() {
return this->root;
}
Node<T>* build(int& index, Node<T>* parent) {
if (index >= arr.size() || arr[index] == 0) {
index++;
return nullptr;
}
Node<T>* node = new Node<T>(arr[index], parent);
index++;
node->left = build(index, node);
node->right = build(index, node);
return node;
}
void buildTree() {
int index = 0;
this->root = build(index, nullptr);
}
Node<T>* create(const string& post, const string& in,
int ps, int pe, int is, int ie, bool& ok) {
if (ps > pe || is > ie) {
return nullptr;
}
T root = post[pe];
int pos = -1;
for (int i = is; i <= ie; i++) {
if (in[i] == root) {
pos = i;
break;
}
}
if (pos == -1) {
ok = false;
return nullptr;
}
Node<T>* node = new Node<T>(root, nullptr);
int len = pos - is;
node->left = create(post, in, ps, ps + len - 1, is, pos - 1, ok);
node->right = create(post, in, ps + len, pe - 1, pos + 1, ie, ok);
return node;
}
int getHeight(Node<T>* node) {
if (node == nullptr) return 0;
int lh = getHeight(node->left);
int rh = getHeight(node->right);
if (lh > rh) return lh + 1;
else return rh + 1;
}
string preorderStr(Node<T>* node) {
if (node == nullptr) return "";
string res = "";
res += node->element;
res += preorderStr(node->left);
res += preorderStr(node->right);
return res;
}
//<2F><><EFBFBD><EFBFBD>
void preorder(Node<T>* node) {
if (node == nullptr) return;
cout << node->element << " ";
preorder(node->left);
preorder(node->right);
}
void inorder(Node<T>* node) {
if (node == nullptr) return;
inorder(node->left);
cout << node->element << ", parent";
//cout << ( node->parent == nullptr ? 0 : node->parent->element) << endl;
inorder(node->right);
}
void postorder(Node<T>* node) {
if (node == nullptr) return;
postorder(node->left);
postorder(node->right);
cout << node->element << " ";
}
Node<T>* inorder2(Node<T>* node, const T& x) {
if (node == nullptr) return nullptr;
if (node->element == x) {
return node;
}
Node<T>* leftResult = inorder2(node->left, x);
if (leftResult != nullptr) {
return leftResult;
}
Node<T>* rightResult = inorder2(node->right, x);
return rightResult;
}
void findparent(const T& e) {
auto x = inorder2(this->root, e);
if (x == nullptr || x->parent == nullptr) cout << 0 << endl;
else cout << x->parent->element << endl;
}
};
bool check(const string& post, const string& in) {
if (post.length() != in.length()) return false;
int cnt[26] = {0};
for (int i = 0; i < post.length(); i++) {
cnt[post[i] - 'A']++;
}
for (int i = 0; i < in.length(); i++) {
cnt[in[i] - 'A']--;
if (cnt[in[i] - 'A'] < 0) return false;
}
return true;
}
int main() {
string post, in;
while (getline(cin, post) && getline(cin, in)) {
if (post.empty() || in.empty()) break;
if (!check(post, in)) {
cout << "INVALID" << endl;
continue;
}
BinTree<char> tree;
bool ok = true;
tree.root = tree.create(post, in, 0, post.length() - 1, 0, in.length() - 1, ok);
if (!ok) {
cout << "INVALID" << endl;
} else {
tree.height = tree.getHeight(tree.root) - 1;
cout << tree.height << endl;
string pre = tree.preorderStr(tree.root);
cout << pre << endl;
}
}
return 0;
}