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

  • Home
  • SEARCH
  • 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 8743357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:39:41+00:00 2026-06-13T11:39:41+00:00

I have a fully built generic Trie in java. I am trying to traverse

  • 0

I have a fully built generic Trie in java. I am trying to traverse through the Trie to obtain all the complete combinations for each path. For example, if the Trie contained chars then it would return all the word combinations. For my purposes, I am trying to put all the nodes for each combination into an array and return them. I am stumped however. I only came up with the traversal that goes through each child (+ subchildren) before going back to the parent/starting node (much like a BST traversal). I am using an ArrayList to hold the children for each node. Sorry if it is a bit confusing. A code sample or pseudo code will be much appreciated. Thanks.

EDIT

By combinations, I mean the following. If I had a Trie<char> that looked like the following:

        "null"
       /  |   \
      a   i    t
     /   /|\    \
    t   f m n    o

The combinations that I would want returned would be:

[a, t]
[i, f]
[i, m]
[i, n]
[t, o]

and all these arrays/lists could be in one single ArrayList which is returned at the end.

  • 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-06-13T11:39:42+00:00Added an answer on June 13, 2026 at 11:39 am

    Do a recursive method to (at least) get all the chars in the tree. Just make sure you initialize the chars as an empty List

    Stack startRead(Tree tree) {
      // validation check
      if (tree == null || !tree.hasChild()) return null;
    
      // create Stack to store the lists
      Stack listStack = new Stack();
    
      // for every child
      List children = tree.getChildren();
      for (Tree child : children) {
        // create a list
        List childList = new ArrayList();
    
        // store (push) it into stack
        listStack.push(childList);
    
        // call the recursive
        readIt(child, listStack);
      }
    
      return listStack;
    }
    
    void readIt(Tree tree, Stack listStack) {
      // pick the top list from stack
      List current = (List) listStack.pop();
    
      // this is the base; if tree has no child don't call this method again.
      if (!tree.hasChild()) {
        // if it's leaf add the value to current list
        current.add(tree.getValue());
    
        // push it back to stack
        listStack.push(current);
      } else {
        // for every child
        List children = tree.getChildren();
        for (Tree child : children) {
          // IMPORTANT! clone the list (if this fails, clone it yourself)
          // clone is called when the tree is branching
          List childList = current.clone();
    
          // insert this tree value to list
          childList.add(tree.getValue());
    
          // push it back
          listStack.push(childList);
    
          // call again
          readIt(child, listStack);
        }
      }
    }
    

    with this you will have a return value of a Stack consisting Lists of value for each combination.

    Hope this helps. 🙂

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

Sidebar

Related Questions

I work for local notification and all I have built fully functional And follows
I have a fully functional redact program written in haskell.it replaces all the words
We have built an internal tool that generates the whole data access, each table
I am trying to have a class that allows implicit casting to certain built
I have a Full and Lite applications that were built from the same code.
I have an NSArray full of NSDictionary object. This structure is built from a
In Vaadin's website it says that only Eclipse and NetBeans have fully functional (which
I have a fully working Setup project within Visual Studio 2008 that takes inputs
Bear with me if this is unclear; I have trouble fully wrapping my head
Sometimes I want to have temporary comments fully left justified on a line (//)

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.