1. 程式人生 > 其它 >瀋陽師範大學大二上資料結構--樹和廣義表

瀋陽師範大學大二上資料結構--樹和廣義表

技術標籤:瀋陽師範大學PTA資料結構

6-1 求二叉樹高度 (10分)
本題要求給定二叉樹的高度。

函式介面定義:

int GetHeight( BinTree BT );

其中BinTree結構定義如下:

typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

要求函式返回給定二叉樹BT的高度值。

裁判測試程式樣例:

#include <stdio.h>
#include
<stdlib.h>
typedef char ElementType; typedef struct TNode *Position; typedef Position BinTree; struct TNode{ ElementType Data; BinTree Left; BinTree Right; }; BinTree CreatBinTree(); /* 實現細節忽略 */ int GetHeight( BinTree BT ); int main() { BinTree BT = CreatBinTree(); printf
("%d\n", GetHeight(BT)); return 0; } /* 你的程式碼將被嵌在這裡 */

輸出樣例(對於圖中給出的樹):
在這裡插入圖片描述

4
int GetHeight( BinTree BT ){
    int LeftHeight=0;
    int RightHeight=0;
    if(BT==NULL){
        return 0;
    }
    LeftHeight=GetHeight(BT->Left);
    RightHeight=GetHeight(BT->Right);
    return (LeftHeight>
RightHeight?LeftHeight:RightHeight)+1; }

6-2 二叉樹的遍歷 (10分)
本題要求給定二叉樹的4種遍歷。

函式介面定義:

void InorderTraversal( BinTree BT );
void PreorderTraversal( BinTree BT );
void PostorderTraversal( BinTree BT );
void LevelorderTraversal( BinTree BT );

其中BinTree結構定義如下:

typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

要求4個函式分別按照訪問順序打印出結點的內容,格式為一個空格跟著一個字元。

裁判測試程式樣例:

#include <stdio.h>
#include <stdlib.h>

typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

BinTree CreatBinTree(); /* 實現細節忽略 */
void InorderTraversal( BinTree BT );
void PreorderTraversal( BinTree BT );
void PostorderTraversal( BinTree BT );
void LevelorderTraversal( BinTree BT );

int main()
{
    BinTree BT = CreatBinTree();
    printf("Inorder:");    InorderTraversal(BT);    printf("\n");
    printf("Preorder:");   PreorderTraversal(BT);   printf("\n");
    printf("Postorder:");  PostorderTraversal(BT);  printf("\n");
    printf("Levelorder:"); LevelorderTraversal(BT); printf("\n");
    return 0;
}
/* 你的程式碼將被嵌在這裡 */

輸出樣例(對於圖中給出的樹):

在這裡插入圖片描述

Inorder: D B E F A G H C I
Preorder: A B D F E C G H I
Postorder: D E F B H G I C A
Levelorder: A B C D F G I E H
void InorderTraversal( BinTree BT ){
    if(BT== NULL){
        return;
    }
    InorderTraversal(BT->Left);
    printf(" %c",BT->Data);
    InorderTraversal(BT->Right);
}
void PreorderTraversal( BinTree BT ){
    if(BT== NULL){
        return;
    }
    printf(" %c",BT->Data);
    PreorderTraversal(BT->Left);
    PreorderTraversal(BT->Right);
}
void PostorderTraversal( BinTree BT ){
    if(BT== NULL){
        return;
    }
    PostorderTraversal(BT->Left);
    PostorderTraversal(BT->Right);
    printf(" %c",BT->Data);
}
void LevelorderTraversal( BinTree BT ){
    BinTree TreePosition[1000];
    int SaveIndex=0;
    int PrintIndex=0;
    TreePosition[SaveIndex]=BT;
    SaveIndex++;
    while (SaveIndex>PrintIndex){
        if(TreePosition[PrintIndex]!=NULL){
            printf(" %c",TreePosition[PrintIndex]->Data);
            TreePosition[SaveIndex]=TreePosition[PrintIndex]->Left;
            SaveIndex++;
            TreePosition[SaveIndex]=TreePosition[PrintIndex]->Right;
            SaveIndex++;
        }
        PrintIndex++;
    }
}

