-->
当前位置:首页 > 题库

PROGRAMMING:Binary tree node statistics and deletion

Luz5年前 (2021-05-10)题库473
To create a binary tree, the node stores integer data. The rule is: the first value is the root of the binary tree, the value less than the parent node is placed in the left child node, and the value greater than the parent node is placed in the right child node. On the basis of creating a good binary tree, node statistics and deletion.
The content of statistics includes: the number of nodes; The depth of the tree; The number of points with degree 0; The number of points with degree 1; The number of points with degree 2.
Delete strategy: if the deleted node is a leaf node, delete the node directly; If there is only one subtree in the deleted node, the root node of the subtree directly replaces the node; If the deleted node has both left and right subtrees, the minimum point in the right subtree is moved to the position of the deleted point to replace the deleted node.
###Input format:
The input value is integer type, and it can be input in multiple lines. The first line: the number of nodes n of binary tree (1 ≤ n ≤ 30). Second line: values of nodes, separated by commas. The third line: the node value to be deleted.
###Output format:
Multi line output. The number of output nodes in the first line. The second line outputs the depth of the tree. The number of nodes with degree 0 in line 3. The fourth line outputs the number of nodes with degree 1. The fifth line outputs the number of nodes with degree 2. The sixth line is the middle order traversal result before deletion. The seventh line is the breadth first traversal result before deletion. The eighth line is the result of middle order traversal after deletion. The ninth line is the breadth first traversal result after deletion. Please refer to the input and output sample for specific format and requirements.
###Input example:
Here is a set of inputs. For example:
```in
five
7,2,5,9,12
seven
```
###Output example:
The corresponding output is given here. For example:
```out
NodeN umbers:5
TreeD epth:3
numbers_ 0:2
numbers_ 1:2
numbers_ 2:1
InO rder:2 ,5,7,9,12
LayerO rder:7 ,2,9,5,12
InOrder after:2 ,5,9,12
LayerOrder after:9 ,2,12,5
```
###Input sample 2:
Here is a set of inputs. For example:
```in
one
three
three
```
###Output sample 2:
The corresponding output is given here. For example:
```out
NodeN umbers:1
TreeD epth:1
numbers_ 0:1
numbers_ 1:0
numbers_ 2:0
InO rder:3
LayerO rder:3
InOrder after:null
LayerOrder after:null
```







answer:If there is no answer, please comment