Package com.jgalgo
Interface LowestCommonAncestorDynamic
-
public interface LowestCommonAncestorDynamicDynamic algorithm for Lowest Common Ancestor (LCA) queries.The lowest common ancestor of two vertices in a tree is the vertex that appear in both vertices paths to the root (common ancestor), and its farthest from the root (lowest). Algorithm implementing this interface support modifying the tree by adding leafs as children to existing parents nodes, while supporting LCA queries.
LowestCommonAncestorDynamic lca = LowestCommonAncestorDynamic.newBuilder().build(); Node rt = lca.initTree(); Node n1 = lca.addLeaf(rt); Node n2 = lca.addLeaf(rt); Node n3 = lca.addLeaf(n1); assert lca.findLowestCommonAncestor(n1, n2) == rt; assert lca.findLowestCommonAncestor(n1, n3) == n1; Node n4 = lca.addLeaf(n1); assert lca.findLowestCommonAncestor(n1, n4) == n1;- Author:
- Barak Ugav
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceLowestCommonAncestorDynamic.BuilderA builder forLowestCommonAncestorDynamicobjects.static interfaceLowestCommonAncestorDynamic.NodeA tree node in anLowestCommonAncestorDynamicdata structure.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description LowestCommonAncestorDynamic.NodeaddLeaf(LowestCommonAncestorDynamic.Node parent)Add a new leaf node to the tree.voidclear()Clear the data structure by removing all nodes in the tree.LowestCommonAncestorDynamic.NodefindLowestCommonAncestor(LowestCommonAncestorDynamic.Node u, LowestCommonAncestorDynamic.Node v)Find the lowest common ancestor of two nodes in the tree.LowestCommonAncestorDynamic.NodeinitTree()Initialize the tree the LCA will operate on and create a root node.static LowestCommonAncestorDynamic.BuildernewBuilder()Create a new dynamic LCA algorithm builder.intsize()Get the number of nodes in the tree.
-
-
-
Method Detail
-
initTree
LowestCommonAncestorDynamic.Node initTree()
Initialize the tree the LCA will operate on and create a root node.- Returns:
- the new root node
- Throws:
IllegalStateException- if the tree is not empty
-
addLeaf
LowestCommonAncestorDynamic.Node addLeaf(LowestCommonAncestorDynamic.Node parent)
Add a new leaf node to the tree.- Parameters:
parent- parent of the new node- Returns:
- the new node
-
findLowestCommonAncestor
LowestCommonAncestorDynamic.Node findLowestCommonAncestor(LowestCommonAncestorDynamic.Node u, LowestCommonAncestorDynamic.Node v)
Find the lowest common ancestor of two nodes in the tree.- Parameters:
u- the first nodev- the second node- Returns:
- the lowest common ancestor of the two nodes
-
size
int size()
Get the number of nodes in the tree.- Returns:
- number of nodes in the tree
-
clear
void clear()
Clear the data structure by removing all nodes in the tree.
-
newBuilder
static LowestCommonAncestorDynamic.Builder newBuilder()
Create a new dynamic LCA algorithm builder.This is the recommended way to instantiate a new
LowestCommonAncestorDynamicobject.- Returns:
- a new builder that can build
LowestCommonAncestorDynamicobjects
-
-