Class Tree


  • public class Tree
    extends java.lang.Object
    Tree data structure.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String POSTORDER
      The postorder constant.
      static java.lang.String PREORDER
      The preorder constant.
      protected TreeNode root
      The root node.
    • Constructor Summary

      Constructors 
      Constructor Description
      Tree()
      Create a new tree.
      Tree​(TreeNode root)
      Create a new tree.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      TreeNode getRootNode()
      Return the root node of the tree.
      boolean isEmpty()
      Test if empty.
      void setRootElement​(TreeNode root)
      Set the root node for the tree.
      java.util.List toList​(java.lang.String order)
      Returns the tree as a List of node objects.
      java.lang.String toString()
      Returns a String representation of the tree.
      protected void walkPostorder​(TreeNode node, java.util.List list)
      Walks the tree in post-order style.
      protected void walkPreorder​(TreeNode node, java.util.List list)
      Walks the tree in pre-order style.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • PREORDER

        public static final java.lang.String PREORDER
        The preorder constant.
        See Also:
        Constant Field Values
      • POSTORDER

        public static final java.lang.String POSTORDER
        The postorder constant.
        See Also:
        Constant Field Values
      • root

        protected TreeNode root
        The root node.
    • Constructor Detail

      • Tree

        public Tree()
        Create a new tree.
      • Tree

        public Tree​(TreeNode root)
        Create a new tree.
    • Method Detail

      • getRootNode

        public TreeNode getRootNode()
        Return the root node of the tree.
        Returns:
        The root node.
      • setRootElement

        public void setRootElement​(TreeNode root)
        Set the root node for the tree.
        Parameters:
        root - The root node to set.
      • toList

        public java.util.List toList​(java.lang.String order)
        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:
        Tree elements.
      • isEmpty

        public boolean isEmpty()
        Test if empty.
      • toString

        public java.lang.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 java.lang.Object
        Returns:
        the String representation of the Tree.
      • walkPreorder

        protected void walkPreorder​(TreeNode node,
                                    java.util.List list)
        Walks the tree in pre-order style.
        Parameters:
        node - The starting element.
        list - The output of the walk.
      • walkPostorder

        protected void walkPostorder​(TreeNode node,
                                     java.util.List list)
        Walks the tree in post-order style.
        Parameters:
        node - The starting element.
        list - The output of the walk.