标签:cout \n ast new t bsp data int printf 节点
product_all_tree(0, 5);
递归返回子树的指针集合。作为 左子树或右子树。
从而构建整颗树。
#include<iostream>
#include<vector>
using namespace std;
//double p[1000]{ 0,0.15,0.10,0.05,0.10,0.20 };
double p[1000]{ 1,2,3,4,5,6,7,8,9 };
#define N 5
class Tree
{
public:
Tree*parent_;
Tree*l_c_;
Tree*r_c_;
double data_;
Tree(double a) {
parent_ = nullptr;
l_c_ = nullptr;
r_c_ = nullptr;
data_ = a;
}
};
vector<Tree*> product_all_tree(int start,int end)//start end 起始下标,结束下标
{
vector<Tree*> trees;
if (start > end)
{
trees.push_back(nullptr);
return trees;
}
if (start ==end)
{
trees.push_back(new Tree(p[start]));
return trees;
}
//以i作为分界点获取左右两部分的子树集合
for (int i = start; i <= end; ++i)
{
auto lefts = product_all_tree(start,i-1);
auto rights = product_all_tree(i+1, end);
for(auto left: lefts)
for (auto right : rights)
{
Tree* root = new Tree(p[i]);
root->l_c_ = left;
root->r_c_ = right;
trees.push_back(root);
}
}
return trees;
}
void PrintTree(Tree*T, int Layer=1)
{/*按竖向树状打印的二叉树*/
int i;
if (T == NULL)
return;
PrintTree(T->r_c_, Layer + 1);
for (i = 0; i<Layer; i++)
printf(" ");
printf("%d\n", static_cast<int>(T->data_)); //按逆中序输出结点,用层深决定结点的左右位置
PrintTree(T->l_c_, Layer + 1);
}
int main()
{
auto trees=product_all_tree(0, 5);//一共六个数
for (auto tree : trees)
{
PrintTree(tree);
cout << "-----------------"<< endl ;
}
}
结果:二叉树的输出有点丑 横向的,主要关注点,这是个搜索二叉树,
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
6
5
4
3
2
1
--------------------------
请按任意键继续. . .
cout << "--------------------------" << endl ;
标签:cout \n ast new t bsp data int printf 节点
原文地址:https://www.cnblogs.com/l2017/p/10349473.html