Package com.jgalgo

Interface LowestCommonAncestorDynamic


  • public interface LowestCommonAncestorDynamic
    Dynamic 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