【Golang】实现url匹配字典树

首先定义字典树上的结点:

type node struct {
	pattern string
	part string
	children []*node
	isWild bool
}

pattern定义了最终匹配的结点串
part定义了url中的分段
children是孩子结点
isWild用于记录匹配的通配符’:‘和’*’

定义字符串输出函数:

func (n *node) String() string {
	return fmt.Sprintf("node{pattern=%s, part=%s, isWild=%t}", n.pattern, n.part, n.isWild)
}

定义层匹配函数,返回所有孩子结点:

func (n *node) matchChildren(part string) []*node{
	nodes := make([]*node, 0)
	for _, child := range n.children {
		if child.part == part || child.isWild {
			nodes = append(nodes, child)
		}
	}
	return nodes
}

返回下一层结点中匹配part的孩子部分

定义层匹配函数,返回第一个匹配的孩子结点:

func (n *node) matchChild(part string) *node {
	for _, child := range n.children {
		if child.part == part || child.isWild {
			return child
		}
	}
	return nil
}

主要在插入的时候使用

遍历函数:

func (n *node) travel(list *([]*node)) {
	if n.pattern != "" {
		*list = append(*list, n)
	}
	for _, child := range n.children {
		child.travel(list)
	}
}

获取包含完整url的结点,存储在外部传入的list中

查找函数:

func (n *node) search(parts []string, height int) {
	if len(parts) == height || string.HasPrefix(n.part, "*") {
		if n.pattern "" {
			return nil
		}
		return n
	}
	part := parts[height]
	children := n.matchChildren(part)
	for _, child := range children {
		result := child.search(parts, height + 1)
		if result != nil {
			return result
		}
	}
	return nil
}

插入函数:

func (n *node) insert(pattern string, parts []string, height int) {
	if len(parts) == height {
		n.pattern = pattern
		return
	}
	part := parts[height]
	child := n.matchChild(part)
	if child == nil {
		child = &node{part: part, isWild: part[0] == ':' || part[0] == '*'}
		n.children = append(n.children, child)
	}
	child.insert(pattern, part, height + 1)
}
发布了365 篇原创文章 · 获赞 14 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/LU_ZHAO/article/details/105353685