问题描述 :
目的:使用C++模板设计并逐步完善二叉树的抽象数据类型(ADT)。
内容:(1)请参照链表的ADT模板,设计二叉树并逐步完善的抽象数据类型。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。参考教材、课件,以及网盘中的链表ADT原型文件,自行设计二叉树的ADT。)
注意:二叉树ADT的基本操作的算法设计很多要用到递归的程序设计方法。
(2)基本操作:二叉树的二叉链表存储形式的建立,完成后将其加入到二叉树的ADT基本操作集中。
输入数据为层次次序,要求设计一个算法,将二叉树转化为二叉链表的存储形式。
输入说明 :
第一行:表示无孩子或指针为空的特殊分隔符
第二行:二叉树的层次次序序列(结点元素之间以空格分隔)
输出说明 :
第一行:二叉树先序遍历结果
第二行:二叉树中序遍历结果
第三行:二叉树后序遍历结果
输入范例 :
#
+ - 62 * / # # + 3 400 + 12 14 # # # # 30 * # # # # # # 10 5
输出范例 :
+,-,*,+,12,14,3,/,400,+,30,*,10,5,62
12,+,14,*,3,-,400,/,30,+,10,*,5,+,62
12,14,+,3,*,400,30,10,5,*,+,/,-,62,+
template
struct TreeNode {
T val;
int flag = 1;
TreeNode* left;
TreeNode* right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(T x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(T x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};
#include
#include
#include
#include
using namespace std;
template
TreeNode* build(T empty) {
T val;
cin >> val;
if (val != empty) {
TreeNode* node = new TreeNode(val);
node->left = build(empty);
node->right = build(empty);
return node;
}
else {
return NULL;
}
}
template
void preorder(TreeNode* tree, vector& list) {
if (tree) {
if(tree->val != "")list.push_back(tree->val);
preorder(tree->left, list);
preorder(tree->right, list);
}
}
template
void inorder(TreeNode* tree, vector& list) {
if (tree) {
inorder(tree->left, list);
if (tree->val!="")list.push_back(tree->val);
inorder(tree->right, list);
}
}
template
TreeNode* seek(TreeNode* t, T target) {
if (!t)return NULL;
if (t->val == target)return t;
auto l = seek(t->left, target), r = seek(t->right, target);
if (l)return l;
else return r;
}
template
TreeNode* seekparent(TreeNode* t, T target) {
if (!t)return NULL;
if (t->val == target)return NULL;
if (t->left && t->left->val == target || t->right && t->right->val == target)
return t;
else {
auto l = seekparent(t->left, target), r = seekparent(t->right, target);
if (l)return l;
else return r;
}
}
template
void postorder(TreeNode* tree, vector& list) {
if (tree) {
postorder(tree->left, list);
postorder(tree->right, list);
if (tree->val != "")list.push_back(tree->val);
}
}
template
void out(TreeNode* head) {
vectorpr, in, po;
preorder(head, pr);
inorder(head, in);
postorder(head, po);
for (int i = 0; i < pr.size(); ++i)
cout << pr[i] << ",\n"[i == pr.size() - 1];
for (int i = 0; i < in.size(); ++i)
cout << in[i] << ",\n"[i == in.size() - 1];
for (int i = 0; i < po.size(); ++i)
cout << po[i] << ",\n"[i == po.size() - 1];
}
#include
template
TreeNode* buildlayer(T empty) {
string str;
getline(cin, str);
stringstream ss(str);
TreeNode* head = new TreeNode("");
queue< TreeNode*>q;
q.push(head);
while (ss >> str) {
TreeNode* obj = q.front();
q.pop();
if (str != empty) {
obj->val = str;
obj->left= new TreeNode("");
obj->right = new TreeNode("");
q.push(obj->left);
q.push(obj->right);
}
}
while (q.size()) {
q.front()->flag = 0;
q.pop();
}
return head;
}
int main() {
TreeNode* head = NULL;
string empty, str;
getline(cin, empty);
head = buildlayer(empty);
out(head);
return 0;
}