Class NaryTreeNode<D,​N extends NaryTreeNode<D,​N>>

  • Type Parameters:
    D - is the type of the data inside the tree
    N - is the type of the tree nodes.
    All Implemented Interfaces:
    Serializable, Comparable<N>, IterableNode<N>, TreeNode<D,​N>, JsonableObject
    Direct Known Subclasses:
    NaryTreeNode.DefaultNaryTreeNode

    public abstract class NaryTreeNode<D,​N extends NaryTreeNode<D,​N>>
    extends AbstractTreeNode<D,​N>
    This is the generic implementation of a n-ary tree. This node has not a constant count of children. Indeed when a child node was removed, the count of children is also changed. It also means that a child node is never null. If you want a generic implementation of a tree node which has always the same count of children, please see ConstantNaryTreeNode.

    moveTo

    According to its definition in TreeNode.moveTo(TreeNode, int), the binary tree node implementation of moveTo does not replace any existing node at the position given as parameter of moveTo. In place moveTo insert the moving node in the new parent.
    Since:
    13.0
    Version:
    17.0 2020-01-04 14:41:41
    Author:
    Stéphane GALLAND
    See Also:
    ConstantNaryTreeNode, Serialized Form
    Maven Group Id:
    org.arakhne.afc.core
    Maven Artifact Id:
    mathtree
    • Constructor Detail

      • NaryTreeNode

        public NaryTreeNode()
        Empty node.
      • NaryTreeNode

        public NaryTreeNode​(Collection<D> data)
        Constructor.
        Parameters:
        data - are the initial user data.
      • NaryTreeNode

        public NaryTreeNode​(D data)
        Constructor.
        Parameters:
        data - are the initial user data.
      • NaryTreeNode

        public NaryTreeNode​(boolean useLinkedList)
        Constructor.
        Parameters:
        useLinkedList - indicates if a linked list must be used to store the data. If false, an ArrayList will be used.
      • NaryTreeNode

        public NaryTreeNode​(boolean useLinkedList,
                            boolean copyDataCollection,
                            List<D> data)
        Constructor.
        Parameters:
        useLinkedList - indicates if a linked list must be used to store the data. If false, an ArrayList will be used.
        copyDataCollection - indicates if the given data collection is copied if true or the inner data collection will be the given collection itself if false.
        data - are the initial user data
      • NaryTreeNode

        public NaryTreeNode​(boolean useLinkedList,
                            D data)
        Constructor.
        Parameters:
        useLinkedList - indicates if a linked list must be used to store the data. If false, an ArrayList will be used.
        data - are the initial user data
    • Method Detail

      • getPartitionEnumeration

        @Pure
        public Class<? extends Enum<?>> getPartitionEnumeration()
        Description copied from interface: TreeNode
        Replies the enumeration type that is defining the children of the nodes.
        Returns:
        the enumeration type of the child partition, or null if no such enumeration type is defined.
      • clear

        public void clear()
        Clear the tree.

        Caution: this method also destroyes the links between the child nodes inside the tree. If you want to unlink the first-level child node with this node but leave the rest of the tree unchanged, please call setChildAt(i,null).

      • getNotNullChildCount

        @Pure
        public int getNotNullChildCount()
        Description copied from interface: IterableNode
        Replies count of not-null children in this node.

        The number of not-null children is lower or equal to the value replied by IterableNode.getChildCount().

        Returns:
        the count of not-null children.
        See Also:
        IterableNode.getChildCount()
      • indexOf

        @Pure
        public final int indexOf​(N child)
        Description copied from interface: TreeNode
        Replies the index of the specified child.
        Parameters:
        child - is the node to search for.
        Returns:
        the index or -1.
      • getChildAt

        @Pure
        public final N getChildAt​(int index)
                           throws IndexOutOfBoundsException
        Description copied from interface: IterableNode
        Replies the n-th child in this node.
        Parameters:
        index - is the index of the child to reply
        Returns:
        the child node.
        Throws:
        IndexOutOfBoundsException - if the given index was invalid
      • moveTo

        public boolean moveTo​(N newParent)
        Move this node in the given new parent node.

        This function adds this node at the end of the list of the children of the new parent node.

        This function is preferred to a sequence of calls to AbstractTreeNode.removeFromParent() and setChildAt(int, NaryTreeNode) because it fires a limited set of events dedicated to the move the node.

        Parameters:
        newParent - is the new parent for this node.
        Returns:
        true on success, otherwise false.
      • moveTo

        public boolean moveTo​(N newParent,
                              int index)
        Description copied from interface: TreeNode
        Move this node in the given new node.

        If any child node is already present at the given position in the new parent node, the tree node may replace the existing node or insert the moving node according to its implementation.

        This function is preferred to a sequence of calls to IterableNode.removeFromParent() and TreeNode.setChildAt(int, TreeNode) because it fires a limited set of events dedicated to the move the node.

        Parameters:
        newParent - is the new parent for this node.
        index - is the position of this node in the new parent.
        Returns:
        true on success, otherwise false.
      • removeChild

        public final boolean removeChild​(N child)
        Description copied from interface: TreeNode
        Remove the specified node from this node if it is a child.
        Parameters:
        child - is the child to remove.
        Returns:
        true if the node was removed, otherwise false
      • addChild

        public final boolean addChild​(N child)
        Add a child node at the end of the children list.
        Parameters:
        child - is the new child to insert.
        Returns:
        true on success, otherwise false
      • addChild

        public final boolean addChild​(int index,
                                      N newChild)
        Add a child node at the specified index.
        Parameters:
        index - is the insertion index.
        newChild - is the new child to insert.
        Returns:
        true on success, otherwise false
      • isLeaf

        @Pure
        public final boolean isLeaf()
        Description copied from interface: IterableNode
        Replies if this node is a leaf.
        Returns:
        true is this node is a leaf, otherwise false
      • getChildren

        public void getChildren​(Object[] array)
        Description copied from interface: TreeNode
        Replies the child nodes of this node.

        This function may put null in the array cells if the current tree node has not a child at the corresponding index.

        Parameters:
        array - is the array to fill.
        See Also:
        TreeNode.getChildren(Class), TreeNode.children()
      • getMinHeight

        @Pure
        public int getMinHeight()
        Description copied from interface: TreeNode
        Replies the minimal height of the tree.
        Returns:
        the height of the uppest leaf in the tree.
      • getMaxHeight

        @Pure
        public int getMaxHeight()
        Description copied from interface: TreeNode
        Replies the maximal height of the tree.
        Returns:
        the height of the lowest leaf in the tree.
      • getHeights

        protected void getHeights​(int currentHeight,
                                  List<Integer> heights)
        Description copied from class: AbstractParentlessTreeNode
        Replies the heights of all the leaf nodes. The order of the heights is given by a depth-first iteration.
        Specified by:
        getHeights in class AbstractParentlessTreeNode<D,​N extends NaryTreeNode<D,​N>>
        Parameters:
        currentHeight - is the current height of this node.
        heights - is the list of heights to fill