简体   繁体   中英

Where does the difference in size come from?

I created a trie of sorts to store all the words (not definitions) in the English dictionary. The point of it was so that I can get all the words that only contain letters within a given range.

The text file containing all the words is about 2.7 mb, but after creating the tree and writing it to a file using pickle, the file is >33 mb.

Where does this difference in size come from? I thought I would be saving space by not needing to store multiple copies of the same letter for different word, eg for the words app and apple I would only need 5 nodes, for a -> p -> p -> l -> e.

My code is as follows:

import pickle

class WordTrieNode:
    def __init__(self, nodeLetter='', parentNode=None, isWordEnding=False):
        self.nodeLetter = nodeLetter
        self.parentNode = parentNode
        self.isWordEnding = isWordEnding
        self.children = [None]*26 # One entry for each lowercase letter of the alphabet

    def getWord(self):
        if(self.parentNode is None):
            return ''

        return self.parentNode.getWord() + self.nodeLetter

    def isEndOfWord(self):
        return self.isWordEnding

    def markEndOfWord():
        self.isWordEnding = True

    def insertWord(self, word):
        if(len(word) == 0):
            return

        char = word[0]
        idx = ord(char) - ord('a')
        if(len(word) == 1):
            if(self.children[idx] is None):
                node = WordTrieNode(char, self, True)
                self.children[idx] = node
            else:
                self.children[idx].markEndOfWord()
        else:
            if(self.children[idx] is None):
                node = WordTrieNode(char, self, False)
                self.children[idx] = node
                self.children[idx].insertWord(word[1:])
            else:
                self.children[idx].insertWord(word[1:])

    def getAllWords(self):
        for node in self.children:
            if node is not None:
                if node.isEndOfWord():
                    print(node.getWord())
                node.getAllWords()

    def getAllWordsInRange(self, low='a', high='z'):
        i = ord(low) - ord('a')
        j = ord(high) - ord('a')
        for node in self.children[i:j+1]:
            if node is not None:
                if node.isEndOfWord():
                    print(node.getWord())
                node.getAllWordsInRange(low, high)



def main():

    tree = WordTrieNode("", None, False)

    with open('en.txt') as file:
        for line in file:
            tree.insertWord(line.strip('\n'))
    with open("treeout", 'wb') as output:
        pickle.dump(tree, output, pickle.HIGHEST_PROTOCOL)

    #tree.getAllWordsInRange('a', 'l')
    #tree.getAllWords()
if __name__ == "__main__":
    main()

Nodes of a trie are huge as they store a link for all possible next letters. As you can see in the code, every node holds a list of 26 links (children).

More compact schemes are possible ( https://en.wikipedia.org/wiki/Trie#Compressing_tries ), at the expense of more complexity and slower speed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM