我用Python写了一个三元搜索树,我注意到当该树变得很深时,尝试删除它会导致Python无限期挂起。这是产生此行为的代码的剥离版本:
import random
import sys
from collections import deque
class Node():
__slots__ = ("char", "count", "lo", "eq", "hi")
def __init__(self, char):
self.char = char
self.count = 0
self.lo = None
self.eq = None
self.hi = None
class TernarySearchTree():
"""Ternary search tree that stores counts for n-grams
and their subsequences.
"""
def __init__(self, splitchar=None):
self.root = None
self.splitchar = splitchar
def insert(self, string):
self.root = self._insert(string, self.root)
def _insert(self, string, node):
"""Insert string at a given node.
"""
if not string: …Run Code Online (Sandbox Code Playgroud) python memory tree memory-management recursive-datastructures