生成所有可能的搜索二叉樹
阿新 • • 發佈:2019-02-03
parent data names 搜索 遞歸 pub auto ren ostream
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 ;
生成所有可能的搜索二叉樹