Inspired by a Codingame challenge I tried (and failed at) about a month ago.
Given a binary tree of words, say:
HELLO / \ WORLD EATING / / \ ARCH IDAHO GUARD / DOOZY / ZEPPELIN / \ POWER LIGHT
- Take the root of the tree.
- Print it in the South direction.
H E L L O
- For each of the tree's children, given iteration index i:
- Find the first character in the child that it has in common with the root. If there are multiple occurrences of that character in the root, choose the first occurrence as the join point.
- The drawn root word will have to intersect the child at this point.
- Draw the left node down to the left, and the right node down to the right.
H E LA LWT O I R N L G D
- Now, for each of the diagonal words' children, do the same, except always print downward.
H E LA LWT AO I R DN LC A G D H H U O A R D
- Go back to step3 and perform the steps again until the tree is complete.
H E LA LWT AO I R DN LC A G D H H U O A R D O O Z YE P OP W E E L R II N G H T
- No two child words will have the same first common letter.
- Words from different subtrees will never intersect.
- Input will always be a valid binary tree.
- Each parent will always have a common character with both of its children.
- Trees can be taken in any suitable and reasonable format for your language.
- Leading and trailing whitespace is allowed, so long as the structure of the lightning is as shown.
- All words will be given in the same case.
PROGRAMMING | CHALLENGES | \ PUZZLES CODING
P R PO UG C ZRHO ZA D LM I LEM N E SI G N N G G E S
DIGERIDOO | \ GIRDLE ORNATE | | \ EXQUISITE ROLL TALONS | \ QUEEN TAPESTRY | \ PASTE YELLOW | ERROR
D I G IE R R D I L D E O X OR Q ON UU L A E I L T E S AE N I L T O EA N P P S EA S TT E R R Y R E O L R L O W