Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tree of Node<T> explaination

Tags:

java

tree

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<String> tree = new Tree<String>()
  2. The tree class should implement the tree structure using an adjacency list and be defined in the following way: 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()]);
        }
    }
}
like image 508
A Gilani Avatar asked Apr 24 '12 01:04

A Gilani


People also ask

What is the meaning of tree node?

A tree is a collection of entities called nodes . Nodes are connected by edges . Each node contains a value or data , and it may or may not have a child node . The first node of the tree is called the root .

What is degree of tree with example?

The number of subtrees of a node is called its degree. For example, node A is of degree three, while node E is of degree two. The maximum degree of all nodes is called the degree of the tree.


1 Answers

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??

like image 55
Juan Alberto López Cavallotti Avatar answered Oct 09 '22 07:10

Juan Alberto López Cavallotti