Construct XOR tree by Given leaf nodes of Perfect Binary Tree | Maximum size for xor tree ; Allocating space to xor tree ; A recursive function that constructs xor tree for vector array [ start ... . . end ] . x is index of current node in XOR tree ; If there is one element in vector array , store it in current node of XOR tree ; cout << xor_tree [ x ] << " ▁ x " ; ; for left subtree ; for right subtree ; for getting the middle index from corner indexes . ; Build the left and the right subtrees by xor operation ; merge the left and right subtrees by XOR operation ; Function to construct XOR tree from the given vector array . This function calls construct_Xor_Tree_Util ( ) to fill the allocated memory of xor_tree vector array ; leaf nodes of Perfect Binary Tree ; Build the xor tree ; Height of xor tree ; Maximum size of xor tree ; Root node is at index 0 considering 0 - based indexing in XOR Tree ; print value at root node
var maxsize = 100005 ; var xor_tree = Array ( maxsize ) ; function construct_Xor_Tree_Util ( current , start , end , x ) { if ( start == end ) { xor_tree [ x ] = current [ start ] ; return ; } var left = x * 2 + 1 ; var right = x * 2 + 2 ; var mid = start + parseInt ( ( end - start ) / 2 ) ; construct_Xor_Tree_Util ( current , start , mid , left ) ; construct_Xor_Tree_Util ( current , mid + 1 , end , right ) ; xor_tree [ x ] = ( xor_tree [ left ] ^ xor_tree [ right ] ) ; } function construct_Xor_Tree ( arr , n ) { construct_Xor_Tree_Util ( arr , 0 , n - 1 , 0 ) ; } var leaf_nodes = [ 40 , 32 , 12 , 1 , 4 , 3 , 2 , 7 ] ; var n = leaf_nodes . length ; construct_Xor_Tree ( leaf_nodes , n ) ; var x = ( Math . ceil ( Math . log2 ( n ) ) ) ; var max_size = 2 * Math . pow ( 2 , x ) - 1 ; document . write ( " " ) ; for ( var i = 0 ; i < max_size ; i ++ ) { document . write ( xor_tree [ i ] + " " ) ; } var root = 0 ; document . write ( " " + xor_tree [ root ] ) ;