45 lines
1.2 KiB
C++
45 lines
1.2 KiB
C++
/*
|
|
* 以字符串的形式定义一棵二叉树的先序序列,若字符是‘#’, 表示该二叉树是空树,否则该字符是相应结点的数据元素。读入相应先序序列,建立二叉链式存储结构的二叉树,然后中序遍历该二叉树并输出结点数据。
|
|
* 输入格式:
|
|
* 字符串形式的先序序列(即结点的数据类型为单个字符)
|
|
* 输出格式:
|
|
* 中序遍历结果
|
|
* 输入样例:
|
|
* 在这里给出一组输入。例如:
|
|
* ABC##DE#G##F###
|
|
* 输出样例:
|
|
* CBEGDFA
|
|
*/
|
|
#include <iostream>
|
|
|
|
#include "BiTree.h"
|
|
using namespace std;
|
|
|
|
int main()
|
|
{
|
|
string input;
|
|
cout << "请输入二叉树的先序序列(如ABC##DE#G##F###):";
|
|
cin >> input;
|
|
|
|
size_t index = 0;
|
|
BiTree* root = BiTree::build_from_preorder_string(input, index);
|
|
|
|
ostringstream oss;
|
|
if (root)
|
|
{
|
|
root->inOrder(oss);
|
|
cout << "中序遍历结果:" << oss.str() << endl;;
|
|
root->destory();
|
|
delete root;
|
|
}
|
|
else
|
|
{
|
|
cout << "输入的二叉树为空" << endl;
|
|
}
|
|
|
|
//cout << "该树结构为:" << endl;
|
|
//cout << root->toString() << endl;
|
|
|
|
return 0;
|
|
}
|