using
System;
using
System.Collections.Generic;
class
GFG
{
static
int
maxsize = 100005;
static
int
[]xor_tree =
new
int
[maxsize];
static
void
construct_Xor_Tree_Util(
int
[]current,
int
start,
int
end,
int
x)
{
if
(start == end)
{
xor_tree[x] = current[start];
return
;
}
int
left = x * 2 + 1;
int
right = x * 2 + 2;
int
mid = start + (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]);
}
static
void
construct_Xor_Tree(
int
[]arr,
int
n)
{
construct_Xor_Tree_Util(arr, 0, n - 1, 0);
}
public
static
void
Main(String[] args)
{
int
[]leaf_nodes = { 40, 32, 12, 1, 4, 3, 2, 7 };
int
n = leaf_nodes.Length;
construct_Xor_Tree(leaf_nodes, n);
int
x = (
int
)(Math.Ceiling(Math.Log(n)));
int
max_size = 2 * (
int
)Math.Pow(2, x) - 1;
Console.Write(
"Nodes of the XOR Tree:\n"
);
for
(
int
i = 0; i < max_size; i++)
{
Console.Write(xor_tree[i] +
" "
);
}
int
root = 0;
Console.Write(
"\nRoot: "
+ xor_tree[root]);
}
}