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

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

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

 #ifndef _IP_TRIE_H_
#define _IP_TIRE_H_ #define SPLIT_SIGN "."
#define IP_BINARY_LEN 32 typedef struct ip_trie_node
{
struct ip_trie_node *child[]; //two child node
}ip_trie_node; ip_trie_node *create_iptrie_node(); void insert_iptrie_node(ip_trie_node *root,char ip[]); int select_iptrie_node(ip_trie_node *root,char ip[]); #endif

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

 #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include "iptrie.h" /*
*name: itobinary
*
*param:
* num: orignal number; binary_str: dest string; index: the binary str copy index
*
*return:
* void
*/
void itobinary(int num,char binary_str[],int index)
{
if(binary_str == NULL)
{
return;
} int i,bit = 0x01;
for(i = ; i < ; i++)
{//conver integer to 8 bit binary str
if((num & bit) != )
{//oprater & is lower than !=
binary_str[index + - i] = '';
}
else
{
binary_str[index + - i] = '';
} bit <<= ; //bit * 2
}
} /*
*name: convert_ip_binary
*
*param:
* ip:orign ip string; binary_str:dest binary string
*
*return:
* void
*/
void convert_ip_binary(char ip[],char binary_str[])
{
if(ip == NULL || binary_str == NULL)
{
return;
} /*为确保正确性在进行转换之前可以进一步进行IP格式校验*/ char *ip_sub = NULL;
int i,index =; ip_sub = strtok(ip,SPLIT_SIGN); //slit ip by . itobinary(atoi(ip_sub),binary_str,index); for(i = ; i < ; i++)
{//need to ip legal detect to pretend error
ip_sub = strtok(NULL,SPLIT_SIGN); index += ;
itobinary(atoi(ip_sub),binary_str,index);
} } /*
*name: create_iptrie_node
*
*return:
* new ip trie node
*/
ip_trie_node *create_iptrie_node()
{
ip_trie_node *node = (ip_trie_node *)calloc(,sizeof(ip_trie_node)); if(node == NULL)
{
perror("create ip trie node error -- calloc");
}
else
{
node->child[] = NULL;
node->child[] = NULL;
} return node;
} /*
*name: insert_iptrie_node
*
*param:
* root: trie root; ip: orignal ip string
*
*return:
* void
*
*notice:
* this function call strtok it will change input ip
* so if input ip need to use at other position
* you shold input a copy of ip
*/
void insert_iptrie_node(ip_trie_node *root,char ip[])
{
if(root == NULL)
{
printf("trie have not init\n"); return;
} if(ip == NULL)
{
return;
} char binary_str[IP_BINARY_LEN + ];
int i,child_index; memset(binary_str,,IP_BINARY_LEN + ); convert_ip_binary(ip,binary_str); //to binary string for(i = ; i < IP_BINARY_LEN; i++)
{
child_index = binary_str[i] - ''; //child is 0 or 1
if(root->child[child_index] == NULL)
{
root->child[child_index] = create_iptrie_node();
} root = root->child[child_index];
}
} /*
*name: select_iptrie_node
*
*param:
* root: trie root; ip: orignal ip string
*
*return:
* 0 :not find; 1:find
*
*notice:
* this function call strtok it will change input ip
* so if input ip need to use at other position
* you shold input a copy of ip
*/
int select_iptrie_node(ip_trie_node *root,char ip[])
{
if(root == NULL)
{
printf("trie have not init\n");
return ;
} if(ip == NULL)
{
return ;
} int i;
char binary_str[IP_BINARY_LEN + ]; memset(binary_str,,IP_BINARY_LEN + ); convert_ip_binary(ip,binary_str); //to binary string int child_index;
for(i = ; i < IP_BINARY_LEN; i++)
{
child_index = binary_str[i] - ''; if(root->child[child_index] == NULL)
{
return ;
} root = root->child[child_index];
} return ;
}

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

 #include <stdio.h>
#include <stdlib.h> #include "iptrie.h" int main()
{
char sip[];
char dip[];
int i = ;
int isfind = ;
ip_trie_node *root = create_iptrie_node(); while()
{
printf("insert a ip:\n");
scanf("%s",sip);
insert_iptrie_node(root,sip); printf("query a ip:\n");
scanf("%s",dip);
isfind = select_iptrie_node(root,dip);
if(isfind == )
{
printf("find\n");
}
else
{
printf("not find\n");
}
}
}

4.Makefile (linux下编译)

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

