Reputation: 443
Don't know if i am allowed to do this according to the rules of the site... but i will take my chance... please bear with me, i am only a student... :-)
I have a college assignment... I am having hard time understanding what the classes should do... i have gone to my teacher on three different occasions and the answer i got from him didn't help at all. Anyway the assignment details goes as follow...
Create a class called Tree
that acts as a container for nodes. The tree class should support the following methods.
public void add(Node parent, Node child){} -- Adds a new child node to the parent node
public void removeChild(Nodeparent, Node child){} -- Removes a child node from a parent.
public Node getRootNode(){} -- Returns the root of the tree
public void setRoot(Node root){} -- Sets the root node of the tree
public boolean contains(T data){} -- Searches the tree for a given type
public void dfs(Node child){} -- Performs a depth-first-search of the tree and outputs each node (indented)
public void bfs(Node child){} -- Performs a breadth-first-search of the tree and outputs each node (indented)
Tree<String> tree = new Tree<String>()
Map<Node<T>, List<Node<T>>> tree = new HashMap<Node<T>, List<Node<T>>();
The node class should also be parameterized to handle a generic type T and expose several methods...
Now i have written my Node class which works fine... and to be honest, i was sure that i have written a Node class which is creating a Tree. but after reading the Tree class description i am confused. What i should store in the tree Map. I am having hard time visualizing the whole thing.
Perhaps someone can explain what teacher wants and put me in the right direction. I am NOT looking for the code itself... just want to understand what i am suppose to do.
My Node Class
public class Node<T>
{
private Node<T> root; // a T type variable to store the root of the list
private Node<T> parent; // a T type variable to store the parent of the list
private T child;
private List<Node<T>> children = new ArrayList<Node<T>>(); // a T type list to store the children of the list
// default constructor
public Node(T child)
{
setParent(null);
setRoot(null);
setItem(child);
}
// constructor overloading to set the parent
public Node(Node<T> parent)
{
this.setParent(parent);
//this.addChild(parent);
}
// constructor overloading to set the parent of the list
public Node(Node<T> parent, Node<T> child)
{
this(parent);
this.children.add(child);
}
/**
* This method doesn't return anything and takes a parameter of
* the object type you are trying to store in the node
*
* @param Obj an object
* @param
**/
public void addChild(Node<T> child)
{
child.root = null;
child.setParent((Node<T>)this);
this.children.add(child); // add this child to the list
}
public void removeChild(Node<T> child)
{
this.children.remove(child); // remove this child from the list
}
public Node<T> getRoot() {
return root;
}
public boolean isRoot()
{
// check to see if the root is null if yes then return true else return false
return this.root != null;
}
public void setRoot(Node<T> root) {
this.root = root;
}
public Node<T> getParent() {
return parent;
}
public void setParent(Node<T> parent) {
this.parent = parent;
}
public T getItem() {
return child;
}
public void setItem(T child) {
this.child = child;
}
public boolean hasChildren()
{
return this.children.size()>0;
}
@SuppressWarnings("unchecked")
public Node<T>[] children()
{
return (Node<T>[]) children.toArray(new Node[children.size()]);
}
@SuppressWarnings({ "unchecked"})
public Node<T>[] getSiblings()
{
if(this.isRoot()!=false && parent==null)
{
System.out.println("this is root or there are no siblings");
return null;
}
else{
List<Node<T>> siblings = new ArrayList<Node<T>>((Collection<? extends Node<T>>) Arrays.asList(new Node[this.parent.children.size()]));
Collections.copy(siblings, this.parent.children);
siblings.remove(this);
return siblings.toArray(new Node[siblings.size()]);
}
}
}
Upvotes: 5
Views: 25591
Reputation: 4704
You use the map for the following thing:
The key of the hashmap is a given node, and the value of the hashmap are the child nodes for the given node.
public class Tree<T> {
private Node<T> rootNode;
private HashMap<Node<T>, List<Node<T>> tree;
//and then some kind of function to go through the tree.
public void expandNode(Node<T> node) {
if (tree.get(node) == null) {
System.out.println(node);
return;
}
for(Node<T> n : tree.get(node)) {
System.out.println(node);
expandNode(n);
}
}
}
Could I make it clear how the tree works??
Upvotes: 8
Reputation: 25686
This is a bit strange. If I were doing this and the assignment didn't say otherwise, I probably wouldn't write a Tree class at all; I'd just use Node as a recursive data structure, and have the root node of a tree represent the entire tree.
Since it doesn't look like you're supposed to do this, you could just make Tree<T>
a wrapper class that has a reference to a single Node<T>
object. You can put the logic for the required methods in either class.
The beginning of the code could look something like this:
public class Tree<T> {
private Node<T> root;
public Tree(Node<T> root) {
this.root = root;
}
}
Upvotes: 0
Reputation: 3526
Looking at the 2 points in the list, I'm going to guess that point number 1 is the most confusing to you.
The tree itself can be parameterized. The type parameter on the tree class can be used inside an inner class that is used to create the nodes. That is to say, for the purposes of your assignment, the node class should probably be inside the Tree class so that it can use the T type parameter given to the Tree class.
That way, the tree can store anything (strings, files, Doubles, etc). The Node class simply acts as a nice way to store any type T object.
Upvotes: 0