Brief
- Print an ASCII representation of a binary search tree.
- You should write your own minimum implementation logic of a BST (node, left, right, insert)
(50,30,70,20,80,40,75,90) gives: __50__70__80__90 | | | |__75 | |__30__40 | |__20
Detailed requirements
- Items are inserted into the tree in the order in which they are in the list
- All node values are right aligned in columns.
- Right nodes are joined using
__
- Right nodes are represented along the horizontal and appear directly to the right of the parent node
- Right nodes are separated by a minimum of 2 underscores (__)
- Left nodes are joined using
|
and|__
on the next line - Left nodes are represented along the vertical and appear under and to the right of the parent (ie, next column).
- Left nodes are separated by a a minimum of a blank line (discounting pipes (|) linking child to parent)
- The vertical pipes joining 2 left nodes should be in the column directly under the last character of the parent node (see samples)
- Column width is determined using something like
max(col_values[]).toString().length() + 2
- Branching left always adds a new row to the tree diagram
- 2 separate (left) branches never appear on the same line (right__right__right is OK)
- The root node may optionally be prefixed with
__
(Removing it just about doubled my test code!) - Input is a set of positive and/or negative integers
- You do not need to deal with duplicate values (drop/ignore them)
- I'm not placing many restrictions on trailing whitespaces:
- You may have trailing whitespaces after the last node on a line
- You may have trailing whitespace after a
|
on the empty lines - No whitespace between nodes when when branching right
- Standard loopholes and T&Cs apply
- This is code-golf. Shortest code in bytes wins!
I'm aware that this is a potential duplicate, but I think I've been specific enough in my requirements that it should pose new challenges.
Sample Input/Output
Input
(50,40,30,20,10)
Output
__50 | |__40 | |__30 | |__20 | |__10
Input
(10,20,30,40,50)
Output
__10__20__30__40__50
Input
(50,700,30,800,60,4,5,10,20,2,500,850,900,950,870,35,1,-10)
Output
__50__700__800__850__900__950 | | | | | |__870 | | | |___60__500 | |___30___35 | |____4____5___10___20 | |____2 | |____1 | |__-10