Skip to content

Commit

Permalink
Merge pull request WaderManasi#1 from utkarshbajpai28/patch-1
Browse files Browse the repository at this point in the history
Create Ancestor matrix for binary tree
  • Loading branch information
kothariji authored Oct 4, 2020
2 parents bae7f06 + 78cfec2 commit f9ad941
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions Tree/Ancestor matrix for binary tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// C++ program to construct ancestor matrix for
// given tree.
#include<bits/stdc++.h>
using namespace std;
#define MAX 100

/* A binary tree node */
struct Node
{
int data;
Node *left, *right;
};

// Creating a global boolean matrix for simplicity
bool mat[MAX][MAX];

// anc[] stores all ancestors of current node. This
// function fills ancestors for all nodes.
// It also returns size of tree. Size of tree is
// used to print ancestor matrix.
int ancestorMatrixRec(Node *root, vector<int> &anc)
{
/* base case */
if (root == NULL) return 0;;

// Update all ancestors of current node
int data = root->data;
for (int i=0; i<anc.size(); i++)
mat[anc[i]][data] = true;

// Push data to list of ancestors
anc.push_back(data);

// Traverse left and right subtrees
int l = ancestorMatrixRec(root->left, anc);
int r = ancestorMatrixRec(root->right, anc);

// Remove data from list the list of ancestors
// as all descendants of it are processed now.
anc.pop_back();

return l+r+1;
}

// This function mainly calls ancestorMatrixRec()
void ancestorMatrix(Node *root)
{
// Create an empty ancestor array
vector<int> anc;

// Fill ancestor matrix and find size of
// tree.
int n = ancestorMatrixRec(root, anc);

// Print the filled values
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
cout << mat[i][j] << " ";
cout << endl;
}
}

/* Helper function to create a new node */
Node* newnode(int data)
{
Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}

/* Driver program to test above functions*/
int main()
{
/* Construct the following binary tree
5
/ \
1 2
/ \ /
0 4 3 */
Node *root = newnode(5);
root->left = newnode(1);
root->right = newnode(2);
root->left->left = newnode(0);
root->left->right = newnode(4);
root->right->left = newnode(3);

ancestorMatrix(root);

return 0;
}

0 comments on commit f9ad941

Please sign in to comment.