Construct Binary Tree from Ancestor Matrix | Top Down Approach
Given an ancestor matrix mat[n][n] where the ancestor matrix is defined as below.
mat[i][j] = 1 if i is ancestor of j
mat[i][j] = 0, otherwise
Construct a Binary Tree from the given ancestor matrix where all its values of nodes are from 0 to n-1.
- It may be assumed that the input provided by the program is valid and the tree can be constructed out of it.
- Many Binary trees can be constructed from one input. The program will construct any one of them.
Examples:
Input:
{ {0, 1, 1},
{0, 0, 0},
{0, 0, 0}
};
Output:
0
/ \
1 2
Input:
{ {0, 0, 0, 1, 1, 0},
{0, 0, 0, 0, 0, 1},
{1, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
};
Output:
2
/ \
0 1
/ \ /
3 4 5
Please refer to this article for Bottom-Up approach: Construct tree from ancestor matrix
Approach: First, we will find the root of the tree. The root is the one whose column has all zeros. Once we find the root, we can then construct a tree from the root using DFS recursive approach.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
Node( int _val)
{
data = _val;
left = right = NULL;
}
};
int getRootIndex(vector<vector< int > >& arr)
{
int root_index = -1;
for ( int j = 0; j < arr[0].size(); j++) {
int count = 0;
for ( int i = 0; i < arr.size(); i++)
if (arr[i][j] == 0) {
count++;
}
if (count == arr.size()) {
root_index = j;
break ;
}
}
return root_index;
}
void printInorder(Node* node)
{
if (node == NULL) {
return ;
}
printInorder(node->left);
cout << node->data << " " ;
printInorder(node->right);
}
Node* createTreeRec(vector<vector< int > >& arr, int index)
{
Node* node = new Node(index);
int left = 1;
for ( int j = 0; j < arr[index].size(); j++) {
if (arr[index][j] == 1) {
if (left == 1) {
node->left = createTreeRec(arr, j);
}
else if (left == 2) {
node->right = createTreeRec(arr, j);
}
left++;
}
}
return node;
}
int main()
{
vector<vector< int > > arr = {
{ 0, 0, 0, 1, 1, 0 },
{ 0, 0, 0, 0, 0, 1 },
{ 1, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
};
int root_index = getRootIndex(arr);
Node* root = createTreeRec(arr, root_index);
printInorder(root);
return 0;
}
|
Java
import java.io.*;
public class GFG {
static class Node {
int data;
Node left, right;
Node( int _val)
{
data = _val;
left = right = null ;
}
};
static int getRootIndex( int [][] arr)
{
int root_index = - 1 ;
for ( int j = 0 ; j < arr[ 0 ].length; j++) {
int count = 0 ;
for ( int i = 0 ; i < arr.length; i++)
if (arr[i][j] == 0 ) {
count++;
}
if (count == arr.length) {
root_index = j;
break ;
}
}
return root_index;
}
static void printInorder(Node node)
{
if (node == null ) {
return ;
}
printInorder(node.left);
System.out.print(node.data + " " );
printInorder(node.right);
}
static Node createTreeRec( int [][] arr, int index)
{
Node node = new Node(index);
int left = 1 ;
for ( int j = 0 ; j < arr[index].length; j++) {
if (arr[index][j] == 1 ) {
if (left == 1 ) {
node.left = createTreeRec(arr, j);
}
else if (left == 2 ) {
node.right = createTreeRec(arr, j);
}
left++;
}
}
return node;
}
public static void main(String[] args)
{
int [][] arr = {
{ 0 , 0 , 0 , 1 , 1 , 0 }, { 0 , 0 , 0 , 0 , 0 , 1 },
{ 1 , 1 , 0 , 0 , 0 , 0 }, { 0 , 0 , 0 , 0 , 0 , 0 },
{ 0 , 0 , 0 , 0 , 0 , 0 }, { 0 , 0 , 0 , 0 , 0 , 0 },
};
int root_index = getRootIndex(arr);
Node root = createTreeRec(arr, root_index);
printInorder(root);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def getRootIndex(arr : list ) - > int :
root_index = - 1
for j in range ( len (arr)):
count = 0
for i in range ( len (arr)):
if (arr[i][j] = = 0 ):
count + = 1
if (count = = len (arr)):
root_index = j
break
return root_index
def printInorder(node : Node) - > None :
if (node is None ):
return
printInorder(node.left)
print (node.data, end = " " )
printInorder(node.right)
def createTreeRec(arr : list ,
index : int ) - > Node:
node = Node(index)
left = 1
for j in range ( len (arr[index])):
if (arr[index][j] = = 1 ):
if (left = = 1 ):
node.left = createTreeRec(arr, j)
elif (left = = 2 ):
node.right = createTreeRec(arr, j)
left + = 1
return node
if __name__ = = "__main__" :
arr = [[ 0 , 0 , 0 , 1 , 1 , 0 ],
[ 0 , 0 , 0 , 0 , 0 , 1 ],
[ 1 , 1 , 0 , 0 , 0 , 0 ],
[ 0 , 0 , 0 , 0 , 0 , 0 ],
[ 0 , 0 , 0 , 0 , 0 , 0 ],
[ 0 , 0 , 0 , 0 , 0 , 0 ]]
root_index = getRootIndex(arr)
root = createTreeRec(arr, root_index)
printInorder(root)
|
C#
using System;
class GFG
{
class Node
{
public int data;
public Node left, right;
public Node( int _val)
{
data = _val;
left = right = null ;
}
};
static int getRootIndex( int [,]arr)
{
int root_index = -1;
for ( int j = 0; j < arr.GetLength(0); j++)
{
int count = 0;
for ( int i = 0; i < arr.GetLength(1); i++)
if (arr[i, j] == 0)
{
count++;
}
if (count == arr.GetLength(0))
{
root_index = j;
break ;
}
}
return root_index;
}
static void printInorder(Node node)
{
if (node == null )
{
return ;
}
printInorder(node.left);
Console.Write(node.data + " " );
printInorder(node.right);
}
static Node createTreeRec( int [,]arr, int index)
{
Node node = new Node(index);
int left = 1;
for ( int j = 0; j < arr.GetLength(1); j++)
{
if (arr[index, j] == 1)
{
if (left == 1)
{
node.left = createTreeRec(arr, j);
}
else if (left == 2)
{
node.right = createTreeRec(arr, j);
}
left++;
}
}
return node;
}
public static void Main(String[] args)
{
int [,] arr = {
{ 0, 0, 0, 1, 1, 0 },
{ 0, 0, 0, 0, 0, 1 },
{ 1, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
};
int root_index = getRootIndex(arr);
Node root = createTreeRec(arr, root_index);
printInorder(root);
}
}
|
Javascript
<script>
class Node
{
constructor(_val)
{
this .data = _val;
this .left = null ;
this .right = null ;
}
};
function getRootIndex(arr)
{
var root_index = -1;
for ( var j = 0; j < arr.length; j++)
{
var count = 0;
for ( var i = 0; i < arr.length; i++)
if (arr[i][j] == 0)
{
count++;
}
if (count == arr[0].length)
{
root_index = j;
break ;
}
}
return root_index;
}
function printInorder(node)
{
if (node == null )
{
return ;
}
printInorder(node.left);
document.write(node.data + " " );
printInorder(node.right);
}
function createTreeRec(arr, index)
{
var node = new Node(index);
var left = 1;
for ( var j = 0; j < arr.length; j++)
{
if (arr[index][j] == 1)
{
if (left == 1)
{
node.left = createTreeRec(arr, j);
}
else if (left == 2)
{
node.right = createTreeRec(arr, j);
}
left++;
}
}
return node;
}
var arr = [
[ 0, 0, 0, 1, 1, 0 ],
[ 0, 0, 0, 0, 0, 1 ],
[ 1, 1, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0 ]
];
var root_index = getRootIndex(arr);
var root = createTreeRec(arr, root_index);
printInorder(root);
</script>
|
Time Complexity: O(N2), where N is the size of the binary tree.
Auxiliary Space: O(N)
Last Updated :
09 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...