poj1577~Falling Leaves~二叉排序树~
来源:互联网 发布:弗洛伊德算法 path 编辑:程序博客网 时间:2024/06/07 05:38
题意看的真尼马蛋疼,其实内容很简单,就是说有一颗二叉排序树,每次都把叶子去掉,直到最后连根节点也去掉,给出每次掉落的叶子,问你这棵树的先序遍历。
二叉排序树的概念很简单,就是对于树里的每个节点。它的左孩子总是比它小,它的右孩子总是比它大。这样子从根节点开始寻找某个节点时就非常简单,如果比根节点大就去右子树找,比根节点小就去左子树找。
知道了二叉排序树,那么这题的算法就很简单,最后掉落的是根节点,所以只需要自己创建一个二叉排序树,然后从根节点开始一个个扔进排序树,最后先序输出就可以了
#include<iostream>#include<string>#include<cmath>#include<algorithm>using namespace std;typedef struct Tree{char date; //所放字母int v; //v表示这个节点有没有放入过字母Tree *lchild;Tree *rchild;};void insert(Tree *t,char c){if(t->v==0) //如果这个点还未放入字母,则放入字母{t->lchild=new Tree; //创建两个没放入字母的左右孩子节点t->rchild=new Tree;t->lchild->v=0; t->rchild->v=0;t->date=c;t->v=1;}else{if(c>t->date) //若 c比这个节点的字母要大,则插入右子树中,否则插入左子树{insert(t->rchild,c);}else insert(t->lchild,c);}}void dfs(Tree *T){if(T->v!=0) //按前序遍历,得到的就是按答案{printf("%c",T->date);dfs(T->lchild);dfs(T->rchild);}}int main(){int i,j,k,len,flag;char str[26][26];Tree *root;root=new Tree;flag=1;while(flag){k=0;root->v=0;//初始化根节点while(1) //输入过程……有点蛋疼{gets(str[k++]);if(str[k-1][0]=='*') break;else if(str[k-1][0]=='$') {flag=0;break;}}for(i=k-2;i>=0;i--) //要倒着插入排序树,*和$不算的,所以i从k-2开始{len=strlen(str[i]);for(j=0;j<len;j++)insert(root,str[i][j]);}dfs(root); //遍历输出cout<<endl;}return 0;}
0 0
- poj1577~Falling Leaves~二叉排序树~
- poj1577 Falling Leaves 二叉排序树
- poj1577 Falling Leaves
- poj1577 Falling Leaves
- POJ1577 二叉排序树
- poj1577 二叉排序树基础练习题
- ZOJ 1700 Falling Leaves
- ZOJ-1700 falling leaves
- 699 - The Falling Leaves
- 699 - The Falling Leaves
- UVA699 - The Falling Leaves
- uva_699_The Falling Leaves
- uva_699-The Falling Leaves
- POJ-1577-Falling Leaves
- UVaOJ699---The Falling Leaves
- ZOJ 1700 Falling Leaves
- POJ 1577 Falling Leaves
- PKU 1577 Falling Leaves
- 解决远程连接mysql很慢的方法(mysql_connect 打开连接慢)
- 【图像处理】简单的图像处理软件
- UML之类图(Class Diagram)
- Android DVM 記憶體管理研究分析
- IO复用、多进程和多线程三种并发编程模型
- poj1577~Falling Leaves~二叉排序树~
- 2014多校4,hdu4897,hdu4898,hdu4902,hdu4906题解
- License控制实现原理(20140808)
- URAL1005 - Stone Pile - 动态规划
- ListView的一个小应用
- I'm Telling the Truth(二分匹配)
- JSP引入JS文件路径错误原因分析
- 编写Android.mk中的LOCAL_SRC_FILES的终极技巧
- Android App 内存泄露之调试工具(1)