简体   繁体   中英

I have a linked trie returning false instead of true. What errors can be fixed in the code?

I have a trie that isn't doing as expected. The code for it is given below:

class Trie:
    def __insert(node, item):
        # This is the recursive insert function.        
        if len(item) == 0:
            return None

        elif node == None:
            newnode = Trie.TrieNode(item[0])
            newnode.follows = Trie.__insert(newnode.follows, item[1:])
            return newnode

            # return Trie.TrieNode(item[0], follows=Trie.__insert(node, item[1:]))

        elif node.item == item[0]:
            node.follows = Trie.__insert(node.follows, item[1:])

        else:
            node.next = Trie.__insert(node.next, item)

    def __contains(node, item):
        # This is the recursive membership test.
        if len(item) == 0:
            return True

        elif node == None:
            return False

        elif node.item == item[0]:
            return Trie.__contains(node.follows, item[1:])

        else:
            return Trie.__contains(node.next, item)

    class TrieNode:
        def __init__(self, item, next=None, follows=None):
            self.item = item
            self.next = next
            self.follows = follows

        def __str__(self):
            return "TrieNode(" + str(self.item) + ")"

    def __init__(self):
        self.start = None

    def insert(self, item):
        sentinel = item + "$"
        self.start = Trie.__insert(self.start, sentinel)

    def __contains__(self, item):
        sentinel = item + "$"
        return Trie.__contains(self.start, sentinel)


def main():
    t = Trie()
    t.insert("cow")
    t.insert("cat")
    # t.insert("rat")
    # t.insert("rabbit")
    # t.insert("dog")

    print("cow" in t)

if __name__ == "__main__":
    main()

Here are the rules for the code:

For the __insert function:

  1. If the key is empty (ie no units are left in the key), return None as the empty node.
  2. If the node is None then a new node is created with the next unit of the key and the rest of the key is inserted and added to the follows link.
  3. If the first unit of the key matches the unit of the current node, then the rest of the key is inserted into the follows link of the node.
  4. Otherwise, the key is inserted into the next link of the node.

For the __contains function:

  1. If the length of the key is 0, then report success by returning True .
  2. If the node we are looking at is None then report failure by returning False .
  3. If the first unit of the key matches the unit in the current node, then check membership of the rest of the key starting with the follows node.
  4. Otherwise, check membership of the key starting with the next node in the trie.

Inserting one item is okay; for example, it gives True when it checks for cat if it only had cat in there.

It's a different story with multiple strings, though. The main function gives False , instead of True , as it should. I'm wondering if this has anything to do with the __insert or __contains functions. Is there any way I can fix it?

Your __insert method should always return a node when item is not empty. Yet, in the case that node is not None, it returns None. But in that case is should return node .

So add return node at the end of this method.

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