字典树Trie实战

本文介绍了在《软件工程》课程中使用字典树Trie解决字符串查找问题的实践。通过字典树实现快速查找,强调了算法在特定场景下的效率优势,分享了初步的Python 3实现,但表示会后续完善。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问起起源于在学堂在线上的《软件工程》这门课,一般每章后面都会有一个实践作业,发现其难度可以比拟大一大二的课程大作业,早有这样的训练强度该有多好呢?呵呵O(∩_∩)O~

A. 问题描述

功能需求
以上是基础的要求,其实字符串的题目一般都可以随便暴力做,所以评价一个算法的优劣,主要是看它在特定应用场景下的效率,比如:
效率要求
基于上面的要求,我决定使用“字典树”Trie来实现基于字符串的快速查找,字典树的描述请自行搜索,这里暂时只分享我自己的python 3实现。

B. 实现

初次实现,有点乱,先mark一下,过些日子回来完善【未完待续】

# !/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'jacket'


import sys
import re


SIZE = 27

def char2int(ch):
    # return ord(ch) - ord('a') + 1
    # for quick calculation, ord('a') = 97, so the above equal:
    return ord(ch) - 96


class TrieNode:
    def __init__(self, data=None):
        self.data = data
        self.cnt = 0
        self.leafNode = None
        self.children = []

    def haveChildren(self):
        return len(self.children) == SIZE

    def extend(self):
        self.children = [TrieNode() for _ in range(SIZE)]

    def addToLeaf(self, word):
        if not self.leafNode:
            self.leafNode = TrieNode(word)
        self.leafNode.cnt += 1
        return self.leafNode.cnt - 1

    def addToChild(self, ch, index):
        if not self.children[index].data:
            self.children[index].data = ch
        self.children[index].cnt += 1

        if not self.leafNode:
            self.leafNode = TrieNode()
        self.leafNode.cnt += 1


class Trie:
    def __init__(self):
        self.root = TrieNode()

    def insert(self, word):
        now = self.root

        for ch in word:
            index = char2int(ch)
            if not now.haveChildren():
                now.extend()
            now.addToChild(ch, index)
            now = now.children[index]

        return now.addToLeaf(word)


def main(args):
    t = Trie()
    with open(args[0], 'r') as f:
        for line in f:
            if line[-1] == '\n':
                line = line[0:-1]
            if len(line) > 0
                t.insert(line.lower())

    print('Trie build')
    while True:
        q = input()
        if q == '0':
            break
        print(t.insert(q.lower()))

    return 0



if __name__ == '__main__':
    exit(main(sys.argv[1:]))

C. More

Trie
图片来自wiki百科

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值