Vous êtes sur la page 1sur 2

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

create ancestor matrix for binary tree


1 / \ 2 3 /\ /\ 4 56 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 itan[]{} n []n=0; vi udtmti(n a]n,tutnd *otittm[,n idx{ od paearxit [[]src oe ro,n ep]it ne) i(ot= NL) fro = UL rtr ; eun iti n ; fri0i idxi+ o(=;< ne;+) aro-dt]tm[]=; [ot>aa[epi]1 tm[ne]ro-dt; epidx=ot>aa udtmti(,ot>ettm,ne+) paearxaro-lf,epidx1; udtmti(,ot>ih,epidx1; paearxaro-rgttm,ne+) } 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 jrouquie 1,862 4 12

asked Sep 30 at 19:20 Sree Ram 36 1 5 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 a r by a, and replace the second r occurrence of a r by t m jrouquie Sep 30 at 19:50 r e p. @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

1 Answer
t m contains the path from root to current node, i.e. the set of nodes visited while walking down the ep 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 t m But this uses more memory. e p.
stackoverflow.com/questions/12664257/create-ancestor-matrix-for-binary-tree 1/2

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

You can also walk down the tree several times (pseudocode): dfudtmti(,oe: e paearxand) i nd i nl:rtr f oe s ul eun wlDw(.aand.et akonadt,oelf) wlDw(.aand.ih) akonadt,oergt udtmti(,oelf) paearxand.et udtmti(,oergt paearxand.ih) dfwlDw(aand) e akondt,oe: i nd i nl:rtr f oe s ul eun and]dt]=1 [oe[aa wlDw(aand.et akondt,oelf) wlDw(aand.ih) akondt,oergt Same complexity, but the pattern of memory access looks less cache friendly.
answered Oct 1 at 6:49 jrouquie 1,862 4 12 feedback

Not the answer you're looking for? Browse other questions tagged algorithm tree or ask your own question.

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

2/2

Vous aimerez peut-être aussi