Hello, I would like to ask is there any class connected with the Tree data structure in the Java Api, since I couldn't find one. I'm just starting to study the Binary Search Trees, the Heap data structure etc. As far as I could see until now, I will have to define my own classes when working with Trees... I mean, this is not what I like to do, defining classes like this all the time:

class TwoChildNode{
 protected Object data;
 protected TwoChildNode left,right;
 public TwoChildNode(){
 data = null;
 left = right = null;
 }

 public TwoChildNode(Object d){
 data = d;
 left = right = null;
 }
 public void setLeft(TwoChildNode l){
 left = l;
 }

 public void setRight(TwoChildNode r){
 right = r;
 }

 public void setData(Object d){
 data = d;
 }

 public TwoChildNode getLeft(){
 return left;
 }

 public TwoChildNode getRight(){
 return right;
 }

 public Object getData(){
 return data;
 }

 public String toString(){
 return ""+data;
 }
}

public class BinaryTree {
 TwoChildNode root; //sekoe drvo ima samo eden koren
 public BinaryTree()
 {
 root=new TwoChildNode(); //kreiranje na koren
 }

 public boolean setRoot(Object o)
 {
 //ako veke korenot e postaven,nemoze da postavime nov
 if(root.getData()!=null) return false;
 else
 root.setData(o);
 return true;
 }

What I mean is - is there any class like the LinkedList, or the ArrayList class that I will be able to use instead ? Thank for any answer.

OK, thanks for this one, but I have seen these classes, and I am asking if there's another class somewhere, 'cause these are not quite a tree class... I guess I will have to work with my own classes then.

If you are learning the concept of Binary Search Trees, Heap etc, it would be better option that you implement them by hand rather than using Collections.
That way you would understand the concept better and also improve your logical thinking ability and the ability of transferring logic to code.

However while designing real world applications I suppose the TreeSet, TreeMap or some other class of the java.util package will suite the job description better than you think right now.

Well, you are right. I kind of got into this these days, and it is interesting indeed. Thanks for the help, I appreciate it. Regards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.