6-3 先序輸出葉結點 (10分)
本題要求按照先序遍歷的順序輸出給定二叉樹的葉結點。

函式介面定義:

void PreorderPrintLeaves( BinTree BT );

其中BinTree結構定義如下:

typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

函式PreorderPrintLeaves應按照先序遍歷的順序輸出給定二叉樹BT的葉結點,格式為一個空格跟著一個字元。

裁判測試程式樣例:

#include <stdio.h>
#include <stdlib.h>

typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

BinTree CreatBinTree(); /* 實現細節忽略 */
void PreorderPrintLeaves( BinTree BT );

int main()
{
    BinTree BT = CreatBinTree();
    printf("Leaf nodes are:");
    PreorderPrintLeaves(BT);
    printf("\n");

    return 0;
}
/* 你的程式碼將被嵌在這裡 */

輸出樣例(對於圖中給出的樹):

在這裡插入圖片描述

Leaf nodes are: D E H I

作者
陳越
單位
浙江大學
程式碼長度限制
16 KB
時間限制
400 ms
記憶體限制
64 MB

void PreorderPrintLeaves( BinTree BT ){
    if(BT==NULL){
        return;
    }
    if(BT->Right==NULL&&BT->Left==NULL){
        printf(" %c",BT->Data);
    }
    PreorderPrintLeaves(BT->Left);
    PreorderPrintLeaves(BT->Right);
}

6-4 統計二叉樹結點個數 (10分)
本題要求實現一個函式,可統計二叉樹的結點個數。

函式介面定義:

int NodeCount ( BiTree T);

T是二叉樹樹根指標,函式NodeCount返回二叉樹中結點個數,若樹為空,返回0。

裁判測試程式樣例:

#include <stdio.h>
#include <stdlib.h>

typedef char ElemType;
typedef struct BiTNode
{
    ElemType data;
    struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

BiTree Create();/* 細節在此不表 */

int NodeCount ( BiTree T);

int main()
{
    BiTree T = Create();

    printf("%d\n", NodeCount(T));
    return 0;
}

/* 你的程式碼將被嵌在這裡 */
輸出樣例(對於圖中給出的樹):
二叉樹.png
在這裡插入圖片描述

6

作者
DS課程組
單位
臨沂大學
程式碼長度限制
16 KB
時間限制
400 ms
記憶體限制
64 MB

int NodeCount ( BiTree T){
    int Sum=0;
    if(T==NULL){
        return 0;
    }
    Sum++;
    Sum+=NodeCount(T->lchild);
    Sum+=NodeCount(T->rchild);
    return Sum;
}

6-5 統計二叉樹葉子結點個數 (10分)
本題要求實現一個函式,可統計二叉樹的葉子結點個數。

函式介面定義:

int LeafCount ( BiTree T);

T是二叉樹樹根指標,函式LeafCount返回二叉樹中葉子結點個數,若樹為空,則返回0。

裁判測試程式樣例:

#include <stdio.h>
#include <stdlib.h>

typedef char ElemType;
typedef struct BiTNode
{
    ElemType data;
    struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

BiTree Create();/* 細節在此不表 */

int LeafCount ( BiTree T);

int main()
{
    BiTree T = Create();

    printf("%d\n", LeafCount(T));
    return 0;
}

/* 你的程式碼將被嵌在這裡 */
輸出樣例(對於圖中給出的樹):
二叉樹.png
在這裡插入圖片描述

3

作者
DS課程組
單位
臨沂大學
程式碼長度限制
16 KB
時間限制
400 ms
記憶體限制
64 MB

int LeafCount ( BiTree T){
    int RootNum=0;
    if(T==NULL){
        return 0;
    }
    if(T->rchild==NULL&&T->lchild==NULL){
        RootNum++;
    }
    RootNum+=LeafCount(T->lchild);
    RootNum+=LeafCount(T->rchild);
    return RootNum;
    
}