樹的建立與基本操作

來源:互聯網
上載者:User

 樹的建立與基本操作(10分)
成績: 10 / 折扣: 0.8
在本實驗中,程式的輸入是一個表示樹結構的廣義表。假設樹的根為 root ,其子樹森林 F = ( T1 , T2 , … , Tn ),設與該樹對應的廣義表為 L ,則 L =(原子,子表 1 ,子表 2 , … ,子表 n ),其中原子對應 root ,子表 i ( 1<i<=n )對應 Ti 。例如:廣義表 (a,(b,(c),(d)),(f,(g),(h ),(i))) 表示的樹:

程式的輸出為樹的階層、樹的度以及各種度的結點個數。
在輸出樹的階層時,先輸出根結點,然後依次輸出各個子樹,每個子樹向裡縮排 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

 

#include <stdio.h>#include <stdlib.h>#include <string.h>struct treenode{        char data;        int parents;        int degree;};int CreateTree(char in[100],struct treenode tree[100],int list[100]){        int i,j,k=1,len,kuo=0;        len = strlen(in);        tree[0].data = in[1];        tree[0].parents = -1;        list[0]=1;        for (i = 0;i < len;i++)        {                tree[i].data = in[i];                tree[i].parents = -1;                tree[i].degree = 0;        }        for (i = 2;i < len-1;i++)        {                if (in[i] == ',' || in[i] == '(' || in[i] == ')')                {                        continue;                }                else                {                        for (j = i , kuo = 0;j >= 0 && kuo != 2;j--)                        {                                if (in[j] == '(')                                {                                        kuo++;                                        if (kuo == 2)                                        {                                                break;                                        }                                }                                else if (in[j] == ')')                                {                                        kuo--;                                }                        }                        tree[i].data = in[i];                        tree[i].parents = j+1;                        tree[j+1].degree++;                        list[k++] = i;                }        }        return k;}PrintTree(struct treenode tree[100],int list[100],int i){        int space = 0 , j = i;        if (i == 0)        {                printf("%c\n",tree[list[i]].data);                return;        }        i=list[i];        while (tree[i].parents != -1)        {                space+=4;                i=tree[i].parents;        }        for (i=0;i<space;i++)        {                printf(" ");        }        printf("%c\n",tree[list[j]].data);}void main(){        int i,j,k,n,max,degree[100],list[100];        char in[100];        struct treenode tree[100];        gets(in);        n = CreateTree(in,tree,list);        for (i=0,max=0;i<n;i++)        {                PrintTree(tree,list,i);                if (tree[list[i]].degree > max)                {                        max = tree[list[i]].degree;                }        }        for (i = 0;i <= max;i++)        {                degree[i] = 0;        }        for (i = 0;i <n;i++)        {                degree[tree[list[i]].degree]++;        }        printf("Degree of tree: %d\n",max);        for (i = 0;i<=max;i++)        {                printf("Number of nodes of degree %d: %d\n",i,degree[i]);        }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.