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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:13:34+00:00 2026-06-15T18:13:34+00:00

We have many nodes implemented like this: public class Person{ private String name; private

  • 0

We have many nodes implemented like this:

public class Person{
    private String name;
    private int id1;
    private int id2;
    private Node next; // or left/right, depending on what you're using.
}
  1. How to get name using id1 or id2 in average less than O(n)?
  2. How to print all names in order of id2 in average O(n) or faster?

I thought of using a hash table ordered with id1 and a binary search tree organized by id2. As a beginner to data structures, I’m still unsure about this approach.

  1. Is this the simplest solution, in terms of ease of implementation
    and data structures used?
  2. Does using two data structures, both based on the same object, pose any problems? I’m wondering if “duplicating” data like I am here poses any problems for deleting and inserting, but other problems and solutions to the original questions are welcome too.
  • 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-15T18:13:35+00:00Added an answer on June 15, 2026 at 6:13 pm

    I would indeed use a HashMap<Integer, Person> to store persons by ID1, and a TreeMap<Integer, Person> to store persons by ID2, sorted by ID2.

    The first one is O(1) to get the name by ID1. The second one is O(N) for iterating through all the values.

    To answer your questions:

    1. I think this is indeed the simplest solution
    2. Make sure to encapsulate the two maps in an object offering the needed methods and inserting/deleting in both maps at once. Don’t expose the two maps to the outside. If you expose an iterator or the collection of values of the second map to the outside, make sure to wrap it using Collections.unmodifiableCollection() before returning it, to prevent a modification of the set from the outside.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Node Node.java public class WebPage implements BusinessObject { @Id private
I have many nodes in the treeview like nodes it children, children of children....
I have a treeView with many nodes. I want that some nodes change their
I implemented a gallery, and inside it I have many listviews from left to
I have implemented a GEF editor for a graph-like EMF model, with a remove
I have a node with many CCK fields. I want to hide a field
I have a form tha contains a TreeView with many Textboxes in each node.
I have many different NSArray 's stored in .dat files, in the Documents folder
I have many PowerPoint presentations that I need to be able to add to
I have many many files in a folder, and I want to process them

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.