一 問題描述:
輸入一個整數和一棵二元樹。從樹的根結點開始往下訪問一直到葉結點所經過的所有結點形成一條路徑。列印出和與輸入整數相等的所有路徑。
,輸入整數70 和如下二叉排序樹
輸出為:30 15 25
二 解題思路:
利用二叉樹的前序走訪,
注意:路徑是從根到葉子的所有節點的序列
三: 代碼
/*This is a free Program, You can modify or redistribute it under the terms of GNU*Description:在二叉排序樹中尋找和為某一值的所有路徑*Language: C++*Development Environment: VC6.0*Author: Wangzhicheng*E-mail: 2363702560@qq.com*Date: 2012/11/24*/#include <iostream>#include <cstdlib>#include <vector>#include <stack>#include <algorithm>#include <ctime>using namespace std;/*----------------- 二叉排序樹-----------------------------------------------------------*/template<class Type> //向前引用 class BST;/**二叉排序樹結點類*@data:結點的資料域*@left:結點的左孩子指標*@right:結點的右孩子指標*/template<class Type>class BSTNode {private:Type data;BSTNode *left;BSTNode *right;public:friend class BST<Type>;BSTNode() {}BSTNode(Type data,BSTNode *left=0,BSTNode *right=0) {this->data=data;this->left=left;this->right=right;}BSTNode(const BSTNode &node) {this->data=node.data;this->left=node.left;this->right=node.right;}BSTNode & operator=(const BSTNode &node) {this->data=node.data;this->left=node.left;this->right=node.right;return *this;}void setData(Type& data) {this->data=data;}Type getData() const {return this->data;}void setLeft(BSTNode *left) {this->left=left;}BSTNode * getLeft() const {return this->left;}void setRight(BSTNode *right) {this->right=right;}BSTNode * getRight() const {return this->right;}};/**二叉排序樹類*@root:二叉排序樹根指標*@v:要建立二叉排序樹所需資料的向量*/template<class Type>class BST {private:BSTNode<Type> *root; //二叉排序樹的根指標vector<Type>v; //存放資料域的向量stack<Type>Stack; //輔助棧,用來存放路徑/**向二叉排序樹插入一個新的資料*@root:根指標*@data:一個新的資料*/void Insert(BSTNode<Type> *&root,const Type &data) {if(!root) {root=new BSTNode<Type>(data,0,0);}else if(root->getData()>=data) {Insert(root->left,data);}else {Insert(root->right,data);}}/**中序遍曆二叉排序樹*@p:初始值是指向二叉排序樹的根結點*/void VisitBST_In_order(BSTNode<Type> *p) {if(!p) return;VisitBST_In_order(p->getLeft());cout<<p->getData()<<" ";VisitBST_In_order(p->getRight());}/**按照逆向中序遍曆尋找 即右孩子->根->左孩子*/void SearchTopN(BSTNode<Type> *p) { static int n;if(!p) return;else {if(n<N) SearchTopN(p->getRight());if(++n<=N) TopN.push_back(p->getData());if(n<N) SearchTopN(p->getLeft());}}/**檢查棧中所有元素的和是不是等於num*如果是,函數返回true*/bool CheckStack(Type num) {stack<Type>temp; //輔助棧Type s=0; //用來求和 while(Stack.empty()==false) {s+=Stack.top();temp.push(Stack.top());Stack.pop();}while(temp.empty()==false) {Stack.push(temp.top());temp.pop();}if(s==num) return true;else return false;}/**列印棧中資料*/void PrintStack() {static int k;stack<Type>temp; //輔助棧while(Stack.empty()==false) {temp.push(Stack.top());Stack.pop();}cout<<"第"<<++k<<"條路徑是:"<<endl;while(temp.empty()==false) {cout<<temp.top()<<" ";Stack.push(temp.top());temp.pop();}cout<<endl;}void PrintPathEqualToAInteger(BSTNode<Type> *p,Type num) {if(p) {Stack.push(p->getData());PrintPathEqualToAInteger(p->left,num);PrintPathEqualToAInteger(p->right,num);/**如果符合條件,則列印出路徑*!p->getLeft() && !p->getRight()可以保證棧頂元素是葉子節點的元素*此時儲存在棧裡的是一條從根節點到達葉子節點的路徑*/if(!p->getLeft() && !p->getRight() &&CheckStack(num)==true) PrintStack();if(Stack.empty()==false) Stack.pop();}}public:/**二叉排序樹的建構函式*@input:輸入的向量,儲存著等待建立二叉排序樹的資料*此函數將向量input的內容儲存到向量v中,並且對v進行混洗,一定程度上避免產生單支樹*/BST(vector<Type>&input) {v.clear();vector<Type>::iterator it;for(it=input.begin();it!=input.end();it++) {v.push_back(*it);}//random_shuffle(v.begin(),v.end()); //將向量混洗,避免產生單支二叉排序樹root=0;}/**建立二叉排序樹*/void Create() {vector<Type>::iterator it;for(it=v.begin();it!=v.end();it++) {Insert(root,*it);}}void show() {VisitBST_In_order(root);cout<<endl;}void PrintPathEqualToAInteger(const Type &num) {PrintPathEqualToAInteger(root,num);}};void main() {srand(unsigned(time(0)));int N=6;int input[]={30,15,5,25,40,35};vector<int>v(input,input+N);BST<int>bst(v);bst.Create();bst.show();int num;cout<<"請輸一個整數:";cin>>num;if(!cin.good()) {cerr<<"輸入資料格式錯誤,程式退出!"<<endl;exit(1);}bst.PrintPathEqualToAInteger(num);}
四 測試