Using a tree to find stuff

Posted on 2008-02-08
Medium Priority
Last Modified: 2010-03-30
Im having trouble figuring out how to use a tree to find stuff, or even if my thought is possible.
Im trying to create a search tree - category, with connection to products.
Something like:
class Category {
    private Integer id;
   private String name;
   private List<Category> children;
Now, from this code I should be able to make a visual tree, something like:
  |-id1, Cat1
  |-id2, Cat2
      |-id3, Cat2_1
      |-id4, Cat2_2
          |-id6, Cat2_2_1
  |-id5, Cat3
Then I have my product:
class Product {
  private Integer id;
  private String name;
  private Category category;
  public Product(String name, Category cat) { &.}
say I have:
List<Product> productList;
productList.add(new Product(prod0, Cat2));
productList.add(new Product(prod1,  Cat2_1));
productList.add(new Product(prod2, Cat2_2));
productList.add(new Product(prod3, Cat2_2_1));
productList.add(new Product("prod4", Cat3));

I need to figure out a way to return a List<Product> when Cat2 is selected that contains:
Cat2, Cat2_1, Cat2_2, Cat2_2_1.
What is the easiest way to do this?
Question by:kristian_gr
LVL 24

Accepted Solution

sciuriware earned 1500 total points
ID: 20852008
I guess a map is simpler and quicker.
Think of a HashMap of categories, each being a HashMap of key-child pairs.


Author Comment

ID: 20856916
Do you have any exaples?

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month6 days, 14 hours left to enroll

594 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question