Edited by Nyfer, 08 November 2011 - 07:50 AM.
8 replies to this topic
#1
Posted 07 November 2011 - 09:53 PM
hey guys i want to implement tree in java.. basic operations such as insert,delete,search.. how to go about it?
|
|
|
#2
Posted 09 November 2011 - 01:51 PM
Not trying to be mean, but did you ever try google search? Usually that solves most problems. If that and the articles below don't help just come back... give us the code your working with... and we will go from there.
I suggest you look at the following:
How to Use Trees (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
Swing Tutorial: JTree
Understanding the TreeModel
I suggest you look at the following:
How to Use Trees (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
Swing Tutorial: JTree
Understanding the TreeModel
#3
Posted 09 November 2011 - 08:18 PM
i want to create nodes base on user input. e.g Suppose if have a class called nodes. and then am asking user to enter the name for node from the console. how can i create different nodes based on user input???
#4
Posted 09 November 2011 - 08:49 PM
You can use a switch statement or if statemets... then once their input meets a true or false statement then you can add their input with those nodes.
#5
Posted 09 November 2011 - 08:59 PM
but i want to create different objects so that i can added to tree.
#6
Posted 10 November 2011 - 08:25 AM
You want multiple trees, correct?
If so then create multiple trees and name them different things. If you want you can make a class out of it that you can manipulate.
Have you looked at the tutorial? Show me some code or at least some pseudo code so I can help you from there.
If so then create multiple trees and name them different things. If you want you can make a class out of it that you can manipulate.
Have you looked at the tutorial? Show me some code or at least some pseudo code so I can help you from there.
#7
Posted 10 November 2011 - 03:14 PM
You can also do a search for Tree Data Structures to know the basic idea of how operations on trees work. The implementation is different across all languages, but the basic idea of a tree remains true.
#8
Posted 10 November 2011 - 08:33 PM
following are the classes which i got from some site
Node class
Tree class
I dont know whether their correct or not
Node class
import java.util.ArrayList;
import java.util.List;
public class Node<E> {
public E data;
public List<Node<E>> children;
public Node() {
super();
}
public Node(E data) {
this();
setData(data);
}
public List<Node<E>> getChildren() {
if (this.children == null) {
return new ArrayList<Node<E>>();
}
return this.children;
}
public void setChildren(List<Node<E>> children) {
this.children = children;
}
public int getNumberOfChildren() {
if (children == null) {
return 0;
}
return children.size();
}
public void addChild(Node<E> child) {
if (children == null) {
children = new ArrayList<Node<E>>();
}
children.add(child);
}
public void insertChildAt(int index, Node<E> child) throws IndexOutOfBoundsException {
if (index == getNumberOfChildren()) {
// this is really an append
addChild(child);
return;
} else {
children.get(index); //just to throw the exception, and stop here
children.add(index, child);
}
}
public void removeChildAt(int index) throws IndexOutOfBoundsException {
children.remove(index);
}
public E getData() {
return this.data;
}
public void setData(E data) {
this.data = data;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{").append(getData().toString()).append(",[");
int i = 0;
for (Node<E> e : getChildren()) {
if (i > 0) {
sb.append(",");
}
sb.append(e.getData().toString());
i++;
}
sb.append("]").append("}");
return sb.toString();
}
}
Tree class
import java.util.ArrayList;
import java.util.List;
public class Tree<E>
{
private Node<E> rootElement;
public Tree() {
super();
}
public Node<E> getRootElement() {
return this.rootElement;
}
public void setRootElement(Node<E> rootElement) {
this.rootElement = rootElement;
}
public List<Node<E>> toList() {
List<Node<E>> list = new ArrayList<Node<E>>();
walk(rootElement, list);
return list;
}
public String toString() {
return toList().toString();
}
private void walk(Node<E> element, List<Node<E>> list) {
list.add(element);
for (Node<E> data : element.getChildren()) {
walk(data, list);
}
}
}
I dont know whether their correct or not
#9
Posted 11 November 2011 - 07:31 AM
Why don't you try to use it? Then report your problems you can't figure out by using Google here.
Happy coding!
Happy coding!
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









