Neterukun's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Neterukun1993/Library

:heavy_check_mark: Trie 木
(String/Trie.py)

使い方

Trie()
空の Trie 木を初期構築する。計算量 $O(1)$

Verified with

Code

class TrieNode:
    def __init__(self):
        self.child = {}
        self.valid = False

    def set_child(self, s):
        self.child[s] = TrieNode()

    def get_child(self, s):
        if s not in self.child:
            return None
        return self.child[s]


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

    def search(self, string):
        ptr = self.root
        for s in string:
            if ptr.get_child(s) is None:
                return False
            ptr = ptr.get_child(s)
        return ptr.valid

    def insert(self, string):
        ptr = self.root
        for s in string:
            if ptr.get_child(s) is None:
                ptr.set_child(s)
            ptr = ptr.get_child(s)
        if ptr.valid:
            return False
        ptr.valid = True
        return True

    def delete(self, string):
        ptr = self.root
        for s in string:
            if ptr.get_child(s) is None:
                return False
            ptr = ptr.get_child(s)
        ptr.valid = False
        return True
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/site-packages/onlinejudge_verify/documentation/build.py", line 71, in _render_source_code_stat
    bundled_code = language.bundle(stat.path, basedir=basedir, options={'include_paths': [basedir]}).decode()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/site-packages/onlinejudge_verify/languages/python.py", line 96, in bundle
    raise NotImplementedError
NotImplementedError
Back to top page