Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 按顺序遍历打印所有二叉树_C++_Binary Tree - Fatal编程技术网

C++ 按顺序遍历打印所有二叉树

C++ 按顺序遍历打印所有二叉树,c++,binary-tree,C++,Binary Tree,在一次采访中遇到了这个问题。 给定二叉树的有序遍历。从中打印所有可能的二叉树 初步想法: 假设数组中只有2个元素。比如说2,1。 然后选择两棵可能的树 2 \ 1 1 / 2 如果3个元素表示,2,1,4。然后我们有5棵可能的树 2 1 4 2 4 \

在一次采访中遇到了这个问题。 给定二叉树的有序遍历。从中打印所有可能的二叉树

初步想法:

假设数组中只有2个元素。比如说2,1。 然后选择两棵可能的树

              2 
               \
                1     
    1
   /
   2  
如果3个元素表示,2,1,4。然后我们有5棵可能的树

 2               1            4           2            4
  \             / \          /             \          /
   1           2   4        1               4        2
    \                      /               /          \
     4                    2               1            1
所以,基本上如果我们有n个元素,那么我们有n-1个分支(child,/或)。 我们可以按任何顺序排列这些n-1分支。 对于n=3,n-1=2。所以,我们有两个分支。 我们可以通过以下方式安排这两个分支机构:

  /     \         \           /         /\
 /       \        /           \
初步尝试:

struct  node *findTree(int *A,int l,int h)
{
    node *root = NULL;
    if(h < l)
            return NULL;
    for(int i=l;i<h;i++)
    {
            root = newNode(A[i]);
            root->left = findTree(A,l,i-1);
            root->right = findTree(A,i+1,h);
            printTree(root);
            cout<<endl;
    }

}
struct node*findTree(int*A、int-l、int-h)
{
node*root=NULL;
if(hright=findTree(A,i+1,h);
打印树(根);

我要写一个函数来构造树,另一个函数用来打印树

树木的建造过程如下:

#include <vector>
#include <iostream>
#include <boost/foreach.hpp>

struct Tree {
    int value;
    Tree* left;
    Tree* right;

    Tree(int value, Tree* left, Tree* right) :
        value(value), left(left), right(right) {}
};

typedef std::vector<Tree*> Seq;

Seq all_trees(const std::vector<int>& xs, int from, int to)
{
    Seq result;
    if (from >= to) result.push_back(0);
    else {
        for (int i = from; i < to; i++) {
            const Seq left = all_trees(xs, from, i);
            const Seq right = all_trees(xs, i + 1, to);
            BOOST_FOREACH(Tree* tl, left) {
                BOOST_FOREACH(Tree* tr, right) {
                    result.push_back(new Tree(xs[i], tl,  tr));
                }
            }
        }
    }
    return result;
}

Seq all_trees(const std::vector<int>& xs)
{
    return all_trees(xs, 0, (int)xs.size());
}
#包括
#包括
#包括
结构树{
int值;
树*左;
树*对;
树(int值,树*左,树*右):
值(value),左(左),右(右){
};
typedef std::向量序列;
Seq所有_树(const std::vector&xs,int from,int to)
{
Seq结果;
如果(从>=到)结果。向后推(0);
否则{
for(int i=from;i
请注意,对于根值,可以从根值的左侧和右侧的值构造多个树。这些左树和右树的所有组合都包括在内

编写漂亮的打印机只是一个练习(一个枯燥的练习),但我们可以测试该函数是否确实构建了预期数量的树:

int main()
{
    const std::vector<int> xs(3, 0); // 3 values gives 5 trees.
    const Seq result = all_trees(xs);
    std::cout << "Number of trees: " << result.size() << "\n";
}
intmain()
{
const std::vector xs(3,0);//3个值给出5棵树。
const Seq result=所有_树(xs);

std::cout这个问题可以很好地分解为子问题。给定一个有序遍历,在选择根之后,我们知道前面的所有内容都是左子树,后面的所有内容都是右子树(可能是空的)

因此,为了枚举所有可能的树,我们只需尝试根的所有可能值,并递归地求解左、右子树(尽管这样的树的数量增长非常快!)


antonakos提供的代码显示了如何做到这一点,尽管该解决方案可能会使用比预期更多的内存。可以通过向递归添加更多的状态来解决这一问题,这样它就不必保存左侧和右侧的答案列表,并在最后合并它们;而是嵌套这些进程,并在找到每个树时打印它们。

In你的例子是(1,2,4),最后的例子应该是从上到下的4-1-2吗?@ChuckBlumreich数组是2,1,4。我猜数字是正确的。有趣的问题,但我不确定你的图表。我将“顺序”遍历解释为“访问左子节点,访问节点,访问右子节点”(与预订“拜访N,拜访L,拜访R”和预订后“拜访L,拜访R,拜访N”形成对比。如果是这样,那么这对
(2,1)
的两棵树是
(root=2,R child=1)
如图所示和
(左child=2,root=1)
,这不是您绘制的图表。我对更复杂的图表也有类似的担忧,但我可能完全误解了一些东西。我同意@Jonathan的观点,这些图表不正确。同意..是一个愚蠢的错误。现在更正了图表