😭
This commit is contained in:
+49
-49
@@ -1,49 +1,49 @@
|
||||
/*
|
||||
* 以字符串的形式定义一棵二叉树的先序序列,若字符是‘#’, 表示该二叉树是空树,否则该字符是相应结点的数据元素。读入相应先序序列,建立二叉链式存储结构的二叉树,然后中序遍历该二叉树并输出结点数据。
|
||||
* 输入格式:
|
||||
* 字符串形式的先序序列(即结点的数据类型为单个字符)
|
||||
* 输出格式:
|
||||
* 中序遍历结果
|
||||
* 输入样例:
|
||||
* 在这里给出一组输入。例如:
|
||||
* ABC##DE#G##F###
|
||||
* 输出样例:
|
||||
* CBEGDFA
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "BiTree.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
string input;
|
||||
cout << "请输入二叉树的先序序列(如ABC##DE#G##F###):";
|
||||
cin >> input;
|
||||
|
||||
size_t index = 0;
|
||||
BiTree<char>* root = BiTree<char>::buildFromPreorderString(input, index); // 构建字符型二叉树
|
||||
|
||||
ostringstream oss;
|
||||
if (root)
|
||||
{
|
||||
// 中序遍历
|
||||
root->inorderTraversal(oss);
|
||||
cout << "中序遍历结果:" << oss.str() << endl;
|
||||
|
||||
// 打印树结构
|
||||
cout << "该树结构为:" << endl;
|
||||
cout << root->toString();
|
||||
|
||||
// 销毁树,释放内存
|
||||
root->destroy();
|
||||
delete root;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "输入的二叉树为空。" << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
* 以字符串的形式定义一棵二叉树的先序序列,若字符是‘#’, 表示该二叉树是空树,否则该字符是相应结点的数据元素。读入相应先序序列,建立二叉链式存储结构的二叉树,然后中序遍历该二叉树并输出结点数据。
|
||||
* 输入格式:
|
||||
* 字符串形式的先序序列(即结点的数据类型为单个字符)
|
||||
* 输出格式:
|
||||
* 中序遍历结果
|
||||
* 输入样例:
|
||||
* 在这里给出一组输入。例如:
|
||||
* ABC##DE#G##F###
|
||||
* 输出样例:
|
||||
* CBEGDFA
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "BiTree.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
string input;
|
||||
cout << "请输入二叉树的先序序列(如ABC##DE#G##F###):";
|
||||
cin >> input;
|
||||
|
||||
size_t index = 0;
|
||||
BiTree<char>* root = BiTree<char>::buildFromPreorderString(input, index); // 构建字符型二叉树
|
||||
|
||||
ostringstream oss;
|
||||
if (root)
|
||||
{
|
||||
// 中序遍历
|
||||
root->inorderTraversal(oss);
|
||||
cout << "中序遍历结果:" << oss.str() << endl;
|
||||
|
||||
// 打印树结构
|
||||
cout << "该树结构为:" << endl;
|
||||
cout << root->toString();
|
||||
|
||||
// 销毁树,释放内存
|
||||
root->destroy();
|
||||
delete root;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "输入的二叉树为空。" << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user