生成所有可能的搜索二叉树

Posted l2017

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生成所有可能的搜索二叉树相关的知识,希望对你有一定的参考价值。

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 ;

 


以上是关于生成所有可能的搜索二叉树的主要内容,如果未能解决你的问题,请参考以下文章

算法漫游指北(第十三篇):二叉树的基本概念满二叉树完全二叉树二叉树性质二叉搜索树二叉树定义二叉树的广度优先遍历

C++二叉树进阶

二叉树二叉搜索树中的众数(leetcode501)

二叉树二叉搜索树中的众数(leetcode501)

带你整理面试过程中关于 二叉树二叉搜索树平衡二叉树B 树和 B+树的相关知识

数据结构~基础2~树《二叉树二叉搜索树AVL树B树红黑树》的设计~高度平衡二叉树AVL树