1. 程式人生 > >04-樹7 二叉搜索樹的操作集(30 分)

04-樹7 二叉搜索樹的操作集(30 分)

pty clean class 結構 其中 stc stack AI findmi

本題要求實現給定二叉搜索樹的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;
}
/* 你的代碼將被嵌在這裏 */

輸入樣例:

10
5 8 6 2 4 1 0 10 9 7
5
6 3 10 0 5
5
5 7 0 10 3

輸出樣例:

Preorder: 5 2 1 0 4 8 6 7 10 9
6 is found
3 is not found
10 is found
10 is the largest key
0 is found
0 is the smallest key
5 is found
Not Found
Inorder: 1 2 4 6 8 9

main.c:
技術分享圖片
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 #include "
03_01_PTA.h" 5 #include "stack.h" 6 7 int main() 8 { 9 BinTree BST, MinP, MaxP, Tmp; 10 ElementType X; 11 int N, i; 12 13 BST = NULL; 14 scanf("%d", &N); 15 for(i=0;i<N;i++) { 16 scanf("%d", &X); 17 BST = Insert(BST, X); 18 } 19 printf("Preorder:"); PreorderTraversal(BST); printf("\n"); 20 MinP = FindMin(BST); 21 MaxP = FindMax(BST); 22 scanf("%d", &N); 23 for(i=0;i<N;i++) { 24 scanf("%d", &X); 25 Tmp = Find(BST, X); 26 if(Tmp == NULL) printf("%d is not found\n", X); 27 else { 28 printf("%d is found\n", Tmp->Data); 29 if(Tmp == MinP) printf("%d is the smallest key\n", Tmp->Data); 30 if(Tmp == MaxP) printf("%d is the largest key\n", Tmp->Data); 31 } 32 } 33 scanf("%d", &N); 34 for(i=0;i<N;i++) { 35 scanf("%d", &X); 36 BST = Delete(BST, X); 37 } 38 printf("Inorder:"); InorderTraversal(BST); printf("\n"); 39 40 return 0; 41 } 42 43 void PreorderTraversal(BinTree BT) 44 { 45 BinTree T = BT; 46 Stack S = CreateStack(); 47 while(T || !IsEmpty(S)) { 48 while(T) { 49 printf(" %d", T->Data); 50 StackPush(T, S); 51 T = T->Left; 52 } 53 if(!IsEmpty(S)) { 54 T = StackPop(S); 55 T = T->Right; 56 } 57 } 58 } 59 60 void InorderTraversal(BinTree BT) 61 { 62 BinTree T = BT; 63 Stack S = CreateStack(); 64 while(T || !IsEmpty(S)) { 65 while(T) { 66 StackPush(T, S); 67 T = T->Left; 68 } 69 if(!IsEmpty(S)) { 70 T = StackPop(S); 71 printf(" %d", T->Data); 72 T = T->Right; 73 } 74 } 75 } 76 77 BinTree Insert(BinTree BST, ElementType X) 78 { 79 if(!BST) { 80 BST = (BinTree)malloc(sizeof(struct TNode)); 81 BST->Data = X; 82 BST->Left = BST->Right = NULL; 83 } else { 84 if(X < BST->Data) { 85 BST->Left = Insert(BST->Left, X); 86 } else if(X > BST->Data) { 87 BST->Right = Insert(BST->Right, X); 88 } 89 } 90 return BST; 91 } 92 93 94 BinTree Delete(BinTree BST, ElementType X) 95 { 96 BinTree Tmp; 97 if(!BST) printf("Not Found\n"); 98 else { 99 if(X < BST->Data) 100 BST->Left = Delete(BST->Left, X); /* 左子樹遞歸刪除 */ 101 else if(X > BST->Data) 102 BST->Right = Delete(BST->Right, X); /* 右子樹遞歸刪除 */ 103 else { /* 找到需要刪除的結點 */ 104 if(BST->Left && BST->Right) { /* 被刪除的結點有左右子結點 */ 105 Tmp = FindMin(BST->Right); /* 在右子樹中找到最小結點填充刪除結點 */ 106 BST->Data = Tmp->Data; 107 BST->Right = Delete(BST->Right, BST->Data); /* 遞歸刪除要刪除結點的右子樹中最小元素 */ 108 } else { /* 被刪除結點有一個或沒有子結點 */ 109 Tmp = BST; 110 if(!BST->Left) /* 有右孩子或者沒有孩子 */ 111 BST = BST->Right; 112 else if(!BST->Right) /* 有左孩子,一定要加else,不然BST可能是NULL */ 113 BST = BST->Left; 114 free(Tmp); /* 如無左右孩子直接刪除 */ 115 } 116 } 117 } 118 return BST; 119 } 120 121 Position Find(BinTree BST, ElementType X) 122 { 123 if(!BST) 124 return NULL; 125 if(X == BST->Data) 126 return BST; 127 if(X < BST->Data) 128 return Find(BST->Left, X); 129 if(X > BST->Data) 130 return Find(BST->Right, X); 131 } 132 133 Position FindMin(BinTree BST) 134 { 135 if(BST) { 136 while(BST->Left) { 137 BST = BST->Left; 138 } 139 } 140 return BST; 141 } 142 143 Position FindMax(BinTree BST) 144 { 145 if(BST) { 146 while(BST->Right) { 147 BST = BST->Right; 148 } 149 } 150 return BST; 151 }
main.c

