1. 程式人生 > 其它 >計算機網路2.0物理層

計算機網路2.0物理層

二叉搜尋樹的操作集

本題要求實現給定二叉搜尋樹的5種常用操作

函式介面定義

BinTree Insert( BinTree BST, ElementType X );
BinTree Delete( BinTree BST, ElementType X );
Position Find( BinTree BST, ElementType X );
Position FindMin( BinTree BST );
Position FindMax( BinTree BST );

其中BinTree結構定義如下:

typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};
  • 函式InsertX插入二叉搜尋樹BST並返回結果樹的根結點指標;
  • 函式DeleteX從二叉搜尋樹BST中刪除,並返回結果樹的根結點指標;如果X不在樹中,則列印一行Not Found並返回原樹的根結點指標;
  • 函式Find在二叉搜尋樹BST中找到X,返回該結點的指標;如果找不到則返回空指標;
  • 函式FindMin返回二叉搜尋樹BST中最小元結點的指標;
  • 函式FindMax返回二叉搜尋樹BST中最大元結點的指標。

裁判測試程式樣例

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

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

void PreorderTraversal( BinTree BT ); /* 先序遍歷,由裁判實現,細節不表 */
void InorderTraversal( BinTree BT );  /* 中序遍歷,由裁判實現,細節不表 */

BinTree Insert( BinTree BST, ElementType X );
BinTree Delete( BinTree BST, ElementType X );
Position Find( BinTree BST, ElementType X );
Position FindMin( BinTree BST );
Position FindMax( BinTree BST );

int main()
{
    BinTree BST, MinP, MaxP, Tmp;
    ElementType X;
    int N, i;

    BST = NULL;
    scanf("%d", &N);
    for ( i=0; i<N; i++ ) {
        scanf("%d", &X);
        BST = Insert(BST, X);
    }
    printf("Preorder:"); PreorderTraversal(BST); printf("\n");
    MinP = FindMin(BST);
    MaxP = FindMax(BST);
    scanf("%d", &N);
    for( i=0; i<N; i++ ) {
        scanf("%d", &X);
        Tmp = Find(BST, X);
        if (Tmp == NULL) printf("%d is not found\n", X);
        else {
            printf("%d is found\n", Tmp->Data);
            if (Tmp==MinP) printf("%d is the smallest key\n", Tmp->Data);
            if (Tmp==MaxP) printf("%d is the largest key\n", Tmp->Data);
        }
    }
    scanf("%d", &N);
    for( i=0; i<N; i++ ) {
        scanf("%d", &X);
        BST = Delete(BST, X);
    }
    printf("Inorder:"); InorderTraversal(BST); printf("\n");

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

程式碼

BinTree Insert(BinTree BST, ElementType X) {
    if (BST == NULL) { /* 如果為空樹,在遞迴到空樹時會開闢空間給節點然後將地址傳回去 */
        BST = (BinTree) malloc(sizeof(struct TNode)); /* 為BST開闢空間 */
        BST->Data = X; /* 將元素賦給結點 */
        BST->Left = BST->Right = NULL; /* 初始化左右子樹都為NULL */
        return BST;
    } else if (X < BST->Data) /* 結點應該插入在BST的左子樹 */
        BST->Left = Insert(BST->Left, X); /* 遞迴插入 */
    else if (X > BST->Data) /* 結點應該插入在BST的右子樹 */
        BST->Right = Insert(BST->Right, X);
    return BST; /* 返回根節點 */
}

Position Find(BinTree BST, ElementType X) {
    if (BST == NULL) /* 如果查詢樹為空,或者最後遍歷完為NULL,直接返回NULL */
        return NULL;
    if (X < BST->Data) /* X在BST的左子樹上 */
        return Find(BST->Left, X); /* 遞迴查詢左子樹 */
    else if (X > BST->Data) /* X在BST的右子樹上 */
        return Find(BST->Right, X);
    else
        return BST; /* 找到了X,返回X的位置 */
}


Position FindMax(BinTree BST) {
    if (BST != NULL) /* BST不為空 */
        while (BST->Right) /* 找到BST最右邊的結點 */
            BST = BST->Right;
    return BST; /* BST為空就返回NULL,不為空就返回最右邊的結點的位置 */
}

Position FindMin(BinTree BST) {
    if (BST != NULL) /* BST不為空 */
        while (BST->Left) /* 找到BST最左邊的結點 */
            BST = BST->Left;
    return BST; /* BST為空就返回NULL,不為空就返回左邊的結點 */
}

BinTree Delete(BinTree BST, ElementType X) {
    Position TmpCell;
    if (BST == NULL) { /* BST為空,則找不到刪除元素X */
        printf("Not Found\n");
        return BST; /* 返回NULL */
    } else if (X < BST->Data) /* 如果X在BST的左子樹上 */
        BST->Left = Delete(BST->Left, X); /* 遞迴刪除X在BST的左子樹上 */
    else if (X > BST->Data) /* 如果X在BST的右子樹上 */
        BST->Right = Delete(BST->Right, X); /* 遞迴刪除X在BST的右子樹上 */
    else if (BST->Left && BST->Right) { /* 找到了X,X有左子樹和右子樹 */
        TmpCell = FindMin(BST->Right); /* 將BST右子樹的最小位置返回 */
        BST->Data = TmpCell->Data; /* 用最小位置的資料覆蓋BST的 */
        BST->Right = Delete(BST->Right, BST->Data); /* 遞迴在右子樹上刪除最小的結點 */

    } else { /* BST只有一個左子樹或者右子樹,或者沒有子樹 */
        TmpCell = BST; /* 用TmpCell來記錄BST的位置 */
        if (BST->Left == NULL) /* 如果沒有左子樹 */
            BST = BST->Right; /* 則將BST更新為BST的右子樹 */
        else if (BST->Right == NULL) /* 如果沒有右子樹 */
            BST = BST->Left; /* 將BST更新為BST的左子樹 */
        free(TmpCell); /* 最後釋放TmpCell記錄的BST位置的空間 */
    }
    return BST;
}