自己实现了一个IP trie树接口.

在这里保存一下,方便备份以后使用,同时欢迎纠错和交流,希望有大神能指教更高效的算法.

1.头文件如下(iptrie.h)

 1 #ifndef _IP_TRIE_H_
 2 #define _IP_TIRE_H_
 3
 4 #define SPLIT_SIGN "."
 5 #define IP_BINARY_LEN 32
 6
 7 typedef struct ip_trie_node
 8 {
 9     struct ip_trie_node *child[2]; //two child node
10 }ip_trie_node;
11
12 ip_trie_node *create_iptrie_node();
13
14 void insert_iptrie_node(ip_trie_node *root,char ip[]);
15
16 int select_iptrie_node(ip_trie_node *root,char ip[]);
17
18 #endif

2.c文件如下(iptrie.c)

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4
  5 #include "iptrie.h"
  6
  7 /*
  8  *name: itobinary
  9  *
 10  *param:
 11  * num: orignal number; binary_str: dest string; index: the binary str copy index
 12  *
 13  *return:
 14  * void
 15  */
 16 void itobinary(int num,char binary_str[],int index)
 17 {
 18     if(binary_str == NULL)
 19     {
 20         return;
 21     }
 22
 23     int i,bit = 0x01;
 24     for(i = 0; i < 8; i++)
 25     {//conver integer to 8 bit binary str
 26         if((num & bit) != 0)
 27         {//oprater & is lower than !=
 28             binary_str[index + 7 - i] = '1';
 29         }
 30         else
 31         {
 32             binary_str[index + 7 - i] = '0';
 33         }
 34
 35         bit <<= 1; //bit * 2
 36     }
 37 }
 38
 39 /*
 40  *name: convert_ip_binary
 41  *
 42  *param:
 43  * ip:orign ip string; binary_str:dest binary string
 44  *
 45  *return:
 46  * void
 47  */
 48 void convert_ip_binary(char ip[],char binary_str[])
 49 {
 50     if(ip == NULL || binary_str == NULL)
 51     {
 52         return;
 53     }
 54
 55     /*为确保正确性在进行转换之前可以进一步进行IP格式校验*/
 56
 57     char *ip_sub = NULL;
 58     int i,index =0;
 59
 60     ip_sub = strtok(ip,SPLIT_SIGN); //slit ip by .
 61
 62     itobinary(atoi(ip_sub),binary_str,index);
 63
 64     for(i = 0; i < 3; i++)
 65     {//need to ip legal detect to pretend error
 66         ip_sub = strtok(NULL,SPLIT_SIGN);
 67
 68         index += 8;
 69         itobinary(atoi(ip_sub),binary_str,index);
 70     }
 71
 72 }
 73
 74 /*
 75  *name: create_iptrie_node
 76  *
 77  *return:
 78  * new ip trie node
 79  */
 80 ip_trie_node *create_iptrie_node()
 81 {
 82     ip_trie_node *node = (ip_trie_node *)calloc(1,sizeof(ip_trie_node));
 83
 84     if(node == NULL)
 85     {
 86         perror("create ip trie node error -- calloc");
 87     }
 88     else
 89     {
 90         node->child[0] = NULL;
 91         node->child[1] = NULL;
 92     }
 93
 94     return node;
 95 }
 96
 97 /*
 98  *name: insert_iptrie_node
 99  *
100  *param:
101  * root: trie root; ip: orignal ip string
102  *
103  *return:
104  * void
105  *
106  *notice:
107  * this function call strtok it will change input ip
108  * so if input ip need to use at other position
109  * you shold input a copy of ip
110  */
111 void insert_iptrie_node(ip_trie_node *root,char ip[])
112 {
113     if(root == NULL)
114     {
115         printf("trie have not init\n");
116
117         return;
118     }
119
120     if(ip == NULL)
121     {
122         return;
123     }
124
125     char binary_str[IP_BINARY_LEN + 1];
126     int i,child_index;
127
128     memset(binary_str,0,IP_BINARY_LEN + 1);
129
130     convert_ip_binary(ip,binary_str); //to binary string
131
132     for(i = 0; i < IP_BINARY_LEN; i++)
133     {
134         child_index = binary_str[i] - '0'; //child is 0 or 1
135         if(root->child[child_index] == NULL)
136         {
137             root->child[child_index] = create_iptrie_node();
138         }
139
140         root = root->child[child_index];
141     }
142 }
143
144 /*
145  *name: select_iptrie_node
146  *
147  *param:
148  * root: trie root; ip: orignal ip string
149  *
150  *return:
151  * 0 :not find; 1:find
152  *
153  *notice:
154  * this function call strtok it will change input ip
155  * so if input ip need to use at other position
156  * you shold input a copy of ip
157  */
158 int select_iptrie_node(ip_trie_node *root,char ip[])
159 {
160     if(root == NULL)
161     {
162         printf("trie have not init\n");
163         return 0;
164     }
165
166     if(ip == NULL)
167     {
168         return 0;
169     }
170
171     int i;
172     char binary_str[IP_BINARY_LEN + 1];
173
174     memset(binary_str,0,IP_BINARY_LEN + 1);
175
176     convert_ip_binary(ip,binary_str); //to binary string
177
178     int child_index;
179     for(i = 0; i < IP_BINARY_LEN; i++)
180     {
181         child_index = binary_str[i] - '0';
182
183         if(root->child[child_index] == NULL)
184         {
185             return 0;
186         }
187
188         root = root->child[child_index];
189     }
190
191     return 1;
192 }

