6

Given multiple tuples in the form of (A,B) where A is the parent and B is the child in a binary tree, find if the input is valid or not. 4 error conditions were provided:

  1. If a parent has more than 2 children,
  2. If duplicate tuples entered,
  3. If the tree has a cycle,
  4. If more than one root possible.

For violation of multiple validity conditions, print the condition coming first in the above order. If the input is valid, print the tree in a serial representation. For eg: If input is (A,B), (B,C), (A,D), (C,E) , output: (A(B(C(E)))(D))

I am thinking of solving it through union-find data structure but not able to code it. Can anyone help me with the logic or pseudo code in c/c++

dingalapadum
  • 2,077
  • 2
  • 21
  • 31
Sushil
  • 163
  • 1
  • 1
  • 6
  • How exactly would you use the union-find structure on this problem? I don't think it fits very well... then again, I might be missing something. Can you elaborate a bit on what you've thought so far? This looks like homework, so if you expect any help I suggest you show a bit more of what you've tried so far. – dingalapadum Sep 01 '15 at 18:47
  • I am thinking using union find data structure where i will store the value of parent node in child using an array, lets say this array as parent[].For mentioned error conditions : 1) if in parent array parent node index comes more than 2 times, then clearly parent node try to have more than 2 child. Hence error 1. 2) if in parent array, if input child node index is already pre – Sushil Sep 02 '15 at 04:02
  • Ok. First I suggest you update your question and you put your ideas in there instead of in a comment. You can't expect people to read the comments. They mainly read your question. Next, to the union-find: that data-structure supports 2 operations efficiently: FIND in which set an element is and make a UNION of two disjoint sets. I don't see how you are using those 2 operations. Now, you have ideas with arrays and stuff: please write the code of the structs you have in mind in your post. This is way more precise than some sentences in a comment which are hard to interpret... – dingalapadum Sep 02 '15 at 07:43
  • @Amor I think it is easy, what did u try? – Sumeet Sep 04 '15 at 08:34
  • @dingalapadum What do you think of my answer. – Sumeet Sep 04 '15 at 14:52
  • @Amor feel free for any queries. – Sumeet Sep 04 '15 at 14:52
  • @Dante. Thanks for your valuable comments, i have solve above problem using DFS and Union-Find data structure. – Sushil Sep 13 '15 at 13:58
  • Thanks all for your valuable comments. I have solved the above problem using DFS and Union-Find data structure – Sushil Sep 13 '15 at 13:59

0 Answers0