You should write a program or function which receives a string as input and outputs or returns if the input is an ASCII tree.
_ \/ / \_/ | |
ASCII trees consist of characters
/ \ | _ spaces and
The non-whitespace characters connect two edge points of their cells by a line segment:
/connects the bottom left and top right corners
\connects the bottom right and top left corners
|connects the middle points of the bottom edge and top edge
_connects the bottom left and bottom right corners and the middle point of the bottom edge
(Note that this means that
| can only connect with
_ but not with
An ASCII picture is called a tree if the following rules apply:
- Exactly one point (the root) of exactly one character touches to the bottom edge of the last row.
You can reach any point of any line segment by:
- starting from the root
- using only the line segments
- never going into a downward direction (not even sideways downward)
- A string consisting of the characters
/ \ | _ spaceand
newlinecontaining at least one non-whitespace character.
You can choose of two input format:
- No unnecessary whitespace around the tree (as seen in the examples).
- No unnecessary whitespace around the tree (as seen in the examples) except spaces on the right side of the rows to make all rows the same length.
Trailing newline is optional.
- A consistent truthy value if the input is an ascii tree.
- A consistent falsy value if the input isn't an ascii tree.
_ \/ / \_/ | |
/ / \/ \ \____/ \/ / /
\___/ / \ \___/ | |
__/ _/ /
____ \ ___ \ \/ \/\_____/ \/ \/ \__/ | |
Invalid trees (with extra explanations which are not parts of the inputs):
\/ \_______/ \__ / | \_/ <- reachable only on with downward route |
_ <- multiple roots
\/ <- multiple root characters
/\ <- multiple roots
| <- unreachable part |
__/ / <- unreachable parts |
\____/ | | <- multiple roots
_\__/ <- unreachable parts (_ and \ don't connect to each other) |
This is code-golf so the shortest entry wins.