3.main.c如下(测试程序)

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3
 4 #include "iptrie.h"
 5
 6
 7 int main()
 8 {
 9     char sip[16];
10     char dip[16];
11     int i = 0;
12     int isfind = 0;
13     ip_trie_node *root = create_iptrie_node();
14
15     while(1)
16     {
17         printf("insert a ip:\n");
18         scanf("%s",sip);
19         insert_iptrie_node(root,sip);
20
21         printf("query a ip:\n");
22         scanf("%s",dip);
23         isfind = select_iptrie_node(root,dip);
24         if(isfind == 1)
25         {
26             printf("find\n");
27         }
28         else
29         {
30             printf("not find\n");
31         }
32     }
33 }

4.Makefile (linux下编译)

CC = gcc
CFLAG = -gINC = -I./target:IptrieIptrie:iptrie.o main.c$(CC) $(CFLAG) $(INC) -o $@  $^iptrie.o:iptrie.c$(CC) -c $<clean:rm *.o Iptrie

转载于:https://www.cnblogs.com/daimadebanyungong/p/5130622.html

IP trie树接口相关推荐

  1. trie树java_【数据结构】Trie树的应用:查询IP地址的ISP(Java实现)

    查询IP地址的ISP 给定一个IP地址,如何查询其所属的ISP,如:中国移动(ChinaMobile),中国电信(ChinaTelecom),中国铁通(ChinaTietong)? 现在网上有ISP的 ...

  2. mysql索引用trie树_数据结构与算法之美【完整版】

    资源目录: ├─01-开篇词 (1讲) │ ├─00丨开篇词丨从今天起,跨过"数据结构与算法"这道坎.html │ ├─00丨开篇词丨从今天起,跨过"数据结构与算法&qu ...

  3. 数据结构之trie树——First! G,电子字典,Type Printer,Nikitosh and xor

    文章目录 [USACO12DEC]First! G [JSOI2009]电子字典 [IOI2008] Type Printer Nikitosh and xor [USACO12DEC]First! ...

  4. trie树 mysql_Trie树详解(转)

    特别声明 本文只是一篇笔记类的文章,所以不存在什么抄袭之类的. 以下为我研究时参考过的链接(有很多,这里我只列出我记得的): 1.字典树的概念 字典树,因为它的搜索快捷的特性被单词搜索系统使用,故又称 ...

  5. 实现Trie树(C++)

    文章目录 前言 一.Trie树原理 二.Trie树实现(C++) 1.接口 2.实现 总结 前言 Trie树,又叫前缀树,字典树,单词查找树,是由二叉树衍生出来的一种树形高级数据结构.经常用于处理字符 ...

  6. 【图解算法】Trie树

    欢迎来到我的算法专栏,今天我们来讲一种常见的数据结构:Trie树. 我们会对Trie树的性质,相关操作,应用进行讲解,并模拟实现一个简版的Trie树. 目录 1. Trie树 简介 2. Trie树的 ...

  7. usaco Cowxor (trie 树)

    没想到trie树还可以用在这上面,厉害厉害. [分析]这是字母树的经典应用.首先因为是求xor的最大值,可以用前缀和计算xor值,然后n^2枚举即可. [cpp] view plaincopy for ...

  8. 字符串匹配算法 -- AC自动机 基于Trie树的高效的敏感词过滤算法

    文章目录 1. 算法背景 2. AC自动机实现原理 2.1 构建失败指针 2.2 依赖失败指针过滤敏感词 3. 复杂度及完整代码 1. 算法背景 之前介绍过单模式串匹配的高效算法:BM和KMP 以及 ...

  9. 字符串匹配数据结构 --Trie树 高效实现搜索词提示 / IDE自动补全

    文章目录 1. 算法背景 2. Trie 树实现原理 2.1 Trie 树的构建 2.2 Trie树的查找 2.3 Trie树的遍历 2.4 Trie树的时间/空间复杂度 2.5 Trie 树 Vs ...

最新文章

  1. Android XML pull 解析器
  2. Mxnet Focal Loss实现
  3. CSS3---8.盒模型
  4. 部署 JSP 工程文件
  5. Python函数式编程指南
  6. data的值 如何初始化vue_vue 创建一个基础实例【02】
  7. java native堆_Java Native Memory比堆更快吗?
  8. linux各种压缩包使用方法
  9. 项目经济规模的估算方法_估算英国退欧的经济影响
  10. C++工作笔记-对二级指针的进一步理解(函数的参数使用二级指针,从而操作原数据)
  11. linux 移出权限,如何在 Ubuntu 上为用户授予和移除 sudo 权限
  12. 中心药库管理系统 杀毒软件
  13. wordpress mysql缓存_【新功能】wordpress数据库缓存功能介绍和教程
  14. SLAM前端之ndt_omp使用
  15. mac 苹果电脑恢复 Recovery HD;进不去恢复模式;
  16. maven本地仓库中已有jar包,项目却读取不了
  17. 梦想在三十岁起航!__来自黑马程序员69期安卓班的学员
  18. HTML——超文本标记语言
  19. Linux记录-sysctl.conf优化方案
  20. 项目实训2021.07.09

热门文章

  1. 《Java核心技术 卷Ⅱ 高级特性(原书第10版)》一3.6.2 使用StAX解析器
  2. Meerkat倒下想到的:社交巨头的后院不容置喙
  3. 5G:关键技术实现可引领
  4. jsonp的使用方法
  5. Linux服务器架设笔记-Squid服务器配置
  6. java的for循环取出数据只是拿到最后一个_如何保证缓存与数据库双写的一致性...
  7. 学习云计算学哪种编程语言_您应该学习哪种编程语言?
  8. django girls_Django Girls Budapest团队的活动筹划技巧
  9. 运行Java web时遇到的错误
  10. 某位程序猿柬埔寨开发offer到手,薪资翻倍,去吗?网友:面向阎王编程...