#P8603. Horizontal Print of a Binary Search Tree
Horizontal Print of a Binary Search Tree
Horizontal Print of a Binary Search Tree
Given a sequence of integers, build a binary search tree (BST) by inserting the numbers in the order they appear. The insertion follows these rules:
- If the new number is less than the current node, proceed to the left subtree.
- If the new number is greater than or equal to the current node, proceed to the right subtree.
- If a subtree is empty, insert the new node there.
Once the BST is built, print the tree horizontally using a reverse in-order traversal. In this printing method, for each node, first recursively print the right subtree, then print the current node on a new line with an indentation proportional to its depth (4 spaces per level), and finally recursively print the left subtree.
For example, with the insertion order 10 8 5 7 12 4
, the BST will be structured as illustrated in Diagram 1 (not shown).
Your program should read the input, construct the BST, and output its horizontal representation.
inputFormat
The input is a single line containing space-separated integers. These integers are to be inserted into the BST in the given order.
outputFormat
Output the horizontal print of the BST. For each node, print the node's value on a new line. The amount of indentation (4 spaces per level) represents the depth of the node in the tree. The tree is printed using a reverse in-order traversal (right subtree, node, then left subtree).
sample
10 8 5 7 12 4
12
10
8
7
5
4
</p>