Note that there are some explanatory texts on larger screens.

plurals
  1. POTree of Node<T> explaination
    text
    copied!<p>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... :-)</p> <p>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...</p> <p>Create a class called <code>Tree</code> that acts as a container for nodes. The tree class should support the following methods.</p> <blockquote> <p><strong>public void add(Node parent, Node child){}</strong> -- Adds a new child node to the parent node</p> <p><strong>public void removeChild(Nodeparent, Node child){}</strong> -- Removes a child node from a parent.</p> <p><strong>public Node getRootNode(){}</strong> -- Returns the root of the tree</p> <p><strong>public void setRoot(Node root){}</strong> -- Sets the root node of the tree</p> <p><strong>public boolean contains(T data){}</strong> -- Searches the tree for a given type</p> <p><strong>public void dfs(Node child){}</strong> -- Performs a depth-first-search of the tree and outputs each node (indented)</p> <p><strong>public void bfs(Node child){}</strong> -- Performs a breadth-first-search of the tree and outputs each node (indented)</p> </blockquote> <ol> <li>The tree class should be parameterized to handle a generic type T, allowing trees of strings, files etc... to be created, e.g. <code>Tree&lt;String&gt; tree = new Tree&lt;String&gt;()</code></li> <li>The tree class should implement the tree structure using an adjacency list and be defined in the following way: <code>Map&lt;Node&lt;T&gt;, List&lt;Node&lt;T&gt;&gt;&gt; tree = new HashMap&lt;Node&lt;T&gt;, List&lt;Node&lt;T&gt;&gt;();</code></li> </ol> <p>The node class should also be parameterized to handle a generic type T and expose several methods...</p> <p>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.</p> <p>Perhaps someone can explain what teacher wants and put me in the right direction. I am <strong>NOT</strong> looking for the code itself... just want to understand what i am suppose to do.</p> <p>My Node Class</p> <pre><code>public class Node&lt;T&gt; { private Node&lt;T&gt; root; // a T type variable to store the root of the list private Node&lt;T&gt; parent; // a T type variable to store the parent of the list private T child; private List&lt;Node&lt;T&gt;&gt; children = new ArrayList&lt;Node&lt;T&gt;&gt;(); // 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&lt;T&gt; parent) { this.setParent(parent); //this.addChild(parent); } // constructor overloading to set the parent of the list public Node(Node&lt;T&gt; parent, Node&lt;T&gt; 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&lt;T&gt; child) { child.root = null; child.setParent((Node&lt;T&gt;)this); this.children.add(child); // add this child to the list } public void removeChild(Node&lt;T&gt; child) { this.children.remove(child); // remove this child from the list } public Node&lt;T&gt; 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&lt;T&gt; root) { this.root = root; } public Node&lt;T&gt; getParent() { return parent; } public void setParent(Node&lt;T&gt; parent) { this.parent = parent; } public T getItem() { return child; } public void setItem(T child) { this.child = child; } public boolean hasChildren() { return this.children.size()&gt;0; } @SuppressWarnings("unchecked") public Node&lt;T&gt;[] children() { return (Node&lt;T&gt;[]) children.toArray(new Node[children.size()]); } @SuppressWarnings({ "unchecked"}) public Node&lt;T&gt;[] getSiblings() { if(this.isRoot()!=false &amp;&amp; parent==null) { System.out.println("this is root or there are no siblings"); return null; } else{ List&lt;Node&lt;T&gt;&gt; siblings = new ArrayList&lt;Node&lt;T&gt;&gt;((Collection&lt;? extends Node&lt;T&gt;&gt;) Arrays.asList(new Node[this.parent.children.size()])); Collections.copy(siblings, this.parent.children); siblings.remove(this); return siblings.toArray(new Node[siblings.size()]); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload