zoukankan      html  css  js  c++  java
  • 生成所有可能的搜索二叉树

    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 ", 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 ;

  • 相关阅读:
    软件构造期末复习考点总结
    设计模式
    怎么写测试策略
    FreeRTOS任务创建、启动调度器、任务切换的过程分析——基于ARM-CotexM3
    Altium Designer 创建集成封装库(.IntLib文件)的方法
    Altium Designer多通道设计的原理与应用实例
    我的编程命名风格
    计算机网络学习笔记
    基于串口通信做my_printf时遇到的坑儿
    git常用命令
  • 原文地址:https://www.cnblogs.com/l2017/p/10349473.html
Copyright © 2011-2022 走看看