昨天的上传的是之前没改好的,新改的没保存,重新写的,暂时无错了。
#include <iostream>
#include <deque>
using namespace std;
struct BSTNode
{
int value;
BSTNode* left;
BSTNode* right;
BSTNode()
{
value = 0;
left = NULL;
right = NULL;
}
};
deque<BSTNode*> sq;
void insert(BSTNode* &p, int elem)
{
if(p == NULL)
{
BSTNode* pnew = new BSTNode();
pnew->left = 0;
pnew->right =0;
pnew->value = elem;
p = pnew;
return;
}
if(elem > p->value)
{
insert(p->right, elem);
}
else if(elem < p->value)
{
insert(p->left, elem);
}
else
cout << "The Same!" << endl;
}
void BFS(BSTNode* root)
{
if(!root)
return;
BSTNode* p = root;
sq.push_back(p);
while(sq.size())
{
p = sq.front();
sq.pop_front();
cout << p->value << endl;
if(p->left)
sq.push_back(p->left);
if(p->right)
sq.push_back(p->right);
}
}
void print_deque(deque<BSTNode*> que)
{
deque<BSTNode*>::iterator iter = que.begin();
for(; iter != que.end(); ++iter)
{
cout << (*iter)->value << endl;
}
}
int main(void)
{
BSTNode* root = 0;
insert(root, 8);
insert(root, 6);
insert(root, 10);
insert(root, 5);
insert(root, 7);
insert(root, 9);
insert(root, 11);
BFS(root);
// print_deque(sq);
return 0;
}