(数据结构)树的建立与基本操作

(数据结构)树的建立与基本操作:

程序的输入是一个表示树结构的广义表。假设树的根为 root ,其子树森林 F = ( T1 , T2 , … , Tn ),设与该树对应的广义表为 L ,则 L =(原子,子表 1 ,子表 2 , … ,子表 n ),其中原子对应 root ,子表 i ( 1 (数据结构)树的建立与基本操作_第1张图片
程序的输出为树的层次结构、树的度以及各种度的结点个数。
在输出树的层次结构时,先输出根结点,然后依次输出各个子树,每个子树向里缩进 4 个空格,如:针对上图表示的树,输出的内容应为:
a
b
c
d
f
g
h
i
Degree of tree: 3
Number of nodes of degree 0: 5
Number of nodes of degree 1: 0
Number of nodes of degree 2: 2
Number of nodes of degree 3: 1
例: (下面的黑体为输入)
(a,(b),(c,(d),(e,(g),(h )),(f)))
a
b
c
d
e
g
h
f
Degree of tree: 3
Number of nodes of degree 0: 5
Number of nodes of degree 1: 0
Number of nodes of degree 2: 2
Number of nodes of degree 3: 1

测试用例1:

测试输入:

(a,(b),(c,(d),(e,(g),(h)),(f)))↵

期待输出:

a↵
b↵
c↵
d↵
e↵
g↵
h↵
f↵
Degree of tree: 3↵
Number of nodes of degree 0: 5↵
Number of nodes of degree 1: 0↵
Number of nodes of degree 2: 2↵
Number of nodes of degree 3: 1↵

测试用例2:

测试输入:

(a,(b,(c,(d),(e)),(f)),(g,(h),(i)),(j,(k,(m),(n),(o),(p,®))))↵

期待输出:

a↵
b↵
c↵
d↵
e↵
f↵
g↵
h↵
i↵
j↵
k↵
m↵
n↵
o↵
p↵
r↵
Degree of tree: 4↵
Number of nodes of degree 0: 9↵
Number of nodes of degree 1: 2↵
Number of nodes of degree 2: 3↵
Number of nodes of degree 3: 1↵
Number of nodes of degree 4: 1↵

测试用例3:

测试输入:

(a,(b),©,(d,(m),(n)),(e,(o)),(f),(h))↵

期待输出:

a↵
b↵
c↵
d↵
m↵
n↵
e↵
o↵
f↵
h↵
Degree of tree: 6↵
Number of nodes of degree 0: 7↵
Number of nodes of degree 1: 1↵
Number of nodes of degree 2: 1↵
Number of nodes of degree 3: 0↵
Number of nodes of degree 4: 0↵
Number of nodes of degree 5: 0↵
Number of nodes of degree 6: 1↵

代码如下:

#include    
#include    
int main()    
{
       
    

你可能感兴趣的:(数据结构,数据结构,算法,程序设计)