Algorithm - Create Ancestor Matrix for Binary Tree - Stack Overflow

2
10/21/12 orithm - create ancestor matrix for binary tree - Stack Overflow 1/2 stackoverflow.com/questions/12664257/create-ancestor-matrix-for-binary-tree jrouquie 1,862 4 12 Sree Ram 36 1 5 1 Answer 1 / \ 2 3 / \ / \ 4 5 6 7 for the given binary tree we need to create a matrix a[7][7] satisfying the ancestor property like a[2][1]=1 since 1 is an ancestor of 2 .... i solved it by using extra space an array ...the solution i came up is int a[n][n]={0}; void updatematrix(int a[][n],struct node *root,int temp[],int index){ if(root == NULL) return ; int i; for(i=0;i< index;i++) a[root->data][temp[i]]=1; temp[index]=root->data; updatematrix(a,root->left,temp,index+1); updatematrix(a,root->right,temp,index+1); } is there any mistake in my solution ? can we do this inplace ???(i mean without using the temp array ) algorithm tree edited Sep 30 at 20:01 asked Sep 30 at 19:20 80% accept rate Perhaps you should do something about your accept rate. – Ted Hopp Sep 30 at 19:23 @TedHopp i am new to this site ..can u pls say how to increase the accept rate ..? – Sree Ram Sep 30 at 19:25 I guess that in your code you need to replace the first occurence of arr by a , and replace the second occurrence of arr by temp . – jrouquie Sep 30 at 19:50 @jrouquie yup ..thank you ..edited .. – Sree Ram Sep 30 at 19:53 Go to some of the questions that you've asked (click on your name to see your profile, which includes links to all your questions). For any question that has answers, if one of the answers resolves your problem, click on the check mark next to the answer. This has several benefits: it lets others know that the question has been answered; it also lets others know which answer worked; it gives +15 reputation bonus to the person who provided the answer; it gives you +2 reputation bonus. – Ted Hopp Sep 30 at 19:59 Was this post useful to you? Yes No temp contains the path from root to current node, i.e. the set of nodes visited while walking down the tree to arrive at the current node. If you have a parent pointer in each node (but I guess not), you can follow those pointers and walk up the tree to traverse the same set of nodes as temp . But this uses more memory. create ancestor matrix for binary tree

Transcript of Algorithm - Create Ancestor Matrix for Binary Tree - Stack Overflow

Page 1: Algorithm - Create Ancestor Matrix for Binary Tree - Stack Overflow

10/21/12algorithm - create ancestor matrix for binary tree - Stack Overflow

1/2stackoverflow.com/questions/12664257/create-ancestor-matrix-for-binary-tree

jrouquie

1,862 4 12

Sree Ram

36 1 5

1 Answer

1

/ \

2 3

/ \ / \

4 5 6 7

for the given binary tree we need to create a matrix a[7][7] satisfying the ancestor property like a[2][1]=1

since 1 is an ancestor of 2 ....

i solved it by using extra space an array ...the solution i came up is

int a[n][n]={0};

void updatematrix(int a[][n],struct node *root,int temp[],int index){

if(root == NULL)

return ;

int i;

for(i=0;i< index;i++)

a[root->data][temp[i]]=1;

temp[index]=root->data;

updatematrix(a,root->left,temp,index+1);

updatematrix(a,root->right,temp,index+1);

}

is there any mistake in my solution ? can we do this inplace ???(i mean without using the temp array )

algorithm tree

edited Sep 30 at 20:01 asked Sep 30 at 19:20

80% accept rate

Perhaps you should do something about your accept rate. – Ted Hopp Sep 30 at 19:23

@TedHopp i am new to this site ..can u pls say how to increase the accept rate ..? – Sree Ram Sep 30 at

19:25

I guess that in your code you need to replace the first occurence of arr by a, and replace the second

occurrence of arr by temp. – jrouquie Sep 30 at 19:50

@jrouquie yup ..thank you ..edited .. – Sree Ram Sep 30 at 19:53

Go to some of the questions that you've asked (click on your name to see your profile, which includes links to

all your questions). For any question that has answers, if one of the answers resolves your problem, click on the

check mark next to the answer. This has several benefits: it lets others know that the question has been

answered; it also lets others know which answer worked; it gives +15 reputation bonus to the person who

provided the answer; it gives you +2 reputation bonus. – Ted Hopp Sep 30 at 19:59

Was this post useful to you? Yes No

temp contains the path from root to current node, i.e. the set of nodes visited while walking down the

tree to arrive at the current node.

If you have a parent pointer in each node (but I guess not), you can follow those pointers and walk up

the tree to traverse the same set of nodes as temp. But this uses more memory.

create ancestor matrix for binary tree

Page 2: Algorithm - Create Ancestor Matrix for Binary Tree - Stack Overflow

10/21/12algorithm - create ancestor matrix for binary tree - Stack Overflow

2/2stackoverflow.com/questions/12664257/create-ancestor-matrix-for-binary-tree

jrouquie

1,862 4 12

You can also walk down the tree several times (pseudocode):

def updatematrix(a,node):

if node is null: return

walkDown(a.data,node.left)

walkDown(a.data,node.right)

updatematrix(a,node.left)

updatematrix(a,node.right)

def walkDown(data,node):

if node is null: return

a[node][data] = 1

walkDown(data,node.left)

walkDown(data,node.right)

Same complexity, but the pattern of memory access looks less cache friendly.

answered Oct 1 at 6:49

feedback

Not the answer you're looking for? Browse other questions tagged algorithm tree or

ask your own question.