Implement Tree Datastructure in Java

0 votes

Is there a good available data structure to represent a tree in Java?

Specifically, these are the representational parameters

  • At any node may have an arbitrary number of children
  • Each node is a String having a String child only
  • Able to get all the children given an input string representing a given node

May 9, 2018 in Java by Daisy
• 8,140 points
1,430 views

1 answer to this question.

0 votes

I wrote a small "TreeMap" class based on "HashMap" that supports adding paths:

import java.util.HashMap;
import java.util.LinkedList;

public class TreeMap<T> extends LinkedHashMap<T, TreeMap<T>> {
    public void put(T[] path) {
       LinkedList<T> list = new LinkedList<>();
       for (T key : path) {
            list.add(key);
       }
       return put(list);
   }

    public void put(LinkedList<T> path) {
       if (path.isEmpty()) {
           return;
       }
        T key= path.removeFirst();
       TreeMap<T> val = get(key);
       if (val == null) {
            val= new TreeMap<>();
            put(key, val);
       }
        val.put(path);
   }
}

It can be use to store a Tree of things of type "T" (generic), but does not (yet) support storing extra data in it's nodes. If you have a file like this:

root, child 1
root, child 1, child 1a
root, child 1, child 1b
root, child 2
root, child 3, child 3a

Then you can make it a tree by adding this code

TreeMap<String> root = new TreeMap<>();
Scanner sc = new Scanner(new File("input.txt"));
while (sc.hasNextLine()) {
  root.put(sc.nextLine().split(", "));
}

You can easily adapt to this tree

answered May 9, 2018 by sharth
• 3,370 points

Related Questions In Java

0 votes
1 answer

Can anyone show how to implement SortByValue in Tree Map for Java ?

You can't have the TreeMap itself sort on the ...READ MORE

answered Dec 10, 2018 in Java by Sushmita
• 6,920 points

edited Dec 10, 2018 by Sushmita 790 views
0 votes
1 answer

How to implement constants in java

Here is the solution- public static final TYPE ...READ MORE

answered May 28, 2018 in Java by sophia
• 1,400 points
659 views
0 votes
1 answer

What is an efficient way to implement a singleton pattern in Java?

Use an enum: public enum Foo { ...READ MORE

answered Jul 6, 2018 in Java by Akrati
• 3,190 points
958 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,220 views
0 votes
2 answers

Ho do I Iterate through a HashMap which contains duplicate values

for (Map.Entry<String, String> item : params.entrySet()) { ...READ MORE

answered Jul 24, 2018 in Java by samarth295
• 2,220 points
5,286 views
0 votes
2 answers

Performing Iteration over each entry in a 'Map'

In Java 8 you can do it ...READ MORE

answered Oct 24, 2018 in Java by Sushmita
• 6,920 points
1,102 views
0 votes
2 answers
0 votes
1 answer

How can I Sort an ArrayList in Java

You can sort the ArrayList in 2 ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,370 points
860 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,310 views
0 votes
3 answers

How to reverse a string in java?

public static String reverse(String s) { ...READ MORE

answered Aug 17, 2018 in Java by samarth295
• 2,220 points
1,568 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP