Interface Tree<U>

Type Parameters:
U - Type of the nodes.
All Superinterfaces:
DirectedGraph<U>, Graph<U>, java.io.Serializable
All Known Subinterfaces:
UnweightedTree<U>, WeightedTree<U>
All Known Implementing Classes:
FastTree, FastUnweightedTree, FastWeightedTree

public interface Tree<U>
extends DirectedGraph<U>
Interface for managing and creating tree graphs.
  • Method Details

    • getRoot

      U getRoot()
      Obtains the root of the graph.
      Returns:
      the root of the graph.
    • getLeaves

      java.util.stream.Stream<U> getLeaves()
      Obtain the leaves of the tree.
      Returns:
      a stream containing the leaves of the tree.
    • getLeaves

      java.util.stream.Stream<U> getLeaves​(U u)
      Obtains the leaves of a tree that come from a certain node
      Parameters:
      u - the leaves of the tree.
      Returns:
      a stream containing the selected leaves of the tree.
    • getLevel

      java.util.stream.Stream<U> getLevel​(int level)
      Obtains all the nodes in a given level of the tree. Root equal to 0.
      Parameters:
      level - the level to retrieve.
      Returns:
      the nodes in the corresponding level.
    • getLevels

      java.util.Map<java.lang.Integer,​java.util.Set<U>> getLevels()
      Obtains all the nodes on each level.
      Returns:
      all the nodes on each level.
    • getChildren

      java.util.stream.Stream<U> getChildren​(U u)
      Obtains the direct children of a node.
      Parameters:
      u - the parent node
      Returns:
      A stream containing the children of a node. If it is a leaf, an empty stream is returned. If the node does not exist, null.
    • getChildrenCount

      long getChildrenCount​(U u)
      Gets the number of children of a node.
      Parameters:
      u - the node.
      Returns:
      the number of children, -1 if the node does not exist.
    • getChildrenWeights

      java.util.stream.Stream<Weight<U,​java.lang.Double>> getChildrenWeights​(U u)
      Obtains the weights of the children of a node.
      Parameters:
      u - The parent node.
      Returns:
      A stream containing the children of the node if it exists, null if not.
    • getParentWeight

      double getParentWeight​(U u)
      Obtains the weight of the edge between a node and its parent
      Parameters:
      u - The child node
      Returns:
      The weight if the node exists and it is not the root, NaN otherwise.
    • getParent

      U getParent​(U u)
      Obtains the parent of a node.
      Parameters:
      u - the child.
      Returns:
      the parent of the node, null if the node does not exist, or the node is the root.
    • isLeaf

      boolean isLeaf​(U u)
      Indicates if the node is a leaf or not.
      Parameters:
      u - the user
      Returns:
      true if the node is a leaf, false if the node is not a leaf or it does not exist.
    • isRoot

      boolean isRoot​(U u)
      Indicates if the node is the root node or not.
      Parameters:
      u - the user.
      Returns:
      true if the node is the root, false if the node is not the root or it does not exist.
    • addRoot

      boolean addRoot​(U root)
      Adds a root to the tree.
      Parameters:
      root - the new root of the tree.
      Returns:
      true if the root is correctly added, false if there is already a root in the graph.
    • addChild

      default boolean addChild​(U parent, U child)
      Adds a child to a given node.
      Parameters:
      parent - the parent node.
      child - the child node to add.
      Returns:
      true if the child is correctly added, false if not.
    • addChild

      default boolean addChild​(U parent, U child, double weight)
      Adds a child to a given node
      Parameters:
      parent - the parent node.
      child - the child node to add.
      weight - the weight of the edge
      Returns:
      true if the child is correctly added, false if not.
    • addChild

      boolean addChild​(U parent, U child, double weight, int type)
      Adds a child to a given node.
      Parameters:
      parent - the parent node.
      child - the child node to add.
      weight - the weight of the edge.
      type - the type of the edge.
      Returns:
      true if the child is correcly added, false if not.
    • isParent

      boolean isParent​(U parent, U child)
      Checks if a node is the parent of another one.
      Parameters:
      parent - the parent node.
      child - the node we want to check.
      Returns:
      true if the child
    • isChild

      default boolean isChild​(U child, U parent)
      Checks if a node is the parent of another one.
      Parameters:
      child - the node we want to check.
      parent - the parent node.
      Returns:
      true if the child
    • getDescendants

      Tree<U> getDescendants​(U parent)
      Obtains the subtree formed by the user and its descendants.
      Parameters:
      parent - the parent node.
      Returns:
      a tree rooted in the parent if the node exists, null if not
    • isAscendant

      boolean isAscendant​(U parent, U child)
      Checks if a node is ascendant of another one
      Parameters:
      parent - the first node (the possible ascendant)
      child - the second node (the possible descendant)
      Returns:
      true if the second node descends from the first, false if not.
    • isDescendant

      default boolean isDescendant​(U child, U parent)
      Checks if a node is descendant of another one
      Parameters:
      child - the first node (the possible descendant)
      parent - the second node (the possible ascendant)
      Returns:
      true if the second node descends from the first, false if not.
    • addNode

      default boolean addNode​(U node)
      Description copied from interface: Graph
      Adds a new node to the graph.
      Specified by:
      addNode in interface Graph<U>
      Parameters:
      node - The new node to add.
      Returns:
      true if the node is correctly added, false if not.
    • addEdge

      default boolean addEdge​(U nodeA, U nodeB, double weight, int type, boolean insertNodes)
      Description copied from interface: Graph
      Adds a weighted edge to the graph.
      Specified by:
      addEdge in interface Graph<U>
      Parameters:
      nodeA - The incident node.
      nodeB - The adjacent node.
      weight - The weight.
      type - The edge type.
      insertNodes - If true, nodes will be inserted if they do not exist. If false, the edge will only be added if both nodes are inserted.
      Returns:
      if the edge is correctly added, false if not.