/***************** 运行 环境:http://www.anycodes.cn/zh/ 内容: 基数树 *******************/ #include <stdio.h> #include <stdlib.h> typedef struct RadixNode { char *str; struct RadixNode *lchild, *rchild; } RadixNode; void radix_insert(RadixNode *node, char *str) { int i; for (i = 0; str[i] != ‘\0‘; i++) { if (str[i] == ‘0‘) { if (node->lchild == NULL) node->lchild =(RadixNode*) calloc(sizeof(RadixNode), 1); node = node->lchild; } else { if (node->rchild == NULL) node->rchild = (RadixNode*)calloc(sizeof(RadixNode), 1); node = node->rchild; } } node->str = str; } void radix_preorder_walk(RadixNode *node) { if (node != NULL) { if (node->str != NULL) printf("%s\n", node->str); radix_preorder_walk(node->lchild); radix_preorder_walk(node->rchild); } } int main(void) { RadixNode *root = (RadixNode*)malloc(sizeof(RadixNode)); char ss[5][5]={"01","011","101","1010","1011"}; radix_insert(root, ss[0]); radix_insert(root, ss[1]); radix_insert(root, ss[2]); radix_insert(root, ss[3]); radix_insert(root, ss[4]); radix_preorder_walk(root); return 1; }
原文http://blog.csdn.net/dc_726/article/details/7391988
修改消除了警告与一定编译器造成的错误
时间: 2024-10-23 04:14:49