Tree of Node explaination

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) 1. The tree class should be parameterized to handle a generic type T, allowing trees of strings, files etc... to be created, e.g. `Tree tree = new Tree()` 2. The tree class should implement the tree structure using an adjacency list and be defined in the following way: `Map, List>> tree = new HashMap, List>();` 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 { private Node root; // a T type variable to store the root of the list private Node parent; // a T type variable to store the parent of the list private T child; private List> children = new ArrayList>(); // 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 parent) { this.setParent(parent); //this.addChild(parent); } // constructor overloading to set the parent of the list public Node(Node parent, Node 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 child) { child.root = null; child.setParent((Node)this); this.children.add(child); // add this child to the list } public void removeChild(Node child) { this.children.remove(child); // remove this child from the list } public Node 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 root) { this.root = root; } public Node getParent() { return parent; } public void setParent(Node 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[] children() { return (Node[]) children.toArray(new Node[children.size()]); } @SuppressWarnings({ "unchecked"}) public Node[] getSiblings() { if(this.isRoot()!=false && parent==null) { System.out.println("this is root or there are no siblings"); return null; } else{ List> siblings = new ArrayList>((Collection>) Arrays.asList(new Node[this.parent.children.size()])); Collections.copy(siblings, this.parent.children); siblings.remove(this); return siblings.toArray(new Node[siblings.size()]); } } }

以上就是Tree of Node explaination的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准