#P1185. 绘制二叉树
绘制二叉树
Description
A binary tree is a basic data structure. It is either empty, or consists of a root node, a left subtree, and a right subtree. The left and right subtrees are also binary trees.
When a binary tree has height , it has levels. If, in a binary tree, every level except level has the maximum possible number of nodes and all leaf nodes are on level , then it is a full binary tree.
Now you are required to write a program to draw a binary tree that is obtained by removing some nodes from a full binary tree. For a full binary tree, draw it according to the following rules:
-
Nodes are represented by the lowercase letter
o. For a parent node, use/to connect to its left child and\to connect to its right child. -
Define as the character located at row and column . If is
/, then and must be eitheroor/. If is\, then and must be eitheroor\. Likewise, if is a nodeoon levels , then is/and is\. -
For the nodes on level (the leaves): if two leaves share the same parent, they are separated by 3 spaces; if two leaves are adjacent but do not share the same parent, they are separated by 1 space. There is no space before the first (leftmost) node on level .
Finally, after drawing a full binary tree, delete nodes from it (each deletion removes that node, its left and right subtrees, and its connection to its parent). Replace all removed characters with spaces (the space is ASCII 32; outputting ASCII 0 will be judged as a wrong answer).
Input Format
The first line contains two positive integers and , the number of levels of the binary tree to draw and the number of nodes to delete.
The next lines each contain two positive integers, indicating deletion of the -th node on level .
Output Format
Output the binary tree drawn according to the requirements.
2 0
o
/ \
o o
4 0
o
/ \
/ \
/ \
/ \
/ \
o o
/ \ / \
/ \ / \
o o o o
/ \ / \ / \ / \
o o o o o o o o
4 3
3 2
4 1
3 4
o
/ \
/ \
/ \
/ \
/ \
o o
/ /
/ /
o o
\ / \
o o o
Hint
30% of the testdata satisfy: .
50% of the testdata satisfy: .
100% of the testdata satisfy: .
Translated by ChatGPT 5
京公网安备 11011102002149号