自己实现了一个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. An unexpected exception occurred while binding a dynamic operation 错误的一种情况

    这种错误,出现在dynamic传值的时候,无法动态访问变量. 出错原因是: 使用了嵌套类,class里面又定义了class

  2. linux内存管理swap分区

    一.什么是linux的内存机制? 我们知道,直接从物理内存读写数据要比从硬盘读写数据要快的多,因此,我们希望所有数据的读取和写入都在内存完成,而内存是有限的,这样就引出了物理内存与虚拟内存的概念. 物 ...

  3. 【转】深入理解javascript中的立即执行函数(function(){…})()

    javascript和其他编程语言相比比较随意,所以javascript代码中充满各种奇葩的写法,有时雾里看花,当然,能理解各型各色的写法也是对javascript语言特性更进一步的深入理解. ( f ...

  4. 【javascript】日期转字符串

    function dateFormat(fmt, date) { var ret; var tf = function(str, len){ if(str.length < len) { for ...

  5. 【bat】【windows】通过端口杀死进程

    简单的bat处理,把指定端口集合杀死相应的进程,就是杀死对应的pid @echo off & setlocal EnableDelayedExpansion ]= ]= ]= ]= ]= fo ...

  6. org.apache.hadoop.conf.Configuration无法引用 解决方法

    我用的是Hadoop-common 2.6.4jar,可是明明包里面有这个类却引用不了,然后我看了下包里面是一个抽象类......................................... ...

  7. 分页Model

    <?php namespace Admin\Model; use Think\Model; class PageModel extends Model{ /* *此函数分页函数.传入表名和当前页 ...

  8. [转]matlab GUI 新手入门——最基本的几个概念

    本文摘自iLoveMatlab论坛 1.从最简单的开始 编程的基本思想是:我执行一个操作,程序做出一个反应."一个操作"包括点击鼠标.拖动滑块.填写数据.选择选项等:"做 ...

  9. LuoguP2698 【[USACO12MAR]花盆Flowerpot】

    题目描述 首先我们简化一下题意: 要找一段区间[L,R],使区间[L,R]内元素最大值减最小值大于等于D. 做法: 首先很容易想到采用二分,分什么呢? 我们二分区间长度为mid 这个时候,检验就成为了 ...

  10. Golang 传递任意类型的切片

    肯定有这样的一种场景,写一个函数,该函数可以接收任意类型的切片,完成相应的功能. 就好比这种情况 intSlice := []int{1,2,3,4,5,6,7,8} strSlice := []st ...