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 ;

  • 相关阅读:
    uboot的配置文件在哪里
    uboot的flash sectore的大小由哪个宏指定
    openwrt中的append-ubi定义在哪里
    vi在行首插入注释符号#
    openwrt设置uboot环境变量在flash上的存储地址
    git重命名分支名
    ifconfig设置ip时出现提示 ifconfig: SIOCSIFFLAGS: Address not available
    jquery checkbox选中、改变状态、change和click事件
    jQuery操作复选框checkbox技巧总结 ---- 设置选中、取消选中、获取被选中的值、判断是否选中等
    checkbox选择根据后台List数据进行回显
  • 原文地址:https://www.cnblogs.com/l2017/p/10349473.html
Copyright © 2011-2022 走看看