总结一下模板\(≧▽≦)/
2014-02-20 15:46
381 查看
二叉树的各种操作:
有先序和中序求后序:
中序和后序求先序:
字典树:参考<Message Flood>
图的基础知识:之前bfs也不太会用,就当也算是bfs的模板吧
并查集
整理的不是很好。。但是已经是我这个星期所学的知识都在这里了。。嘿嘿~
#include<stdio.h> #include<string.h> #include<malloc.h> #include<stdlib.h> typedef struct node { char data; struct node *lchild,*rchild; }*Tree; void CreateBitree(Tree &p) //建树 { char ch; scanf("%c",&ch); if(ch==',') p=NULL; else { p=new node; p->data=ch; CreateBitree(p->lchild); CreateBitree(p->rchild); } return ; } void xpreorder(Tree p) //输出先序 { if (p) { printf("%c ",p->data); xpreorder(p->lchild); xpreorder(p->rchild); } } void zpreorder(Tree p) //输出中序 { if(p) { zpreorder(p->lchild); printf("%c",p->data); zpreorder(p->rchild); } } void hpreorder(Tree p) //输出后序 { if(p) { hpreorder(p->lchild); hpreorder(p->rchild); printf("%c",p->data); } } void ycount(Tree p,int &count) //求二叉树叶子节点 { if(p) { if(p->lchild==NULL&&p->rchild==NULL) { count++; return ; } if(p->lchild) ycount(p->lchild,count); if(p->rchild) ycount(p->rchild,count); } } int sleaf(Tree p) //求二叉树深度 { int high=0; if(!p) return high; int n=sleaf(p->lchild); int m=sleaf(p->rchild); high=m>n?m:n; return high+1; } int main() { node *p; CreateBitree(p); xpreorder(p); printf("\n"); zpreorder(p); printf("\n"); hpreorder(p); printf("\n"); int countleaf=0; ycount(p,countleaf); printf("%d\n",countleaf); printf("%d\n",sleaf(p)); return 0; }
有先序和中序求后序:
#include<stdio.h> #include<string.h> #include<malloc.h> struct node { char data; struct node *lchild,*rchild; }; void Bian(struct node *p) { if(p->lchild!=NULL) Bian(p->lchild); if(p->rchild!=NULL) Bian(p->rchild); printf("%c",p->data); } struct node*buildtree(struct node *p,char *pre,char*mid,int n) { if(n<=0) return NULL; p=(struct node*)malloc(sizeof(struct node)); p->data=pre[0]; //先序的第一个数是根节点 int q=strchr(mid,pre[0])-mid; //根节点在中序中的位置好找到左子树和右子树 p->lchild=buildtree(p->lchild,pre+1,mid,q); p->rchild=buildtree(p->rchild,pre+q+1,mid+q+1,n-q-1); //无限递归 return p; } int main() { char pre[1001],mid[1001]; gets(pre); gets(mid); int n=strlen(pre); struct node *root; root=buildtree(root,pre,mid,n); Bian(root); printf("\n"); return 0; }
中序和后序求先序:
void Bian(struct node *p) { if(p) { printf("%c",p->data); Bian(p->lchild); Bian(p->rchild); } } struct node*buildtree(struct node *p,char *mid,char*aft,int n) { if(n<=0) return NULL; p=(struct node*)malloc(sizeof(struct node)); p->data=aft[n-1]; int q=strchr(mid,aft[n-1])-mid; p->lchild=buildtree(p->lchild,mid,aft,q); p->rchild=buildtree(p->rchild,mid+q+1,aft+q,n-q-1); return p; }
字典树:参考<Message Flood>
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<malloc.h> char str1[21000][15],str2[15]; struct node { int flag; struct node *next[26]; //数字定义10,英文字母定义26 }; struct node *creat() { int i; struct node *p; p=new node; for(i=0; i<26; i++) { p->next[i]=NULL; } p->flag=0; return p; } //是根节点的26个方向都指向空 void Insert(struct node *p,char *s) //建字典树树 { int i,n,num; n=strlen(s); for(i=0; i<n; i++) { if(s[i]>='A'&&s[i]<='Z') num=s[i]-'A'; else if(s[i]>='a'&&s[i]<='z') num=s[i]-'a'; if(p->next[num]==NULL) p->next[num]=creat();//如果p指向的为空,则继续向下建树 p=p->next[num]; } p->flag=1;//记录完整的字符串 } int Search(struct node *p,char *s) //查找 { int i,n,num; n=strlen(s); for(i=0; i<n; i++) { if(s[i]>='A'&&s[i]<='Z') num=s[i]-'A'; else if(s[i]>='a'&&s[i]<='z') num=s[i]-'a'; if(p->next[num]==NULL) return 1; p=p->next[num]; } if(p->flag==1) { p->flag=0; //防止被查找的有重复的,被查找过便使其为0 return 0; } return 1; } void Delete(struct node*p) //释放内存!!很重要!!否则会超时 { int i; for(i=0; i<26; i++) { if(p->next[i]!=NULL) Delete(p->next[i]); } free(p); } int main() { int i,n,m; struct node *p; while(~scanf("%d",&n)&&n) { scanf("%d",&m); p=creat(); for(i=0; i<n; i++) { scanf("%s",str1[i]); } for(i=0; i<m; i++) { scanf("%s",str2); Insert(p,str2); } int s=0; for(i=0;i<n;i++) { if(Search(p,str1[i])) ++s; } printf("%d\n",s); Delete(p); } return 0; }
图的基础知识:之前bfs也不太会用,就当也算是bfs的模板吧
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<malloc.h> struct node { int x; int y; int ans; }q[2100]; int map[11000][1100],v[1100]; int k; void bfs(int x) { int e=0,s=0,i; node t,f; t.x=x,t.ans=0; q[e++]=t; v[t.x]=1; while(s<e) { t=q[s++]; if(t.x==1) { printf("%d\n",t.ans); return ; } for(i=1; i<=k; i++) { f.x=i; if(v[f.x]==0&&map[t.x][f.x]==1) { f.ans=t.ans+1; q[e++]=f; v[f.x]=1; } } } printf("NO\n"); return ; } int main() { int n,m,i,u,w,t; while(~scanf("%d%d",&k,&m)) { memset(map,0,sizeof(map)); memset(v,0,sizeof(v)); for(i=0; i<m; i++) { scanf("%d%d",&u,&w); map[u][w]=1; //有向的 //map[w][u]=1; 若都写为无向的 } bfs(k); } return 0; }
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<malloc.h> int map[11000][100]; int v[100]; int flag,k; struct node { int u,v,w; node *next; }*head[11000]; void dfs(int i) { if(flag==0) { printf("%d",i); flag=1; } else printf(" %d",i); for(int j=1;j<k;j++) { if(map[i][j]==1&&v[j]==0) //无限递归 只有当之间有路且没被访问过 { v[j]=1; //标志被访问过 dfs(j); } } } int main() { struct node *p; int n,m,i,x,y; scanf("%d",&n); while(n--) { flag=0; scanf("%d%d",&k,&m); memset(map,0,sizeof(map)); memset(v,0,sizeof(v)); for(i=0;i<m;i++) { scanf("%d%d",&x,&y); map[x][y]=1; map[y][x]=1; //无向的 } for(i=0;i<k;i++) //从小的编号开始 { if(v[i]==0) dfs(i); } printf("\n"); } return 0; }
并查集
#include "stdio.h" int bin[1002]; int findx(int x) //查找根结点 { int i,j int r=x; while(bin[r] !=r) r=bin[r]; i = x; while (i != r) //本循环修改查找路径中所有节点 { j = bin[i]; bin[i] = r; i = j; } return r; //路径压缩。。很省时间 } void merge(int x,int y) { int fx,fy; fx = findx(x); fy = findx(y); if(fx != fy) bin[fx] = fy; } int main() { int n,m,i,x,y,count; while(scanf("%d",&n),n) { for(i=1; i<=n; i++) bin[i] = i; for(scanf("%d",&m);) { scanf("%d %d",&x,&y); merge(x,y); } count=0; for(, i=1; i<=n; i++) if(bin[i] == i) count ++; printf("%d\n",count-1); } }
整理的不是很好。。但是已经是我这个星期所学的知识都在这里了。。嘿嘿~
相关文章推荐
- KMP基础模板- - -看了很多人的模版,总结一下
- HDU 1016 Prime Ring Problem DFS(总结了一下模板)
- C++ 函数模板和类模板简单总结一下
- Smarty模板的逻辑运算符号稍微做一下总结
- 总结一下java获取路径几种途径
- 今天总结一下我对Fragment的理解(碎片的入栈与出栈,碎片的嵌套)
- 很久没更新了,总结一下最近的工作进展(附截图)
- 软件工程课程设计终于告一段落。小小总结一下。
- WinCE学习顺序_看到群里一高手写的计划,计划实行以下,并且争取将进度多做一下总结。
- 今天复习C++的const,又学到了很多内容,总结一下[原创]
- Flask-Jinja2模板学习总结
- 总结一下工作中用到的Mybatis业务逻辑
- 总结一下刚刚写的程序中的C++各种类型的转换string,QString,char*, BYTE等等
- 总结一下在学习WebService中学习遇到的一些不懂的概念
- 总结一下ERP .NET程序员必须掌握的.NET技术
- 总结一下这两天的同济规划设计院和infosys的面试吧
- 总结一下vim常用编辑命令
- 总结一下这两天所学RecyclerView
- 项目里总结出来的log4j模板
- 自己总结一下关联关系映射中的OneToMany,ManyToOne,单向,双向注意的事项!ps:例子