Homework New

This commit is contained in:
e2hang
2025-11-08 13:46:52 +08:00
parent dc879776b1
commit b84c3ba783
8 changed files with 1001 additions and 0 deletions

97
Exercise/Homework4/Q1.cpp Normal file
View File

@@ -0,0 +1,97 @@
#include <iostream>
#include <vector>
using namespace std;
template <class T>
struct Node {
T element;
Node<T>* left;
Node<T>* right;
Node(const T& e) : element(e), 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) {
if (index >= arr.size() || arr[index] == 0) {
index++;
return nullptr;
}
Node<T>* node = new Node<T>(arr[index]);
index++;
node->left = build(index);
//cout << "build left ";
node->right = build(index);
//cout << "build right ";
return node;
}
void buildTree() {
int index = 0;
this->root = build(index);
}
//<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 << " ";
inorder(node->right);
}
void postorder(Node<T>* node) {
if (node == nullptr) return;
postorder(node->left);
postorder(node->right);
cout << node->element << " ";
}
};
int main() {
int x = 0;
vector<int> arr;
int tmp;
Node<int>* parent;
while (cin >> tmp) {
arr.push_back(tmp);
}
BinTree<int> tree(arr);
tree.buildTree();
//tree.test();
auto r = tree.root;
tree.preorder(r);
cout << endl;
tree.inorder(r);
cout << endl;
tree.postorder(r);
cout << endl;
return 0;
}

121
Exercise/Homework4/Q2.cpp Normal file
View File

@@ -0,0 +1,121 @@
#include <iostream>
#include <vector>
#include<string>
#include <sstream>
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);
}
//<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;
}
};
int main() {
int x = 0;
vector<int> arr;
string line;
getline(cin, line);
stringstream ss(line);
int num;
while (ss >> num) {
arr.push_back(num);
}
BinTree<int> tree(arr);
tree.buildTree();
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int t;
cin >> t;
tree.findparent(t);
}
return 0;
}

181
Exercise/Homework4/Q3.cpp Normal file
View File

@@ -0,0 +1,181 @@
#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;
}

View File

@@ -0,0 +1,93 @@
Q1
```
通过带空指针信息的先根序列亦称先序序列创建二叉树并进行先根先序、中根中序、后根后序遍历。二叉树结点数据域值为不等于0的整数可能是正数也可能是负数空指针用0表示例如1 5 8 0 0 0 6 0 0表示如下图的二叉树。
PA567.jpg
输入格式:
输入为一组用空格间隔的整数表示带空指针信息的二叉树先根序列。其中空指针信息用0表示。二叉树结点个数不超过150000高度不超过6000。输入数据保证二叉树各结点数据值互不相等。
输出格式:
输出为3行整数每个整数后一个空格。第1行为该二叉树的先根序列第2行为中根序列第3行为后根序列。
输入样例:
1 5 8 0 0 0 6 0 0
输出样例:
1 5 8 6
8 5 1 6
8 5 6 1
代码长度限制
16 KB
时间限制
200 ms
内存限制
20 MB
栈限制
8192 KB
```
Q2
```
编写程序在二叉树中查找给定结点及父结点。二叉树结点的数据域值不等于0的整数。
输入格式:
输入第1行为一组用空格间隔的整数表示带空指针信息的二叉树先根序列其中空指针用0表示。例如1 5 8 0 0 0 6 0 0表示如下图的二叉树。第2行为整数m表示查询个数。接下来m行每行为一个不等于0的整数K表示要查找的结点的数据值。m不超过100二叉树结点个数不超过150000高度不超过6000。输入数据保证二叉树各结点数据值互不相等。
PA567.jpg
输出格式:
输出为m行每行1个整数表示被查找结点K的父结点数据值若二叉树中无结点K或结点K无父结点则输出0。
输入样例:
1 5 8 0 0 0 6 0 0
3
8
1
6
输出样例:
5
0
1
代码长度限制
16 KB
时间限制
300 ms
内存限制
20 MB
栈限制
131000 KB
```
Q3
```
给定非空二叉树的中根序列和后根序列,请编写程序创建该二叉树,计算其高度和先根序列;如给定的中根和后根序列不合法,则亦能识别。
输入格式:
输入包含多组数据不超过10组每组为两行字符串第一行表示某二叉树的后根序列第二行表示其中根序列。结点的值均为A-Z的大写字母故二叉树结点个数不超过26且保证输入的两个序列都是结点的全排列但不一定是合法的中根和后根序列。输入保证不是空二叉树。
输出格式:
对于每组数据如果输入的序列不合法不是同一棵树的中根序列和后根序列则输出INVALID若输入序列合法输出为两行第一行为一个整数表示该二叉树的高度第二行为一个字符串表示该二叉树的先根序列。
输入样例1:
CEFDBHGA
CBEDFAGH
CBEDFAGH
CEFDBHGA
BCA
CAB
输出样例1:
3
ABCDEFGH
INVALID
INVALID
代码长度限制
16 KB
时间限制
50 ms
内存限制
64 MB
栈限制
8192 KB
```