Tr ie Реализация в Python - Печать ключей - PullRequest
0 голосов
/ 06 марта 2020

Я реализовал структуру данных Tr ie, используя python, теперь проблема в том, что он не отображает ключи, которые Tr ie хранятся в его структуре данных.

class Node:
    def __init__(self):
        self.children = [None] * 26
        self.endOfTheWord = False

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

    def getNode(self):
        return Node()

    def charToIndex(self ,ch):
        return ord(ch) - ord('a')             

    def insert(self ,word):
        current = self.root
        for i in range(len(word)):
            index = self.charToIndex(word[i])

            if current.children[index] is None:
                current.children[index] = self.getNode()

            current = current.children[index]

        current.endOfTheWord = True

    def printKeys(self):
        str = []
        self.printKeysUtil(self.root ,str)

    def printKeysUtil(self ,root ,str):

        if root.endOfTheWord == True:
            print(''.join(str))
            return

        for i in range(26):

            if root.children[i] is not None:
                ch = chr(97) + chr(i)
                str.append(ch)
                self.printKeysUtil(root.children[i] ,str)
                str.pop()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...