(p171)基数树
来源:互联网 发布:锦衣卫知乎 编辑:程序博客网 时间:2024/06/04 19:56
思路很简单,把浅深色的节点设为1,其余的设为0,先序遍历用s存储当前路径对应的字符串,遇到1输出即可
#include<stdio.h>#include<stdlib.h>#include<string.h>#define max 100struct node{int f;struct node *l,*r;};void insert(struct node *root,char s[],int dep){struct node *new;if (dep==strlen(s))root->f=1;elseif (s[dep]=='0'){if (root->l==NULL){new=(struct node *)malloc(sizeof(struct node));new->l=NULL;new->r=NULL;new->f=0;root->l=new;insert(new,s,dep+1);}elseinsert(root->l,s,dep+1);}else{if (root->r==NULL){new=(struct node *)malloc(sizeof(struct node));new->l=NULL;new->r=NULL;new->f=0;root->r=new;insert(new,s,dep+1);}elseinsert(root->r,s,dep+1);}}void print(char s[],int l){int i;for (i=0;i<l;i++)printf("%c",s[i]);printf("\n");}void sort(struct node *root,char s[],int dep){if (root->f==1)print(s,dep);if (root->l!=NULL){s[dep]='0';sort(root->l,s,dep+1);}if (root->r!=NULL)/*回溯*/{s[dep]='1';sort(root->r,s,dep+1);}}int main(void){int n,i;char s[max];struct node *root;root=(struct node *)malloc(sizeof(struct node));root->f=0;root->l=NULL;root->r=NULL;scanf("%d",&n);for (i=0;i<n;i++){scanf("%s",s);insert(root,s,0);}sort(root,s,0);return 0;}
0 0
- (p171)基数树
- 树的最大独立集 《算法竞赛入门经典》P171
- 基数(radix)树
- linux 基数树
- radix tree 基数树
- 基数树(radix tree)
- 基数树(radix tree)
- 基数树(radix tree)
- 【数据结构】基数树
- ngx_radix_tree基数树
- 基数树(radix tree)
- RadixTree(基数树)
- 基数树(radix tree)
- 基数树(radix tree)
- 基数树(Radix Tree)
- 基数树(radix tree)
- 基数
- 基数树(radix tree) 基数树(radix tree) (转)
- 浏览器向服务器请求页面的原理
- 浅谈一类积性函数的前缀和
- Mybatis入门实例(注解实现)(3) - SSM(三)
- RF设计天线 PI型匹配layout注意事项–物联网设计小技巧
- thrift学习第二步,一个简单的双向通信demo以及原理讲解,以及注意事项
- (p171)基数树
- linux grep命令详解
- Ubuntu14.0.4下Hbase1.1.3单机模式部署
- BZOJ_P3620 似乎在梦中见过的样子(KMP)
- 一个Java对象到底占多大内存?
- 自己动手丰衣足食之表格排序插件jquery.tableSort.js
- 代码健壮性--理论篇
- mongodb搭建校内搜索引擎——内容查询与排序1.0
- linux/unix下的opt与etc文件夹