poj1577
来源:互联网 发布:竞价推广账户优化 编辑:程序博客网 时间:2024/06/04 19:13
就是搜索二叉树,树的左儿子比她小,右儿子比她大,本来用递归插入,不知道为什么输不出。。。。。。
#include <iostream>
#include <string>
#include <stack>
using namespace std;
struct BinNode
{
char letter;
BinNode *left,*right;
BinNode(char val='*'):letter(val),left(NULL),right(NULL){}
};
class BinTree
{
public:
BinTree():root(NULL){}
void buildtree()
{
string str;
stack<string>s;
while(cin>>str)
{
if(str[0]=='*'||str[0]=='$')
{
while(!s.empty())
{
string x=s.top();
//cout<<x<<endl;
s.pop();
int len=x.size();
for(int i=0;i<len;i++)
{
//cout<<x[i]<<endl;
insert(x[i]);
}
}
if(root==NULL)cout<<"YES\n";
preorder(root);
cout<<endl;
root=NULL;
if(str[0]=='$')return;
}
else s.push(str);
}
}
private:
BinNode *root;
void insert(const char c) {
if (root==NULL)
root=new BinNode(c);
else {
BinNode *ptr=root;
BinNode *pre=NULL;
while (ptr) {
if (ptr->letter==c)
return;
else if(ptr->letter>c) {
pre=ptr;
ptr=ptr->left;
} else {
pre=ptr;
ptr=ptr->right;
}
}
if (pre->letter>c)
pre->left=new BinNode(c);
else
pre->right=new BinNode(c);
}
}
void preorder(BinNode *r)
{
if(r==NULL)return;
cout<<r->letter;
preorder(r->left);
preorder(r->right);
delete r;
}
};
int main()
{
BinTree BT;
BT.buildtree();
return 0;
}
- poj1577
- POJ1577
- POJ1577 二叉排序树
- 二叉树 poj1577
- poj1577 Falling Leaves
- poj1577~Falling Leaves~二叉排序树~
- poj1577 Falling Leaves
- poj1577 二叉排序树基础练习题
- poj1577解题报告
- poj1577 Falling Leaves 二叉排序树
- POJ1577 树的遍历
- poj1577 二叉查找树复原,然后中序遍历输出
- 第三周周赛——基础数据结构结业场(坚持就会有AK,题目出自codeforces 633C,633D,631B,651A,651C以及poj1577)
- POJ1577 二叉树的创建及先序遍历(给定一些二叉树中的值,大于根的排在右边,小于根的排在左边)
- mysql 外键约束(未完)
- CVG Talks on CHIST-ERA 2015
- Hibernate5.0.2 Unknown Entity 如何创建SessionFactory
- 疯狂iOS讲义(下)之多线程
- 黑马程序员—Java基础—多线程2
- poj1577
- 顺序容器--添加及访问元素
- 如何使用标准的java方式获得日期
- npm发布属于自己的开源模块
- 计算机视觉、模式识别学习资源整理
- Lesson 1 Hello World
- js中的数字如何四舍五入
- java多线程之死锁
- Leetcode #200 Number of Islands