Javascript program:
let V = 5 ; let WHITE = 1 ; let BLACK = 2 ; class Node { constructor ( data ) { this . data = data ; this . left = this . right = null ; } } class subset { constructor ( ) { this . parent = 0 ; this . rank = 0 ; this . ancestor = 0 ; this . child = 0 ; this . sibling = 0 ; this . color = 0 ; } } class Query { constructor ( L , R ) { this . L = L ; this . R = R ; } } function newNode ( data ) { let node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } function makeSet ( subsets , i ) { if ( i < 1 i > V ) return ; subsets [ i ] . color = WHITE ; subsets [ i ] . parent = i ; subsets [ i ] . rank = 0 ; return ; } function findSet ( subsets , i ) { if ( subsets [ i ] . parent != i ) subsets [ i ] . parent = findSet ( subsets , subsets [ i ] . parent ) ; return subsets [ i ] . parent ; } function unionSet ( subsets , x , y ) { let xroot = findSet ( subsets , x ) ; let yroot = findSet ( subsets , y ) ; if ( subsets [ xroot ] . rank < subsets [ yroot ] . rank ) subsets [ xroot ] . parent = yroot ; else if ( subsets [ xroot ] . rank > subsets [ yroot ] . rank ) subsets [ yroot ] . parent = xroot ; else { subsets [ yroot ] . parent = xroot ; ( subsets [ xroot ] . rank ) ++ ; } } function lcaWalk ( u , q , m , subsets ) { makeSet ( subsets , u ) ; subsets [ findSet ( subsets , u ) ] . ancestor = u ; let child = subsets [ u ] . child ; while ( child != 0 ) { lcaWalk ( child , q , m , subsets ) ; unionSet ( subsets , u , child ) ; subsets [ findSet ( subsets , u ) ] . ancestor = u ; child = subsets [ child ] . sibling ; } subsets [ u ] . color = BLACK ; for ( let i = 0 ; i < m ; i ++ ) { if ( q [ i ] . L == u ) { if ( subsets [ q [ i ] . R ] . color == BLACK ) { document . write ( " " + q [ i ] . L + " " + q [ i ] . R + " " , subsets [ findSet ( subsets , q [ i ] . R ) ] . ancestor + " " ) ; } } else if ( q [ i ] . R == u ) { if ( subsets [ q [ i ] . L ] . color == BLACK ) { document . write ( " " + q [ i ] . L + " " + q [ i ] . R + " " , subsets [ findSet ( subsets , q [ i ] . L ) ] . ancestor + " " ) ; } } } return ; } function preprocess ( node , subsets ) { if ( node == null ) return ; preprocess ( node . left , subsets ) ; if ( node . left != null && node . right != null ) { subsets [ node . data ] . child = node . left . data ; subsets [ node . left . data ] . sibling = node . right . data ; } else if ( ( node . left != null && node . right == null ) || ( node . left == null && node . right != null ) ) { if ( node . left != null && node . right == null ) subsets [ node . data ] . child = node . left . data ; else subsets [ node . data ] . child = node . right . data ; } preprocess ( node . right , subsets ) ; } function initialise ( subsets ) { for ( let i = 1 ; i < subsets . length ; i ++ ) { subsets [ i ] = new subset ( ) ; subsets [ i ] . color = WHITE ; } return ; } function printLCAs ( root , q , m ) { let subsets = new Array ( V + 1 ) ; initialise ( subsets ) ; preprocess ( root , subsets ) ; lcaWalk ( root . data , q , m , subsets ) ; } let root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; let q = new Array ( 3 ) ; q [ 0 ] = new Query ( 5 , 4 ) ; q [ 1 ] = new Query ( 1 , 3 ) ; q [ 2 ] = new Query ( 2 , 3 ) ; let m = q . length ; printLCAs ( root , q , m ) ;

Index/key variables: child, i, u, xroot, yroot
Accumulator variables: i
Iterator variables: i
Class/struct names: Node, Query, subset