一 问题描述:
输入一个整数和一棵二元树。从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。打印出和与输入整数相等的所有路径。
如图,输入整数70 和如下二叉排序树
输出为:30 15 25
二 解题思路:
利用二叉树的前序遍历,
注意:路径是从根到叶子的所有节点的序列
#include <iostream>
#include <cstdlib>
#include <vector>
#include <stack>
#include <algorithm>
#include <ctime>
using namespace std;
template<class Type>
class BST;
int N=5;
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;
}
};
template<class Type>
class BST
{
private:
BSTNode<Type>* root;
vector<Type> v;
stack<Type> Stack;
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);
}
}
void VisitBST_In_order(BSTNode<Type> *p)
{
if(!p)
{
return;
}
VisitBST_In_order(p->getLeft());
cout << p->getData() << " ";
VisitBST_In_order(p->getRight());
}
/** No used in this program.
void SearchTopN(BSTNode<Type>* p)
{
static int n;
if(!p)
{
return;
}
else
{
if (n < N)
{
SearchTopN(p->getRight());
}
if (++n <= N)
{
v.push_back(p->getData());
}
if (n < N)
{
SearchTopN(p->getLeft());
}
}
}
*/
bool CheckStack(Type num)
{
stack<Type> temp;
Type s = 0;
bool result;
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)
{
result = true;
}
else
{
result = false;
}
return result;
}
void PrintStack()
{
static int k;
stack<Type>temp;
while (Stack.empty() == false)
{
temp.push(Stack.top());
Stack.pop();
}
cout << ++k<<"th path is:" << 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);
if (!p->getLeft() && !p->getRight() && CheckStack(num) == true)
{
PrintStack();
}
if (Stack.empty() == false)
{
Stack.pop();
}
}
}
public:
BST(vector<Type>& input)
{
v.clear();
typedef typename vector<Type>::iterator iterator;
iterator it;
for(it = input.begin(); it != input.end(); it++) {
v.push_back(*it);
}
// random_shuffle(v.begin(),v.end());
root=0;
}
void Create()
{
typedef typename vector<Type>::iterator iterator;
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);
}
};
int main()
{
//srand(unsigned(time(0)));
int input[] = {30, 15, 5, 25, 40};
vector<int> v(input,input+N);
BST<int> bst(v);
bst.Create();
bst.show();
int num;
cout << "please input test integer N:";
cin >> num;
if(!cin.good())
{
cerr << "data format error, exit." << endl;
exit(1);
}
bst.PrintPathEqualToAInteger(num);
return 0;
}