03_01_PTA.h

技術分享圖片
 1 #ifndef __03_01_PTA_H_
 2 #define __03_01_PTA_H_
 3 
 4 typedef int ElementType;
 5 typedef struct TNode *Position;
 6 typedef Position BinTree;
 7 struct TNode {
 8     ElementType Data;
 9     BinTree Left;
10     BinTree Right;
11 };
12 
13 void PreorderTraversal(BinTree BT);
14 void InorderTraversal(BinTree BT);
15 
16 BinTree Insert(BinTree BST, ElementType X);
17 BinTree Delete(BinTree BST, ElementType X);
18 Position Find(BinTree BST, ElementType X);
19 Position FindMin(BinTree BST);
20 Position FindMax(BinTree BST);
21 
22 #endif
03_01_PTA.h

stack.c

技術分享圖片
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include "stack.h"
 4 
 5 Stack CreateStack()
 6 {
 7     Stack s;
 8     s = (Stack)malloc(sizeof(struct SNode));
 9     s->Next = NULL;
10     return s;
11 }
12 
13 int IsEmpty(Stack S)
14 {
15     return (S->Next == NULL);
16 }
17 
18 void StackPush(BinTree X, Stack S)
19 {
20     struct SNode *TmpCell;
21     TmpCell = (Stack)malloc(sizeof(struct SNode));
22     TmpCell->Data = X;
23     TmpCell->Next = S->Next;
24     S->Next = TmpCell;
25 }
26 
27 BinTree StackPop(Stack S)
28 {
29     struct SNode *FirstCell;
30     BinTree TopElem;
31     if(IsEmpty(S)) {
32         printf("Stack Empty");
33         return NULL;
34     } else {
35         FirstCell = S->Next;
36         S->Next = FirstCell->Next;
37         TopElem = FirstCell->Data;
38         free(FirstCell);
39         return TopElem;
40     }
41 }
stack.c

stack.h

技術分享圖片
 1 #ifndef __STACK_H_
 2 #define __STACK_H_
 3 
 4 #include "03_01_PTA.h"
 5 
 6 //typedef BinTree ElementType;
 7 typedef struct SNode *Stack;
 8 struct SNode {
 9     BinTree Data;
10     Stack Next;
11 };
12 
13 Stack CreateStack();
14 int IsEmpty(Stack S);
15 void StackPush(BinTree X, Stack S);
16 BinTree StackPop(Stack S);
17 
18 #endif
stack.h

Makefile

SOURCE_FILE = stack.c 03_01_PTA.c
STACK_SOURCE_FILE = stack_test.c stack.c

#all: 03_01 stack_test

#stack_test: $(STACK_SOURCE_FILE)
#    gcc $(STACK_SOURCE_FILE) -o stack_test -g -Wall 

03_01:$(SOURCE_FILE)
    gcc $(SOURCE_FILE) -o 03_01 -g -Wall


clean:
    rm -f stack_test 03_01 

04-樹7 二叉搜索樹的操作集(30 分)