Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1088969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:08:56+00:00 2026-05-16T23:08:56+00:00

The problem: I have a trie and I want to return the information stored

  • 0

The problem:

I have a trie and I want to return the information stored in it. Some leaves have information (set as value > 0) and some leaves do not. I would like to return only those leaves that have a value.

As in all trie’s number of leaves on each node is variable, and the key to each value is actually made up of the path necessary to reach each leaf.

I am trying to use a generator to traverse the tree postorder, but I cannot get it to work. What am I doing wrong?

My module:

class Node():
    '''Each leaf in the trie is a Node() class'''
    def __init__(self):
        self.children = {}
        self.value = 0

class Trie():
    '''The Trie() holds all nodes and can return a list of their values'''
    def __init__(self):
        self.root = Node()
    def add(self, key, value):
        '''Store a "value" in a position "key"'''
        node = self.root
        for digit in key:
            number = digit
            if number not in node.children:
                node.children[number] = Node()
            node = node.children[number]
        node.value = value
    def __iter__(self):
        return self.postorder(self.root)
    def postorder(self, node):
        if node:
            for child in node.children.values():
                self.postorder(child)
            # Do my printing / job related stuff here
            if node.value > 0:
                yield node.value

Example use:

>>trie = Trie()
>>trie.add('foo', 3)
>>trie.add('foobar', 5)
>>trie.add('fobaz', 23)

>>for key in trie:
>>....print key
>>
3
5
23

I know that the example given is simple and can be solved using any other data structure. However, it is important for this program to use a trie as it is very beneficial for the data access patterns.

Thanks for the help!

Note: I have omitted newlines in the code block to be able to copy-paste with greater ease.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:08:56+00:00Added an answer on May 16, 2026 at 11:08 pm

    Change

    self.postorder(child)
    

    to

    for n in self.postorder(child):
        yield n
    

    seems to make it work.

    P.S. It is very helpful for you to left out the blank lines for ease of cut & paste 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I have a table that prints out vertical but I would like it
Problem: I have a value that is set in $record, for instance 1.69. Then
Problem: I have a string that looks like this: [1=>2,3,4][5=>6,7,8][9=>10,11,12][13=>14,15][16=>17,18] Question: How can you
Problem: I have a value in a database table. This value can either contain
Maybe some of you could have ran into the same problem i did. Imagine
I’ve got a problem and it seems some before me have had similar problems
Problem : I have to create a number of tables for caching some amount
I have a database of movies with attributes. I'd like to return a queried
Problem: Have made a small mail program which works perfectly on my developer pc
I've seen that a few instances of this problem have been raised already. However,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.