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 8, 2018 in Java by Daisy
• 8,040 points
280 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 8, 2018 by sharth
• 3,320 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,840 points

edited Dec 10, 2018 by Sushmita 55 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
15 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,150 points
71 views
+5 votes
3 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,380 points

edited Nov 6, 2018 by Omkar 6,188 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,190 points
111 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,840 points
38 views
0 votes
1 answer

How can I convert an 'ArrayList<String> to 'String[]' in Java

List<String> list = new ArrayList<String>(); String[] array = ...READ MORE

answered Apr 20, 2018 in Java by Daisy
• 8,040 points
115 views
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,320 points
49 views
0 votes
1 answer

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

You can convert a String to int by using a function: int ...READ MORE

answered Apr 12, 2018 in Java by sharth
• 3,320 points
85 views
0 votes
3 answers

How to reverse a string in java?

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

answered Aug 16, 2018 in Java by samarth295
• 2,190 points
67 views