Class Tree

  • All Implemented Interfaces:
    Serializable

    public class Tree
    extends Object
    implements Serializable
    Represents a Tree of Objects. The Tree is represented as a single rootElement which points to a List<Node> of children. Adapted from public domain code at http://sujitpal.blogspot.com/.
    See Also:
    Serialized Form
    • Constructor Detail

      • Tree

        public Tree()
        Default ctor.
    • Method Detail

      • getRootElement

        public Node getRootElement()
        Return the root Node of the tree.
        Returns:
        the root element.
      • setRootElement

        public void setRootElement​(Node rootElement)
        Set the root Element for the tree.
        Parameters:
        rootElement - the root element to set.
      • toList

        public List<Node> toList()
        Returns the Tree as a List of Node objects. The elements of the List are generated from a pre-order traversal of the tree.
        Returns:
        a List<Node>.
      • toString

        public String toString()
        Returns a String representation of the Tree. The elements are generated from a pre-order traversal of the Tree.
        Overrides:
        toString in class Object
        Returns:
        the String representation of the Tree.
      • find

        public Node find​(String nodeData)
        Find the Node in the tree containing the supplied data. Stops searching at the first match.
        Parameters:
        nodeData - the node data
        Returns:
        matching Node if found, NULL otherwise
      • find

        public Node find​(Node element,
                         String nodeData)
        Find a Node in a tree, containing the given data
        Parameters:
        element - Node to start searching at
        nodeData - to search for
        Returns:
        matching Node if found, NULL otherwise
      • clear

        public void clear()
      • remove

        public boolean remove​(String nodeData)
        Removes the node with the supplied node data. Deletes the first matching node.
        Parameters:
        nodeData - the node data
        Returns:
        TRUE if a node was found and removed