IP trie树接口的更多相关文章

  1. Trie树的应用:查询IP地址的ISP

    1. 问题描述 给定一个IP地址,如何查询其所属的ISP,如:中国移动(ChinaMobile),中国电信(ChinaTelecom),中国铁通(ChinaTietong)?现有ISP的IP地址区段可 ...

  2. 【BZOJ-4523】路由表 Trie树 + 乱搞

    4523: [Cqoi2016]路由表 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 155  Solved: 98[Submit][Status][ ...

  3. Trie树详解(转)

    特别声明 本文只是一篇笔记类的文章,所以不存在什么抄袭之类的. 以下为我研究时参考过的链接(有很多,这里我只列出我记得的): Trie(字典树)的应用——查找联系人 trie树 Trie树:应用于统计 ...

  4. 【Luogu3732】[HAOI2017]供给侧改革(Trie树)

    [Luogu3732][HAOI2017]供给侧改革(Trie树) 题面 洛谷 给定一个纯随机的\(01\)串,每次询问\([L,R]\)之间所有后缀两两之间的\(LCP\)的最大值. 题解 一个暴力 ...

  5. [BinaryTree] AVL树、红黑树、B/B+树和Trie树的比较

    转自:AVL树.红黑树.B/B+树和Trie树的比较 AVL树 最早的平衡二叉树之一.AVL是一种高度平衡的二叉树,所以通常的结果是,维护这种高度平衡所付出的代价比从中获得的效率收益还大,故而实际的应 ...

  6. 【BZOJ4523】[Cqoi2016]路由表 Trie树模拟

    [BZOJ4523][Cqoi2016]路由表 Description 路由表查找是路由器在转发IP报文时的重要环节.通常路由表中的表项由目的地址.掩码.下一跳(Next Hop)地址和其他辅助信息组 ...

  7. HDU 4760 Good Firewall ( Trie树 )

    一开始看的时候就想歪了,比赛的时候一直在YY线段树区间覆盖,然后纠结节点数太多开不下怎么办啊啊啊啊…… 然后昨天吃饭的时候也在纠结这到底是个啥题,后来发现公共前缀->前缀??!!!!->这 ...

  8. AVL树,红黑树,B-B+树,Trie树原理和应用

    前言:本文章来源于我在知乎上回答的一个问题 AVL树,红黑树,B树,B+树,Trie树都分别应用在哪些现实场景中? 看完后您可能会了解到这些数据结构大致的原理及为什么用在这些场景,文章并不涉及具体操作 ...

  9. 基于trie树做一个ac自动机

    基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...

随机推荐

  1. Prometheus监控实战day1-监控简介

    福利 Prometheus监控实战PDF电子书下载 链接:https://pan.baidu.com/s/1QH4Kvha5g70OhYQdp4YsfQ 提取码:oou5 若你喜欢该资料,请购买该资料 ...

  2. oracle 通用事务使用

    private void dothing() { OracleConnection con = DBHelperOracle.init(); OracleTransaction tran = con. ...

  3. MySQL8 全部数据类型

    数字类型 日期类型 字符串类型 CHAR和VARCHAR 表列数和行大小限制 MySQL每个表的硬限制为4096列,但对于给定的表,有效最大值可能更小.确切的列限制取决于几个因素: 表的最大行大小限制 ...

  4. [转帖]70亿!以色列间谍产品公司NSO要被卖掉了

    70亿!以色列间谍产品公司NSO要被卖掉了 2017-06-14 11:11 https://www.sohu.com/a/148739327_257305 E安全6月14日讯以色列的网络能力处于世界 ...

  5. [Docker] Windows 宿主环境下,共享或上传文件到容器的方法

    需求如题. 解决方案1 - 挂载目录(适用于创建新的容器) 格式-v 容器目录 或 -v 本地目录:容器目录 范例Linux宿主环境下:使用镜像 nginx:latest,以后台模式启动一个容器,将容 ...

  6. Python爬虫:现学现用xpath爬取豆瓣音乐

    爬虫的抓取方式有好几种,正则表达式,Lxml(xpath)与BeautifulSoup,我在网上查了一下资料,了解到三者之间的使用难度与性能 三种爬虫方式的对比. 这样一比较我我选择了Lxml(xpa ...

  7. 嵌入式web服务器BOA+CGI+HTML+MySQL项目实战——Linux

    准备环境操作系统: Ubuntu12.04 LTS环境搭建: 需要 BOA,Apache,CCGI,MySQL,GCC[Linux下嵌入式Web服务器BOA和CGI编程开发][数据库的相关知识——学习 ...

  8. Android apk逆向:反编译,回编译,签名,打包。

    Android apk逆向:反编译,回编译,签名,打包流程. 第一步: apk 反编译. 1) 打开命令行窗口,输入java -version, 检测当前java版本,若版本较低, 则下载JAVA S ...

  9. sublime配置python环境及快捷键

    sublime配置python环境 参考链接:https://blog.csdn.net/VertigozZ/article/details/54574006 快捷键的配置:https://www.c ...

  10. SpringBoot获得application.properties中数据的几种方式

    转:https://blog.csdn.net/qq_27298687/article/details/79033102 SpringBoot获得application.properties中数据的几 ...