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 6580333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:00:51+00:00 2026-05-25T16:00:51+00:00

I need to construct a tree in Java. I am already done with tree

  • 0

I need to construct a tree in Java. I am already done with tree as a data structure. But I am having some problem feeding data from an array to the tree. Here is what I need to do.

domain = {"b", "c"};

then, the tree should be like:

null -> b,c
b->c  c->b

So basically I want a node’s child to have all children from the domain that are not already covered in the parent. The problem is that in spite of a lot of tries, I am not able to write code for doing it. I know it can be done with a reccursive function.
I do not want full code. Any hint towards the solution will be highly appreciated. Thank you.

enter image description here

P.S.

I’d clear the specification. “”Every node in the tree has all the values as children from the domain apart from the ones that are already covered in it or its parents””

as in the figure, a is the base (say null). It has all the values from the domain (b,c). b has c and c has b.

  • 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-25T16:00:52+00:00Added an answer on May 25, 2026 at 4:00 pm

    The specification says:

    Every node in the tree has all the values as children from the domain apart from the ones that are already covered in it or its parents

    It is a little unclear, but I am assuming that covered in it or its parents means that a node with value x is allowed if the value x is not on the path from the node to the root. In that case the tree can be constructed like this (the language is Haskell):

    import List
    
    data Tree = Tree String [Tree]
    
    build x xs = Tree x children
        where
        children = map (\x -> build x (delete x xs)) xs
    

    For example, given a root value "a" and a list of domain values ["b", "c", "d"], the program constructs a root node with value "a" and 3 children recursively constructed from:

    • the root value "b" and the domain ["c", "d"],
    • the root value "c" and the domain ["b", "d"],
    • and the root value "d" and the domain ["b", "c"].

    In pseudo-Python this is the algorithm of the Haskell program:

    def build(root_value, domain):
        node = Tree(root_value)
    
        # For each value in the domain:
        for i in range(len(domain)):
            child_root = domain[i]
    
            # The child domain is equal to the original domain
            # with value at position 'i' removed.
            child_domain = domain[: i] + domain[i + 1:]
    
            # Recursively build the child
            child = build(child_root, child_domain)
    
            # - and add the child to the node.
            node.add_child(child)
    
        return node
    

    Here is a test of the build function that prints the tree of the example of the question and the example above:

    pretty level (Tree x children) = do
        mapM_ putStr [indent, x, "\n"]
        mapM_ (pretty (level + 3)) children
        where
        indent = replicate level ' '
    
    main = do
        putStrLn "Tree for a -> b, c:"
        pretty 0 (build "a" ["b", "c"])
        putStrLn "\nTree for a -> b, c, d:"
        pretty 0 (build "a" ["b", "c", "d"])
    

    The test uses indentation to show the depth of each node in the tree:

    Tree for a -> b, c:
    a
       b
          c
       c
          b
    
    Tree for a -> b, c, d:
    a
       b
          c
             d
          d
             c
       c
          b
             d
          d
             b
       d
          b
             c
          c
             b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to construct some rather simple SQL, I suppose, but as it's a
I need to construct an if statement from the data coming from the client
I am trying to construct an expression tree to operate against a string array.
I need to construct a huge treeview from a composite database table with Grouping
I need to put an LDAP contextSource into my Java EE container's JNDI tree
I need to construct the URL of a page in a String, to send
I need to construct a long string with javascript. Thats how i tried to
Currently i need to construct and update large xml files. So what is the
I am using the Zend framework and what I need is to construct a
How can I construct an object using a query if I also need to

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.